背景:
我正在尝试使用Grid CSS,并尝试采用目前已实现缩放的布局
问题:
在桌面上,元素标题应为8列宽,如果我不向element-header
和element
添加网格行,则<div class="element">1</div>
会填充element-header
的旁边。现在,如果我添加grid-rows
,我的element
将不再自动换行。
问题
如何修复网格以匹配上面的“预期布局”屏幕截图?
即.element
将换行并从第二个网格行开始
代码:
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;
}
}
答案 0 :(得分:1)
将标题从网格容器中取出。使其成为独立的块级元素。
在下面,将仅包含图像的网格容器放置。
答案 1 :(得分:1)
您可以使文本占据整行,然后在内部减小其宽度,从而仅占据所需的宽度。这样,您将阻止第一行,并且任何元素都不能进入那里。
这是一个简化的示例:
.samba-grid {
display: grid;
background: inherit;
width: 100%;
grid-template-columns: repeat(12, 1fr);
grid-gap: 24px;
border:1px solid;
}
.element-header {
grid-row: 1;
grid-column: 1/-1;
}
.element-header > h1 {
/*we take 8 colmuns (without gaps) + 7 gaps*/
width:calc(8*(100% - 11*24px)/12 + 7*24px);
background:red;
margin:0;
}
.samba-grid > span {
height:50px;
grid-column: span 2;
background:green;
}
<div class="samba-grid">
<div class="element-header">
<h1>I am a lot of header text that only goes 8 columsn wide</h1>
</div>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
要使其更加动态和易于处理,您可以考虑使用CSS变量:
:root {
--grid:12;
--gap:24px;
}
.samba-grid {
display: grid;
background: inherit;
width: 100%;
grid-template-columns: repeat(var(--grid), 1fr);
grid-gap: var(--gap);
border:1px solid;
}
.element-header {
grid-row: 1;
grid-column: 1/-1;
--grid-column:8; /*simply adjust this value to control the column*/
}
.element-header > h1 {
width:calc(var(--grid-column)*(100% - (var(--grid) - 1)*var(--gap))/var(--grid) + calc(var(--grid-column) - 1)*var(--gap));
background:red;
margin:0;
}
.samba-grid > span {
height:50px;
grid-column: span 2;
background:green;
}
<div class="samba-grid">
<div class="element-header">
<h1>I am a lot of header text that only goes 8 columsn wide</h1>
</div>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
另一个想法是考虑一个隐藏元素,该元素将占用第一行的剩余空间:
.samba-grid {
display: grid;
background: inherit;
width: 100%;
grid-template-columns: repeat(12, 1fr);
grid-gap: 24px;
border:1px solid;
}
.element-header {
grid-row: 1;
grid-column: span 8;
background:red;
order:-2;
}
.samba-grid:before {
content:"";
order:-1;
grid-column: span 4;
background:blue;
height:2px;
}
.samba-grid > span {
height:50px;
grid-column: span 2;
background:green;
}
<div class="samba-grid">
<div class="element-header">
<h1>I am a lot of header text that only goes 8 columsn wide</h1>
</div>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
请注意,设置grid-row: 2
并不意味着从第二行开始,而是意味着在第二行之内
答案 2 :(得分:1)
我正在指示.element-header
始终跨越可用列的最大数量。对于h1
,我在较大的@media查询中添加了一条规则,以保持其正确缩放(12列中的8列)。另外,我评论了.grid-row: 2
。
.element-header {
grid-column-start: 1;
grid-column-end: -1;
}
h1 {
…
@media screen and (min-width: 1140px) {
width: calc(80% / 120 * 100); /* 8 of 12 columns */
}
演示(使用已编译的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) {
.section {
background: orange;
padding: 56px 48px;
}
}
@media screen and (min-width: 1140px) {
.section {
padding: 64px 48px;
background: green;
}
}
.container {
margin: 0 auto;
background: rgba(244, 244, 244, 0.25);
max-width: 599px;
}
@media screen and (min-width: 600px) and (max-width: 1139px) {
.container {
max-width: 1039px;
background: rgba(244, 244, 244, 0.25);
}
}
@media screen and (min-width: 1140px) {
.container {
max-width: 1032px;
background: rgba(244, 244, 244, 0.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) {
.samba-grid {
grid-template-columns: repeat(6, 1fr);
grid-gap: 48px;
}
}
@media screen and (min-width: 1140px) {
.samba-grid {
grid-template-columns: repeat(12, 1fr);
grid-gap: 48px;
}
}
h1 {
font-size: 52px;
}
@media screen and (min-width: 1140px) {
h1 {
width: calc(80% / 120 * 100);
}
}
.element-header {
grid-row: 1;
grid-column-start: 1;
grid-column-end: -1;
}
.element {
display: grid;
background: rgba(0, 0, 0, 0.3);
grid-column: span 3;
}
@media screen and (min-width: 600px) and (max-width: 1139px) {
.element {
grid-column: span 3;
}
}
@media screen and (min-width: 1140px) {
.element {
grid-column: span 4;
}
}
.element img {
width: 100%;
}
<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 columns wide</h1></div>
<div class="element"><img src="https://placebear.com/160/90" alt=""></div>
<div class="element"><img src="https://placebear.com/160/90" alt=""></div>
<div class="element"><img src="https://placebear.com/160/90" alt=""></div>
<div class="element"><img src="https://placebear.com/160/90" alt=""></div>
</div>
</div>
</section>
答案 3 :(得分:0)
我将添加一个空元素只是为了填充更高的分辨率。
HTML
<div class="element-header"><h1>I am a lot of header text that only goes 8 columsn wide</h1></div>
<div class='filler'></div>
<div class="element"><img src="https://placebear.com/160/90" alt=""></div>
CSS
.filler {
display: none;
@media screen and (min-width: 1140px) {
grid-column: 9 / span 4;
display: initial;
}
}
那样,您可以在该空间上放置一些东西,而自动放置算法不会使用它。
如果分辨率较小,则可以隐藏。