4

Need a responsive img inside a container with a max-width.

HTML:

<div class="center-content">
    <img src="assets/test-slider.png" alt="" />
</div>

CSS:

/*center-content*/
.center-content{
    position:relative;
    max-width:1200px;
    margin:0 auto;}


/*img-fix*/
img{
    max-width: 100%;
    height: auto;
}

This works fine in -webkit but not in firefox...

Any help would be appreciated.

/EDIT/

http://jsfiddle.net/WKHHR/

Check the difference between Firefox & Chrome

6
  • any demo or fiddle of your code will be appreciate.
    – Nitesh
    May 31, 2013 at 12:27
  • Please explain what is not working about it. May 31, 2013 at 12:28
  • When resizing browser-width to under the 1200px the img stay's in its original size. While in Chrome and Safarie it changes size according to the browser with.
    – ronni
    May 31, 2013 at 12:30
  • It works fine for me in Firefox, using v24.0a1. May 31, 2013 at 12:38
  • i see... works in fiddle for me to.. STUPID.. must be somthing to do with @media i think..
    – ronni
    May 31, 2013 at 12:44

1 Answer 1

12

the CSS for your image is wrong instead of

img{
    max-width: 100%;
    height: auto;
}

put

img{
    width: 100%;
    height: auto;
}

This is because you always want the image width to be 100% of it's parent div.

3
  • Its always the thing that are simple... Working to long on the same code i think, THNX!
    – ronni
    May 31, 2013 at 12:52
  • this also worked for me when embedding images in Zurb Foundation. On firefox they wern't resizing and thus breaking out of their columns.
    – KingFu
    Jun 14, 2013 at 23:22
  • this may work, but it's not a universal answer. max-width is not the same as width. In the general case, setting width to 100% is wrong - this shouldn't be set globally, and should only be set on images where you explicitly require it. Setting max-width to 100% globally, on the other hand, is a good default.
    – Larry
    Jun 3, 2015 at 15:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.