前言
更新于 2020/7/12,和 Una Kravets 邮件确认了下,最近她在 web.dev 也发布了新的关于视频里面布局的 blog,有兴趣的可以前去一观 one line layouts
周日在家看 web.dev 的 2020 三天 live,发现不少有意思的东西,其中有一项是关于 CSS 的,主播是 Una Kravets(chrome team 成员)。虽然我已经好几个月没有深入研究 CSS 的东西了,不过以前的底子还在(有兴趣的可以看我一年前发布的关于 CSS 的东西,虽然由于太过底层没啥人愿意看, sad)。
注意:下面大部分代码已经由各大主流最新浏览器实现,切记不要使用在 production 当中
注意:本文所有的代码来自于 Una Kravets。网络较好的,可以看 Una Kravets 的为 web.dev 专门写的网址,里面有所有的demo - 1linelayouts
正文
01. 超级居中
在没有 flex 和 grid 之前,垂直居中一直不能很优雅的实现。而现在,我们可以结合 grid
和 place-items
优雅的同时实现水平居中和垂直居中。
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent blue"</span> ></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box coral"</span> <span class="hljs-attr">contenteditable</span>></span>
:)
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="copy-code-btn">复制代码</span>
<span class="hljs-selector-class">.ex1</span> <span class="hljs-selector-class">.parent</span> {
<span class="hljs-attribute">display</span>: grid;
<span class="hljs-attribute">place-items</span>: center;
}
<span class="copy-code-btn">复制代码</span>
02. 可解构的自适应布局(The Deconstructed Pancake)
flex: 0 1 <baseWidth>
这种布局经常出现在电商网站:
- 在 viewport 足够的时候,三个 box 固定宽度横放
- 在 viewport 不够的时候(比如在 mobile 上面),宽度仍然固定,但是自动解构(原谅我的中文水平),不在同一水平面上
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent white"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box green"</span>></span>1<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box green"</span>></span>2<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box green"</span>></span>3<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="copy-code-btn">复制代码</span>
<span class="hljs-selector-class">.ex2</span> <span class="hljs-selector-class">.parent</span> {
<span class="hljs-attribute">display</span>: flex;
<span class="hljs-attribute">flex-wrap</span>: wrap;
<span class="hljs-attribute">justify-content</span>: center;
}
<span class="hljs-selector-class">.ex2</span> <span class="hljs-selector-class">.box</span> {
<span class="hljs-attribute">flex</span>: <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">150px</span>; <span class="hljs-comment">/* flex-grow: 1 ,表示自动延展到最大宽度 */</span>
<span class="hljs-attribute">flex</span>: <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">150px</span>; <span class="hljs-comment">/* No stretching: */</span>
<span class="hljs-attribute">margin</span>: <span class="hljs-number">5px</span>;
}
<span class="copy-code-btn">复制代码</span>
当我们设置 flex: 1 1 150px;
时候:
03. 经典的 sidebar
grid-template-columns: minmax(<min>, <max>) ...
同样使用 grid
layout,可以结合 minmax()
实现弹性的 sidebar(这在你要适应大屏幕的时候很有用)。minmax(<min>, <max>)
就是字面意思。结合 <flex>
单位,非常优雅,避免了数学计算宽度等不灵活的手段(比如我们设置 gap 的时候)。
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"section yellow"</span> <span class="hljs-attr">contenteditable</span>></span>
Min: 150px / Max: 25%
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"section purple"</span> <span class="hljs-attr">contenteditable</span>></span>
This element takes the second grid position (1fr), meaning
it takes up the rest of the remaining space.
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="copy-code-btn">复制代码</span>
<span class="hljs-selector-class">.ex3</span> <span class="hljs-selector-class">.parent</span> {
<span class="hljs-attribute">display</span>: grid;
<span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">minmax</span>(150px, 25%) <span class="hljs-number">1</span>fr;
}
<span class="copy-code-btn">复制代码</span>
04. 固定的 header 和 footer
grid-template-rows: auto 1fr auto
固定高度的 header 和 footer,占据剩余空间的 body 是经常使用的布局,我们可以利用 grid
和 fr
单位完美实现。
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">header</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"blue section"</span> <span class="hljs-attr">contenteditable</span>></span>Header<span class="hljs-tag"></<span class="hljs-name">header</span>></span>
<span class="hljs-tag"><<span class="hljs-name">main</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"coral section"</span> <span class="hljs-attr">contenteditable</span>></span>Main<span class="hljs-tag"></<span class="hljs-name">main</span>></span>
<span class="hljs-tag"><<span class="hljs-name">footer</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"purple section"</span> <span class="hljs-attr">contenteditable</span>></span>Footer Content<span class="hljs-tag"></<span class="hljs-name">footer</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="copy-code-btn">复制代码</span>
<span class="hljs-selector-class">.ex4</span> <span class="hljs-selector-class">.parent</span> {
<span class="hljs-attribute">display</span>: grid;
<span class="hljs-attribute">grid-template-rows</span>: auto <span class="hljs-number">1</span>fr auto;
}
<span class="copy-code-btn">复制代码</span>
05. 经典的圣杯布局(classical holy Grail layout)
grid-template: auto 1fr auto / auto 1fr auto
我们可以轻松的使用 Grid 布局来实现圣杯布局,并且是弹性的。
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">header</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pink section"</span>></span>Header<span class="hljs-tag"></<span class="hljs-name">header</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"left-side blue section"</span> <span class="hljs-attr">contenteditable</span>></span>Left Sidebar<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">main</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"section coral"</span> <span class="hljs-attr">contenteditable</span>></span> Main Content<span class="hljs-tag"></<span class="hljs-name">main</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"right-side yellow section"</span> <span class="hljs-attr">contenteditable</span>></span>Right Sidebar<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">footer</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"green section"</span>></span>Footer<span class="hljs-tag"></<span class="hljs-name">footer</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="copy-code-btn">复制代码</span>
<span class="hljs-selector-class">.ex5</span> <span class="hljs-selector-class">.parent</span> {
<span class="hljs-attribute">display</span>: grid;
<span class="hljs-attribute">grid-template</span>: auto <span class="hljs-number">1</span>fr auto / auto <span class="hljs-number">1</span>fr auto;
}
<span class="hljs-selector-class">.ex5</span> <span class="hljs-selector-tag">header</span> {
<span class="hljs-attribute">padding</span>: <span class="hljs-number">2rem</span>;
<span class="hljs-attribute">grid-column</span>: <span class="hljs-number">1</span> / <span class="hljs-number">4</span>;
}
<span class="hljs-selector-class">.ex5</span> <span class="hljs-selector-class">.left-side</span> {
<span class="hljs-attribute">grid-column</span>: <span class="hljs-number">1</span> / <span class="hljs-number">2</span>;
}
<span class="hljs-selector-class">.ex5</span> <span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">grid-column</span>: <span class="hljs-number">2</span> / <span class="hljs-number">3</span>;
}
<span class="hljs-selector-class">.ex5</span> <span class="hljs-selector-class">.right-side</span> {
<span class="hljs-attribute">grid-column</span>: <span class="hljs-number">3</span> / <span class="hljs-number">4</span>;
}
<span class="hljs-selector-class">.ex5</span> <span class="hljs-selector-tag">footer</span> {
<span class="hljs-attribute">grid-column</span>: <span class="hljs-number">1</span> / <span class="hljs-number">4</span>;
}
<span class="copy-code-btn">复制代码</span>
06. 有意思的叠块
使用 grid-template-columns
和 grid-column
可以实现如下图所示的布局。进一步说明了 repeat
和 fr
的便捷性。
07. RAM 技巧
grid-template-columns: repeat(auto-fit, minmax(<base>, 1fr))
Una Kravets 称之为 repeat, auto, minmax 技巧。这在弹性布局 图片/box 这类非常有用(一行可以排放的卡片数量自动适应)。
<span class="hljs-selector-class">.ex7</span> <span class="hljs-selector-class">.parent</span> {
<span class="hljs-attribute">display</span>: grid;
<span class="hljs-attribute">grid-gap</span>: <span class="hljs-number">1rem</span>;
<span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(auto-fit, minmax(150px, 1fr));
}
<span class="copy-code-btn">复制代码</span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent white"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box pink"</span>></span>1<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box purple"</span>></span>2<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box blue"</span>></span>3<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box green"</span>></span>4<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="copy-code-btn">复制代码</span>
其效果是如果能够满足多个 box 的最小宽度(比如上面的 150px
),自动弹性适应放在多行。 举个例子:
- 当前宽度是
160px
(不考虑 gap),那么上面四个box
的宽度会自适应为160px
,并且分为 4 行 - 当前宽度是
310px
(不考虑 gap),上面四个box
分成两行,两个box
平分宽度 - 当满足一行放下 3 个box 时,第三个 box 自动到第一行
- 当满足一行放下 4 个 box 时,第四个 box 自动到第一行
如果我们将 auto-fit
改为 auto-fill
:
08. 卡片弹性自适应
justify-content: space-between
,结合 grid
和 flex
实现完美的卡片布局。
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent white"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card yellow"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">h3</span>></span>Title - Card 1<span class="hljs-tag"></<span class="hljs-name">h3</span>></span>
<span class="hljs-tag"><<span class="hljs-name">p</span> <span class="hljs-attr">contenteditable</span>></span>Medium length description with a few more words here.<span class="hljs-tag"></<span class="hljs-name">p</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"visual pink"</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card yellow"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">h3</span>></span>Title - Card 2<span class="hljs-tag"></<span class="hljs-name">h3</span>></span>
<span class="hljs-tag"><<span class="hljs-name">p</span> <span class="hljs-attr">contenteditable</span>></span>Long Description. Lorem ipsum dolor sit, amet consectetur adipisicing elit.<span class="hljs-tag"></<span class="hljs-name">p</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"visual blue"</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card yellow"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">h3</span>></span>Title - Card 3<span class="hljs-tag"></<span class="hljs-name">h3</span>></span>
<span class="hljs-tag"><<span class="hljs-name">p</span> <span class="hljs-attr">contenteditable</span>></span>Short Description.<span class="hljs-tag"></<span class="hljs-name">p</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"visual green"</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="copy-code-btn">复制代码</span>
<span class="hljs-selector-class">.ex8</span> <span class="hljs-selector-class">.parent</span> {
<span class="hljs-attribute">height</span>: auto;
<span class="hljs-attribute">display</span>: grid;
<span class="hljs-attribute">grid-gap</span>: <span class="hljs-number">1rem</span>;
<span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(3, 1fr);
}
<span class="hljs-selector-class">.ex8</span> <span class="hljs-selector-class">.visual</span> {
<span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;
<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
<span class="hljs-selector-class">.ex8</span> <span class="hljs-selector-class">.card</span> {
<span class="hljs-attribute">display</span>: flex;
<span class="hljs-attribute">flex-direction</span>: column;
<span class="hljs-attribute">padding</span>: <span class="hljs-number">1rem</span>;
<span class="hljs-attribute">justify-content</span>: space-between;
}
<span class="hljs-selector-class">.ex8</span> <span class="hljs-selector-tag">h3</span> {
<span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>
}
<span class="copy-code-btn">复制代码</span>
无论是宽度或高度的收缩还是延展,都可以完美的展现 card 的布局。
09. 使用 clamp 实现 fluid typography
clamp(<min>, <actual>, <max>)
使用最新的 clamp()
方法可以一行实现 fluid typography。提高 UX,非常适合包含阅读内容的 card,因为我们不希望一行字太短或太长。
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent white"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card purple"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">h1</span>></span>Title Here<span class="hljs-tag"></<span class="hljs-name">h1</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"visual yellow"</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">p</span>></span>Descriptive Text. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Sed est error repellat veritatis.<span class="hljs-tag"></<span class="hljs-name">p</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="copy-code-btn">复制代码</span>
<span class="hljs-selector-class">.ex9</span> <span class="hljs-selector-class">.parent</span> {
<span class="hljs-attribute">display</span>: grid;
<span class="hljs-attribute">place-items</span>: center;
}
<span class="hljs-selector-class">.ex9</span> <span class="hljs-selector-class">.card</span> {
<span class="hljs-attribute">width</span>: <span class="hljs-built_in">clamp</span>(23ch, 50%, 46ch);
<span class="hljs-attribute">display</span>: flex;
<span class="hljs-attribute">flex-direction</span>: column;
<span class="hljs-attribute">padding</span>: <span class="hljs-number">1rem</span>;
}
<span class="hljs-selector-class">.ex9</span> <span class="hljs-selector-class">.visual</span> {
<span class="hljs-attribute">height</span>: <span class="hljs-number">125px</span>;
<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}
<span class="copy-code-btn">复制代码</span>
10. 完美实现比例
aspect-ratio: <width> / <height>
在展现 CMS 或其他设计内容时,我们会期望图片、video、卡片能够按照固定的比例进行布局。而最新的 aspect-ratio
就能优雅的实现该功能(使用 chrome 84+)
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent white"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card blue"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">h1</span>></span>Video Title<span class="hljs-tag"></<span class="hljs-name">h1</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"visual green"</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">p</span>></span>Descriptive Text. This demo works in Chromium 84+.<span class="hljs-tag"></<span class="hljs-name">p</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="copy-code-btn">复制代码</span>
<span class="hljs-selector-class">.ex10</span> <span class="hljs-selector-class">.parent</span> {
<span class="hljs-attribute">display</span>: grid;
<span class="hljs-attribute">place-items</span>: center;
}
<span class="hljs-selector-class">.ex10</span> <span class="hljs-selector-class">.visual</span> {
<span class="hljs-attribute">aspect-ratio</span>: <span class="hljs-number">16</span> / <span class="hljs-number">9</span>;
}
<span class="hljs-selector-class">.ex10</span> <span class="hljs-selector-class">.card</span> {
<span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
<span class="hljs-attribute">display</span>: flex;
<span class="hljs-attribute">flex-direction</span>: column;
<span class="hljs-attribute">padding</span>: <span class="hljs-number">1rem</span>;
}
<span class="copy-code-btn">复制代码</span>