【发布时间】:2019-07-19 12:09:29
【问题描述】:
我正在学习 CSS Grid(我知道,早该学习了)。我挑战自己将相对标准的基于浮动的布局转换为网格,但无法弄清楚最后一部分。
我的目标是有一个内容(徽标 + 导航和侧边栏 + 内容)居中的布局,并带有 max-width。例如,logo + nav 应该有一个 600px 的max-width。我还需要一个实心填充背景覆盖视口的全宽(与可变高度徽标/导航行的高度匹配)。
第一列(徽标和侧边栏)应缩小以适合其内容 - 因此第一列的宽度仅与徽标/侧边栏之间的宽度一样宽。然后导航/内容应填充max-width 允许的剩余空间。
以下是我最好的尝试。主要内容的宽度没有填满max-width。相反,主要内容的宽度是 Logo 的宽度 + 250px(由网格列定义的宽度)。
我希望实现的是 - 将 Logo + Nav 的 max-width 定义为特定宽度(例如 600 像素),并让 Logo 列缩小以适合其内容。
body {
margin: 40px;
}
.fill {
grid-column-start: 1;
grid-column-end: 6;
grid-row-start: 1;
grid-row-end: 1;
background-color: gray;
}
.logo {
grid-area: logo;
font-size: calc(1rem + 4vw);
}
.sidebar {
grid-area: sidebar;
}
.content {
grid-area: content;
}
.nav {
grid-area: nav;
text-align: right;
}
.footer {
grid-area: footer;
}
.wrapper {
display: grid;
grid-gap: 10px;
grid-template-columns: auto min-content 120px 120px auto;
grid-template-areas: "... logo nav nav ..." "... sidebar content content ..." "... footer footer footer ...";
background-color: #fff;
color: #444;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 10px;
}
.header,
.footer {
background-color: #999;
}
<div class="wrapper">
<div class="fill"></div>
<div class="box logo">Logo</div>
<div class="box nav">Nav</div>
<div class="box sidebar">Sidebar</div>
<div class="box content">Content
<br /> More content than we had before so this column is now quite tall.</div>
<div class="box footer">Footer</div>
</div>
CSS Grid 是否可以实现这一点,如果可以,如何实现?
【问题讨论】:
-
您的问题自最初发布以来发生了根本性的变化。因此,我的答案现在不完整。
标签: html css flexbox css-grid grid-layout