【问题标题】:Most efficient way to code a responsive 2 (50%) / 3 (33%) column CSS-grid layout?编写响应式 2 (50%) / 3 (33%) 列 CSS 网格布局的最有效方法?
【发布时间】:2019-04-28 01:14:34
【问题描述】:

我目前正在尝试使用 CSS Grid 布局一个相当简单的响应式图像网格(见图)

我正在尝试创建的 CSS 网格布局的静态图像:

第一行将显示两张图片(均为 50% 宽度),第二行将显示三张图片(每张大约为 33%)。

我对使用 Flexbox 的这种布局没有任何问题,但 CSS Grid 解决方案却让我望而却步。作为记录,我仍然在掌握 CSS Grid,所以问题可能出在我身上?这种布局可行吗?任何提示/提示将不胜感激。

【问题讨论】:

  • edit 发帖并添加您尝试过的minimal reproducible example(包括相关的HTML+CSS)
  • 使用 6 列网格 - 第一行网格项跨越 3 列,第二行网格项分别跨越 2 列...您可以看到一个使用网格(或弹性框)的示例 @987654323 @ 其中第一行是 4 列,第二行是 3 列,依此类推,但类似的逻辑...
  • 谢谢你,阿隆。您提供的链接非常有帮助。

标签: css grid-layout


【解决方案1】:

这是一个例子。您可以使用这些单位来满足您的尺寸要求。代码在这里:

//HTML
<div class="gridcontainer">
    <div class="bigimage1"></div>
    <div class="bigimage2"></div>
    <div class="smallimage1"></div>
    <div class="smallimage2"></div>
    <div class="smallimage3"></div>
</div>

//CSS
.gridcontainer{
    background: #666;
    border: .65vw solid #666;
    display: grid;
    height: 81.75vh;
    width: 57.5vw;
    grid-gap: .7vw;
    grid-template-columns: 9vw 9vw 9vw 9vw 9vw 9vw;
    grid-template-rows: 48vh 32.5vh;
    grid-template-areas: 
    "bigimageleft bigimageleft bigimageleft bigimageright bigimageright bigimageright"
    "smallimageleft smallimageleft smallimagecenter smallimagecenter smallimageright smallimageright";
    }

.bigimage1{
    grid-area: bigimageleft;
    background: white;
    }
.bigimage2{
    grid-area: bigimageright;
    background: white;
    }
.smallimage1{
    grid-area: smallimageleft;
    background: white;
}
.smallimage2{
    grid-area: smallimagecenter;
    background: white;
}
.smallimage3{
    grid-area: smallimageright;
    background: white;
    }

在此处查看示例:https://jsfiddle.net/L5gbfv7m/11/

【讨论】:

  • 谢谢你,克里斯托弗。非常感谢您花时间为我发布代码解决方案。对您的代码稍作修改(以适合我的项目),我现在有了我一直在瞄准的解决方案……但没有完全管理! :) ~露西。
  • 没问题。很高兴我能帮上忙。如果您发现答案有用,您介意在它旁边标记“已接受”复选框吗?它将帮助其他人在未来找到此解决方案。
猜你喜欢
  • 1970-01-01
  • 2019-12-18
  • 1970-01-01
  • 2014-11-20
  • 2014-02-11
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多