我可以将图像设置为网格项目吗?

发布于 2025-02-11 19:22:11 字数 1098 浏览 1 评论 0原文

我有3个这样的元素网格:

“

现在我想用图像替换蓝色元素(可能只是虚拟图像) 。**

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: minmax(30em, auto);
  justify-items: stretch;
  align-items: stretch;
}

.grid-item-1 {
  background: url("https://editzstock.com/wp-content/uploads/2022/04/broken-text-300x300.png");
  background-size: cover;
  overflow: hidden;
}

.grid-item-2 {}

.grid-item-3 {
  grid-column: span 2;
  grid-row: span 1;
}

.grid-item {
  position: relative;
  font-size: 25px;
  padding: 20px;
  padding-top: 50px;
  background-color: #379AD6;
  color: #222;
  border: 1px solid white;
}
<div class="grid-container">
  <div class="grid-item grid-item-1">
  </div>
  <div class="grid-item grid-item-2">
  </div>
  <div class="grid-item grid-item-3"></div>
</div>

I have grid with 3 elements like this:

this

Now I want to replace blue elements with images (could be just dummy images).**

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: minmax(30em, auto);
  justify-items: stretch;
  align-items: stretch;
}

.grid-item-1 {
  background: url("https://editzstock.com/wp-content/uploads/2022/04/broken-text-300x300.png");
  background-size: cover;
  overflow: hidden;
}

.grid-item-2 {}

.grid-item-3 {
  grid-column: span 2;
  grid-row: span 1;
}

.grid-item {
  position: relative;
  font-size: 25px;
  padding: 20px;
  padding-top: 50px;
  background-color: #379AD6;
  color: #222;
  border: 1px solid white;
}
<div class="grid-container">
  <div class="grid-item grid-item-1">
  </div>
  <div class="grid-item grid-item-2">
  </div>
  <div class="grid-item grid-item-3"></div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2025-02-18 19:22:11

仅在过去7年中的经验中说,作为网格 /弹性物品的图像通常会以一种或另一种方式故障。

几乎总是通过将图像嵌套在容器中,使容器成为网格 / Flex项目,从而解决了小故障。

尽管图像作为网格 / flex项目有效,但直到各种浏览器在此问题上取得了更多进展之前,我建议您使用一个容器(从而将图像保持在块格式的上下文中)。

Speaking just from experience over the past 7 years, images as grid / flex items often glitch in one way or another.

The glitch is almost always resolved by nesting the image in a container, making the container the grid / flex item.

Although images are valid as grid / flex items, until the various browsers make some more progress on this issue, I would suggest you use a container (thus keeping the images in a block formatting context).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文