让我们考虑仅具有子网格功能的卡片示例。
body {
max-width: 500px;
margin: 1rem auto;
}
.container {
display: grid;
grid-template-columns: 2fr 1fr;
grid-gap: 1rem;
}
.card {
border: 1px solid #ccc;
padding: 1rem;
display: grid;
grid-gap: 1rem;
grid-row: span 3;
grid-template-rows: subgrid;
}
<div class="container">
<div class="card">
<h2>Title Similique quisquam nesciunt</h2>
<p>
Body Lorem ipsum dolor sit, amet consectetur adipisicing elit. Similique quisquam nesciunt nihil totam laborum corporis animi expedita molestias, accusantium amet libero at neque id voluptatum, numquam, natus blanditiis eos sit!
</p>
<p>Footer nesciunt nihil</p>
</div>
<div class="card">
<h2>Title accusantium amet libero at neque</h2>
<p>
Body Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quisquam minima ducimus numquam deleniti perspiciatis expedita nam commodi laboriosam illo! Blanditiis in dolorem eius. Hic assumenda architecto quidem magnam?
</p>
<p>Footer dolor sit amet consectetur adipisicing elit</p>
</div>
</div>
这完美地生成了我想要的内容(标题、正文和页脚垂直对齐):
Codesanbox 首次实现(https://codesandbox.io/s/silent-voice-cklrms),在 2023 年 7 月 23 日,我建议在 Chrome Canary 或 Firefox 中打开它。
当我尝试添加到示例 CSS @container 查询时,我不知道如何保留子网格之前的功能。
考虑对 HTML 的修改,添加
.card-outer-wrapper
、参考 为什么我必须添加此包装器:
body {
max-width: 500px;
margin: 1rem auto;
}
.container {
display: grid;
grid-template-columns: 2fr 1fr;
grid-gap: 1rem;
}
.card {
border: 1px solid #ccc;
padding: 1rem;
display: grid;
grid-gap: 1rem;
grid-row: span 3;
grid-template-rows: subgrid;
}
.card-outer-wrapper {
container-type: inline-size;
}
@container (min-width: 300px) {
.card {
background-color: aqua;
}
}
<div class="container">
<div class="card-outer-wrapper">
<div class="card">
<h2>Title Similique quisquam nesciunt</h2>
<p>
Body Lorem ipsum dolor sit, amet consectetur adipisicing elit. Similique quisquam nesciunt nihil totam laborum corporis animi expedita molestias, accusantium amet libero at neque id voluptatum, numquam, natus blanditiis eos sit!
</p>
<p>Footer nesciunt nihil</p>
</div>
</div>
<div class="card-outer-wrapper">
<div class="card">
<h2>Title accusantium amet libero at neque</h2>
<p>
Body Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quisquam minima ducimus numquam deleniti perspiciatis expedita nam commodi laboriosam illo! Blanditiis in dolorem eius. Hic assumenda architecto quidem magnam?
</p>
<p>Footer dolor sit amet consectetur adipisicing elit</p>
</div>
</div>
</div>
它可以在较宽的卡片上正确显示背景颜色,但缺乏子网格功能:
这是第二个示例的代码沙箱:https://codesandbox.io/s/nifty-feynman-n42s5p
如何修改我的第二个示例以同时实现子网格对齐和@container查询?
由于现在容器和卡片之间有一个中间元素,因此该中间元素必须在它们之间传播行定义。也就是说,它必须是 display: grid;并将模板行作为子网格:
body {
max-width: 500px;
margin: 1rem auto;
}
.container {
display: grid;
grid-template-columns: 2fr 1fr;
grid-gap: 1rem;
}
.card {
border: 1px solid #ccc;
padding: 1rem;
display: grid;
grid-gap: 1rem;
grid-row: span 3;
grid-template-rows: subgrid;
}
.card-outer-wrapper {
container-type: inline-size;
display: grid;
grid-template-rows: subgrid;
}
@container (min-width: 300px) {
.card {
background-color: aqua;
}
}
<div class="container">
<div class="card-outer-wrapper">
<div class="card">
<h2>Title Similique quisquam nesciunt</h2>
<p>
Body Lorem ipsum dolor sit, amet consectetur adipisicing elit. Similique quisquam nesciunt nihil totam laborum corporis animi expedita molestias, accusantium amet libero at neque id voluptatum, numquam, natus blanditiis eos sit!
</p>
<p>Footer nesciunt nihil</p>
</div>
</div>
<div class="card-outer-wrapper">
<div class="card">
<h2>Title accusantium amet libero at neque</h2>
<p>
Body Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quisquam minima ducimus numquam deleniti perspiciatis expedita nam commodi laboriosam illo! Blanditiis in dolorem eius. Hic assumenda architecto quidem magnam?
</p>
<p>Footer dolor sit amet consectetur adipisicing elit</p>
</div>
</div>
</div>