This specific combination of a nested grid item with justify-items: center
on the grid and both object-fit: contain
and width: 100%
on the child produce different child width in Chromium/Firefox vs Safari.
My questions are: (1) Which of them is correct by the spec. (2) Looking for any bug-tracking issues reporting the behavior difference.
#grid {
display: grid;
justify-items: center;
}
img {
width: 100%;
height: 300px;
object-fit: contain;
background: red;
}
<div id="grid">
<div id="container">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bd/Test.svg/620px-Test.svg.png">
</div>
</div>
2
Answers
There are rendering issues between browsers, especially when it comes to newer technologies (like Grid). In this case, you should be able to achieve cross-browser consistency by simply removing the
width: 100%
.I created some additional scenarios to test various values of
justify-items
as well as various container setups across the two browsers.Here are the results:
My guess is that Chrome’s rendering is correct. If the
.container
is styled withwidth: auto
(the default value) then:width: 100%
has no effectThe
.container
is therefore only as wide as the image inside it, except in scenario 4 wherejustify-items: normal
behaves asstretch
(for elements without an aspect ratio), so.container
grows to fill the grid column.My guess is therefore that Webkit is the rendering engine with the bug, so I recommend that you lodge a report there. If the folks there disagree, they will provide their rationale in their response to the report.