居中
水平居中网格容器相对简单。在父对象上使用弹性对齐特性:
body {
display: flex;
justify-content: center;
}
最大宽度
max-width
或
flex
财产。
.wrapper {
flex: 600px 0 1;
}
这条规则说:
-
flex-basis: 600px
起始宽度(mm)
-
flex-grow: 0
(项目不能扩展到超过600px)
-
flex-shrink: 1
(物品会缩水)
max-width: 600px
.
2列布局
你写道:
第一列(logo和sidebar)应该缩小以适合它们的内容,因此第一列的宽度与logo/sidebar之间的宽度相同。导航/内容应该填充最大宽度所允许的剩余空间。
试试这个:
.wrapper {
flex: 600px 0 1;
display: grid;
grid-template-columns: min-content 1fr;
}
body {
margin: 40px;
display: flex;
justify-content: center;
}
.wrapper {
flex: 600px 0 1;
display: grid;
grid-gap: 10px;
grid-template-columns: min-content 1fr;
grid-template-areas: "logo nav"
"sidebar content"
"footer footer";
background-color: #fff;
color: #444;
}
.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;
}
.fill {
background-color: gray;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 10px;
}
.header,
.footer {
background-color: #999;
}
<div class="wrapper">
<div class="box logo">Logo</div>
<div class="box nav">Nav</div>
<div class="box sidebar">Sidebar</div>
<div class="box content">Content
<br /> These lines wrap when the text hits 600px maximum width. These lines wrap when the text hits 600px maximum width. These lines wrap when the text hits 600px maximum width. These lines wrap when the text hits 600px maximum width. These lines wrap when the text hits 600px maximum width. These lines wrap when the text hits 600px maximum width.</div>
<div class="box footer">Footer</div>
</div>