1

I can't get the real width or height after image loaded. Does anyone know how to do it in the correct way?

HTML:

<div class="editor-container">
    <img src="{{photoUrl}}" alt="" />
</div>

CSS:

.editor-container {
  position: relative;
  width: 100%;
}

.editor-container img {
  display: block;
  width: 100%;
  height: auto;
}

TypeScript:

ngAfterViewInit(){
    try {
        console.log( document.getElementsByClassName('editor-container') );
        console.log( document.getElementsByClassName('editor-container')[0] );
        console.log( 'clientWidth', document.getElementsByClassName('editor-container')[0].clientWidth );
        console.log( 'scrollWidth', document.getElementsByClassName('editor-container')[0].scrollWidth );
    } catch (err) {
        console.error(err);
    }
}

result:

the console log image

4
  • Do you mean the actual width as in what size the image is set to? Because the current way it's set right now there's a div that limits the size of your image. Commented May 4, 2017 at 3:20
  • @MikeTung Thanks for response! I remove 'width: 100%;' in the .editor-container style, then it works now. Commented May 4, 2017 at 3:26
  • mind picking my solution once I post? Commented May 4, 2017 at 3:27
  • I was trying to set the image width. I ended up using your suggested .scrollWidth into a variable, and then <img src="" width="{{maxWidth}}" /> Thanks Commented Aug 16, 2018 at 7:09

1 Answer 1

1

The problem was solved because the div was limiting the size of the picture.

CSS

.editor-container {
  position: relative;
}

.editor-container img {
  display: block;
  height: auto;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.