html - CSS Grid won't wrap children when setting grid-row
问题描述
Background:
I'm experimenting with Grid CSS and trying to get to this layout which is currently implemented to scale
Problem:
On desktop the element-header should be 8 columns wide and if I don't add grid-rows to element-header
and element
than <div class="element">1</div>
will fill in the next to element-header
. Now if I add grid-rows
my element
will no longer wrap.
Question
How can I fix my grid to match the "expected layout" screenshot above?
i.e. .element
will wrap and start on the second grid row
Code:
HTML:
<section class="section">
<div class="container">
<div class="samba-grid">
<div class="element-header"><h1>I am a lot of header text that only goes 8 columsn wide</h1></div>
<div class="element">1</div>
<div class="element">2</div>
<div class="element">3</div>
<div class="element">4</div>
</div>
</div>
</section>
CSS:
.section {
width: 100%;
display: block;
background: red;
box-sizing: border-box;
padding: 40px 24px;
@media screen and (min-width: 600px) and (max-width: 1139px) {
background: orange;
padding: 56px 48px;
}
@media screen and (min-width: 1140px) {
padding: 64px 48px;
background: green;
}
}
.container {
margin: 0 auto;
background: rgba(244,244,244, .25);
max-width: 599px;
@media screen and (min-width: 600px) and (max-width: 1139px) {
max-width: 1039px;
background: rgba(244,244,244, .25);
}
@media screen and (min-width: 1140px) {
max-width: 1032px;
background: rgba(244,244,244, .25);
}
}
.samba-grid {
display: grid;
background: inherit;
width: 100%;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 'auto auto';
grid-gap: 24px;
@media screen and (min-width: 600px) and (max-width: 1139px) {
grid-template-columns: repeat(6, 1fr);
grid-gap: 48px;
}
@media screen and (min-width: 1140px) {
grid-template-columns: repeat(12, 1fr);
grid-gap: 48px;
}
}
h1 {
font-size: 52px;
}
.element-header {
grid-row: 1;
grid-column: span 8; // SET THIS TO "span 12" TO SEE EXPECTED BEHAVIOR
}
.element {
display: grid; // important to do this.
background: rgba(0,0,0,.3);
grid-column: span 3;
grid-row: 2; // REMOVE THIS TO SEE EXPECTED BEHAVIOR
@media screen and (min-width: 600px) and (max-width: 1139px) {
grid-column: span 3;
}
@media screen and (min-width: 1140px) {
grid-column: span 4;
}
}
解决方案
Take the header out of the grid container. Make it a standalone block-level element.
Below, place the grid container with only the images.
推荐阅读
- python - 为什么 ffmpeg-python 无法找到我的文件?
- windows - 运行 npm install WINDOW 的作业时 c 盘中的 Jenkins mkdir 权限问题
- amazon-web-services - 即使帐户处于生产状态,电子邮件也未在 SES 上验证
- javascript - 如何在没有任何 Oauth 验证的情况下将单个 google 表格单元格值作为变量刮到我的网站?(我正在使用 repl.it)
- sql-server - 对电子邮件创建约束
- elasticsearch - Elasticsearch 整个文档搜索(Smart Search)
- firebase - 有没有办法在 MicrophoneInfo 的帮助下选择特定的麦克风作为 AudioSource 进行音频录制?
- python - 使用 Python 中的 Jack Audio Kit 录制数据
- javascript - Puppeteer 测试对话框/模态输入字段
- c# - jQuery选择所有未回答的多项选择题