Skip to content Skip to sidebar Skip to footer

Setting The Image `width: 0` Hides It, While `width: 0%` Makes It Occupy The Space, Too

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 o

Solution 1:

As @Talmid said in his answer we are facing a complex calculation and using width:0 and width:0% isn'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><imgsrc="https://picsum.photos/id/1012/200/100"></span><spanstyle="float:left;"><imgsrc="https://picsum.photos/id/1012/200/100"></span>

This can also happen with non-image elements:

span  {
  display:inline-block;
  border:1px solid red;
}
<divstyle="float:left;border: 1px solid;"><span >some text</span></div><divstyle="float:left;border: 1px solid;clear:left;"><spanstyle="width:30%;">some text</span></div><divstyle="float:left;border: 1px solid;clear:left;"><spanstyle="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;
}
<divstyle="float:left;border: 1px solid;"><span >some text</span></div><divstyle="float:left;border: 1px solid;clear:left;"><spanstyle="width:30px;">some text</span></div><divstyle="float:left;border: 1px solid;clear:left;"><spanstyle="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

Solution 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.

Post a Comment for "Setting The Image `width: 0` Hides It, While `width: 0%` Makes It Occupy The Space, Too"