0

Below I have two versions of a simple flex container:

  • The first one contains an img.
  • The second one contains a div.

Both the img and div have exactly the same styles, which includes max-width: 100%.

For some reason, the behaviour of this percentage max-width style is inconsistent.

Why does the percentage max-width style behave differently on the img compared to the div?

Side note: the same happens when I use display: grid instead of display: flex.

.wrapper {
    width: 300px;
    border: 1px solid;
}

.container {
    display: flex;
}

.img {
    width: 600px;
    max-width: 100%;
    height: 100px;
    background-color: lightGrey;
}
<div class="wrapper">
    <h2>img</h2>
    <div class="container">
        <div class="content">
            <img class="img" />
        </div>
    </div>
    <h2>div</h2>
    <div class="container">
        <div class="content">
            <div class="img"></div>
        </div>
    </div>
</div>

I understand I can make the div behave like the img by adding this style:

.content {
    min-width: 0;
}

However I am keen to understand why the behaviour is inconsistent in the first place.

Oliver Joseph Ash
  • 3,138
  • 2
  • 27
  • 47

0 Answers0