英文:
Flexbox Object-fit Image
问题
我创建了一个非常简单的弹性布局,有两行和5列。我的想法是将图片放在这些框中,但当我添加图片时,它会扭曲网格。
我的代码如下:
<div id="head">
<div class="row">
<div><img src="https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg" /></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
</div>
<div class="row">
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
</div>
</div>
我的CSS如下:
body {
margin: 0;
}
#head .row {
display: flex;
}
#head .row div {
background: #ddd;
flex: 1 0 auto;
height: auto;
margin: 1px;
}
#head .row div::before {
content: '';
float: left;
padding-top: 100%;
}
#head img {
height: 100%;
object-fit: contain;
width: 100%;
}
当我使用object-fit: contain
或cover
添加图像时,似乎什么都没有发生。我的期望是它会适应我的框内。
有人知道我可以调整什么来修复这个问题吗?
没有图像的Fiddle:https://jsfiddle.net/joshrodgers/c7j48zw1/1/
有图像的Fiddle:https://jsfiddle.net/joshrodgers/ro16atsg/
我读到将宽度和高度添加到100%会有助于我的object-fit
,因为这样我就将图像的大小与容器相同...但这似乎并没有改变图像的大小。
任何帮助都将不胜感激
谢谢,
Josh
英文:
I created a very simple flexbox layout, I have two rows and 5 columns. My idea is to put pictures in the boxes, but when I add an image it distorts the grid.
My code looks like:
<div id="head">
<div class="row">
<div><img src="https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg" /></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
</div>
<div class="row">
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
</div>
</div>
My css looks like:
body {
margin: 0;
}
#head .row {
display: flex;
}
#head .row div {
background: #ddd;
flex: 1 0 auto;
height: auto;
margin: 1px;
}
#head .row div::before {
content: '';
float: left;
padding-top: 100%;
}
#head img {
height: 100%;
object-fit: contain;
width: 100%;
}
When I add an image into the mix with object-fit: contain or cover, nothing seems to happen. My expectation is that it would fit inside my box.
Does anyone know what I could adjust to fix this?
Fiddle without the image: https://jsfiddle.net/joshrodgers/c7j48zw1/1/
Fiddle with the image: https://jsfiddle.net/joshrodgers/ro16atsg/
I read that adding the 100% to the width and height would help my object-fit because then I'm making the image the same size as the container...but that doesn't seem to change the size of the image at all.
Any help is appreciated
Thanks,<br />
Josh
答案1
得分: 0
我假设它不起作用是因为您的方框没有明确的大小。 (它们只是被before
伪元素推开,但图像只会进一步推开它们。)我不确定您是否只是使用:before
来给方框一个视觉尺寸,但使用Flexbox可能有更好的方法,比如这样:
<!-- 开始代码段: js隐藏: false控制台: true Babel: false -->
<!-- 语言: lang-css -->
body {
margin: 0;
}
#head .row {
display: flex;
gap: 1px;
flex-wrap: wrap;
}
#head .row div {
background: #ddd;
flex: 1 0 calc(20% - 4px);
aspect-ratio: 1/1;
}
#head img {
height: 100%;
object-fit: cover;
width: 100%;
display: block;
}
<!-- 语言: lang-html -->
<div id="head">
<div class="row">
<div><img src="https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg" /></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
</div>
</div>
<!-- 结束代码段 -->
您可以使用aspect-ratio
获得这些漂亮的正方形方框,但当然还有其他方法可以做到。
英文:
I assume it's not working because your boxes don't have an explicit size. (They're just being pushed open by the before
pseudo-element, but the image then just pushes them further open.) I'm not sure if you're using :before
just to give the boxes visual dimensions, but there may be better approaches with Flexbox, such as this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
margin: 0;
}
#head .row {
display: flex;
gap: 1px;
flex-wrap: wrap;
}
#head .row div {
background: #ddd;
flex: 1 0 calc(20% - 4px);
aspect-ratio: 1/1;
}
#head img {
height: 100%;
object-fit: cover;
width: 100%;
display: block;
}
<!-- language: lang-html -->
<div id="head">
<div class="row">
<div><img src="https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg" /></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
<div><!-- --></div>
</div>
</div>
<!-- end snippet -->
You can get those nice square boxes with aspect-ratio
, but there are other ways to do it, of course.
答案2
得分: 0
如果您想要布局为网格,请使用CSS网格。
body {
margin: 0;
}
.d1 {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 2px;
margin: 2px;
}
.d1>div {
background: #ddd;
aspect-ratio: 1/1;
}
.d1 img {
display: block;
height: 100%;
width: 100%;
object-fit: cover;
object-position: center;
}
<div class="d1">
<div><img src="https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg" /></div>
<div></div>
<div><img src="https://picsum.photos/id/131/500"></div>
<div></div>
<div><img src="https://picsum.photos/id/132/500"></div>
<div></div>
<div><img src="https://picsum.photos/id/133/500"></div>
<div></div>
<div><img src="https://picsum.photos/id/134/500"></div>
<div></div>
</div>
英文:
If you want the layout to be a grid, use a CSS grid.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
margin: 0;
}
.d1 {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 2px;
margin: 2px;
}
.d1>div {
background: #ddd;
aspect-ratio: 1/1;
}
.d1 img {
display: block;
height: 100%;
width: 100%;
object-fit: cover;
object-position: center;
}
<!-- language: lang-html -->
<div class="d1">
<div><img src="https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg" /></div>
<div></div>
<div><img src="https://picsum.photos/id/131/500"></div>
<div></div>
<div><img src="https://picsum.photos/id/132/500"></div>
<div></div>
<div><img src="https://picsum.photos/id/133/500"></div>
<div></div>
<div><img src="https://picsum.photos/id/134/500"></div>
<div></div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论