5

The image inside the container element having a specific display type behaves differently when using the img { width: 0; } or img { width: 0%; } style rules.

Increasing the value of width using any unit other than % gave the same expected result (img occupies only the shown part of it).


I've tried changing the display type of the container the img is in. The result is shown below.

Container display types in which the img is shown not as expected:

  1. -webkit-inline-box
  2. inline-block
  3. inline-flex
  4. inline-grid
  5. inline-table
  6. table
  7. table-cell
  8. table-row
  9. table-row-group
  10. table-header-group
  11. table-footer-group

Not sure how they're related to each other, probably I'm missing something.


button:first-of-type img {
  width: 0;
}

button:last-of-type img {
  width: 0%;
}
<h3>The image width: 0 (hides it)</h3>

<button>
      <img src="https://picsum.photos/id/1012/200/100">
      <p>Add playlist</p>
</button>


<h3>The image width: 0% (occupies the space (natural img width) and hides it)</h3>

<button>
      <img src="https://picsum.photos/id/1012/200/100">
      <p>Add playlist</p>
</button>


The img { width: 0%; } should work as it was img { width: 0; } without occupying any space, which isn't the case here.

5
  • I'm confused as to what it is you are trying to achieve here. If you don't want to show the image why include it in the first place?
    – Paulie_D
    Commented Jan 2, 2019 at 17:05
  • I'm simply curious why it behaves the way it is. Also, try changing the 0%, to, let's say 30%. You will see the occupied space remains the same while the image size changes to 30% of the parent container.
    – Lukas
    Commented Jan 2, 2019 at 17:07
  • Any ideas, guys?
    – Lukas
    Commented Jan 2, 2019 at 17:44
  • 1
    It's a good question. Wish I had time right now to look into it. Note that the difference appears applicable to image elements only. If you try the width: 0 / width: 0% on other elements, such as the button parent, the rendering is consistent. Commented Jan 2, 2019 at 18:39
  • 2
    @Michael_B not only images, check my answer Commented Jan 2, 2019 at 19:41

2 Answers 2

4

As @Talmid said in his answer we are facing a complex calculation. Using width:0 and width:0% aren't the same.

The first one is an absolute value (a length) that the browser can resolve without the need of any reference but the second one is a percentage value that is relative to the width of the containing block so the browser need to know the width of the containing block first to resolve it. (it will not do the effort to conclude that 0% will be the same as 0)

This issue will happen with all the elements where we have a shrink-to-fit behavior (float, inline-block, etc)

Here is more examples:

img {
  width: 30%;
}

span {
  border: 1px solid;
  display: inline-block;
}
<span>
      <img src="https://picsum.photos/id/1012/200/100">
</span>

<span style="float:left;">
      <img src="https://picsum.photos/id/1012/200/100">
</span>

This can also happen with non-image elements:

span  {
  display:inline-block;
  border:1px solid red;
}
<div style="float:left;border: 1px solid;">
      <span >some text</span>
</div>
<div style="float:left;border: 1px solid;clear:left;">
      <span style="width:30%;">some text</span>
</div>
<div style="float:left;border: 1px solid;clear:left;">
      <span style="width:0%;">some text</span>
</div>

Basically, the browser will first define the dimension of the containing block (at this step we don't consider the percentage value defined on the width property of the child element). Logically the dimension of the containing block will be defined by its content (the shrink-to-fit behavior). After that we are able to resolve the percentage value based of the width calculated previously thus the child element will shrink.

Of course we will not get back to calculate the width of the containing block again as we will have a non-ending loop (a cycle).

In case the child element uses a non-percentage value (a length), the browser will consider it first when defining the width of the containing block since it's not a relative value but an absolute one:

span  {
  display:inline-block;
  border:1px solid red;
}
<div style="float:left;border: 1px solid;">
      <span >some text</span>
</div>
<div style="float:left;border: 1px solid;clear:left;">
      <span style="width:30px;">some text</span>
</div>
<div style="float:left;border: 1px solid;clear:left;">
      <span style="width:0;">some text</span>
</div>

Here is the relevant part of the specification detailing this: https://www.w3.org/TR/css-sizing-3/#percentage-sizing

Another related question dealing with the same: How do browsers calculate width when child depends on parent, and parent's depends on child's

7
  • Wow! A really awesome answer, however, it'll take some time for me to understand. The browser tries not to create an endless loop and thus showing us, at first glance, unexpected results. Can you explain a bit more about the ' shrink-to-fit behavior'? What other than float, inline-block elements have it? It means all the elements which have one of those display types I`ve listed above going to have shrink-to-fit behavior? This is so confusing for me right now.
    – Lukas
    Commented Jan 2, 2019 at 20:03
  • @Lukas basically a shrink-to-fit element is the opposite of full width element. For example a block element will by default take all the width (doesn't care about its content) but an inline-block (a shrink-to-fit) element will have its width defined by its content thus the issue because the content is also having a width relative to his parent (a cycle dependency) .. and yes, what you listed are almost all shrink-to-fit Commented Jan 2, 2019 at 20:15
  • Now I get :) Thanks. I'll experiment with your examples and the project I'm working on. Would be great if you could assist me tomorrow if any questions appeared. So much valuable information :)
    – Lukas
    Commented Jan 2, 2019 at 21:06
  • I think this is as good an explanation as one can provide. But I'm not sure it would apply across all browsers, since width: 0 leaves open a wide range of interpretation. As a general rule, it's better to avoid the use of unitless values for lengths. Some browsers will automatically append %. Others px. Others something else. There's no guarantee of stability. There are few exceptions to this "rule", such as the line-height property, which recommends unitless values. Commented Jan 3, 2019 at 14:10
  • @Michael_B I would say it depends on the property but using 0 doesn't require a unit and I don't think it will get interpreted as percentage. If we refer to the specifcation (w3.org/TR/CSS2/syndata.html#length-units) we can see that 0 is mentionned in the length part and not the percentage part. So the browser will consider 0 as a length. Omitting the unit is a kind of optimization since it's zero. I agree we should avoid unitless with numbers (which is invalid in most of the cases) but with 0 it's not an issue. Commented Jan 3, 2019 at 14:20
2

The container's width is determined by the img width, which is a percentage of the container's width, which is determined by the img width, which ... (circular reference).

The browser resolves this by just setting the container width to the image's pre-scaled width when a percentage width for the img is specified.

Although, it does seem like the special case of 0% could be treated like 0px, but perhaps it is not in order to be consistent with the behavior for other specified percentage values.

1
  • I like your ideas but it's really weird I couldn't find the official docs for this behavior. So it's like guessing now.
    – Lukas
    Commented Jan 2, 2019 at 19:41

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.