Flexbox对象适应图像

huangapple go评论67阅读模式
英文:

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: containcover添加图像时,似乎什么都没有发生。我的期望是它会适应我的框内。

有人知道我可以调整什么来修复这个问题吗?

没有图像的Fiddle:https://jsfiddle.net/joshrodgers/c7j48zw1/1/

有图像的Fiddle:https://jsfiddle.net/joshrodgers/ro16atsg/

我读到将宽度和高度添加到100%会有助于我的object-fit,因为这样我就将图像的大小与容器相同...但这似乎并没有改变图像的大小。

任何帮助都将不胜感激 Flexbox对象适应图像

谢谢,
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:

&lt;div id=&quot;head&quot;&gt;
	&lt;div class=&quot;row&quot;&gt;
		&lt;div&gt;&lt;img src=&quot;https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg&quot; /&gt;&lt;/div&gt;
		&lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
		&lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
		&lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
		&lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
	&lt;/div&gt;
	&lt;div class=&quot;row&quot;&gt;
		&lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
		&lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
		&lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
		&lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
		&lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

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: &#39;&#39;;
	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 Flexbox对象适应图像

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

&lt;div id=&quot;head&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div&gt;&lt;img src=&quot;https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg&quot; /&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 结束代码段 -->

您可以使用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 -->

&lt;div id=&quot;head&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div&gt;&lt;img src=&quot;https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg&quot; /&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
    &lt;div&gt;&lt;!-- --&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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&gt;div {
  background: #ddd;
  aspect-ratio: 1/1;
}

.d1 img {
  display: block;
  height: 100%;
  width: 100%;
  object-fit: cover;
  object-position: center;
}

<!-- language: lang-html -->

&lt;div class=&quot;d1&quot;&gt;
  &lt;div&gt;&lt;img src=&quot;https://cdn.britannica.com/98/214598-050-9879F2FA/giant-sequoia-tree-Sequoia-National-Park-California.jpg&quot; /&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;img src=&quot;https://picsum.photos/id/131/500&quot;&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;img src=&quot;https://picsum.photos/id/132/500&quot;&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;img src=&quot;https://picsum.photos/id/133/500&quot;&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;img src=&quot;https://picsum.photos/id/134/500&quot;&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月26日 05:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定