超级居中
- <div class="parent" >
- <div class="box coral" contenteditable>
- 测试
- div>
- div>
- .parent {
- display: grid;
- /*等价于align-items和justify-items都设置为center*/
- place-items: center;
- }
关于flex,前置知识有一定的介绍。
- <div class="parent white">
- <div class="box green">1div>
- <div class="box green">2div>
- <div class="box green">3div>
- div>
- .parent {
- display: flex;
- flex-wrap: wrap;
- justify-content: center;
- }
-
- .box {
- flex: 1 1 150px; /* Stretching: */
- flex: 0 1 150px; /* No stretching: */
- margin: 5px;
- }
同样使用 grid layout,可以结合 minmax() 实现弹性的 sidebar(这在你要适应大屏幕的时候很有用)。minmax(,) 就是字面意思。结合单位,非常优雅,避免了数学计算宽度等不灵活的手段(比如我们设置 gap 的时候)。
- <div class="parent">
- <div class="section yellow" contenteditable>
- Min: 150px / Max: 25%
- div>
- <div class="section purple" contenteditable>
- This element takes the second grid position (1fr), meaning
- it takes up the rest of the remaining space.
- div>
- div>
- .parent {
- display: grid;
- /分两列,一列最小150px,最大占比25%, 一列自适应*/
- grid-template-columns: minmax(150px, 25%) 1fr;
- }
固定高度的 header 和 footer,占据剩余空间的 body 是经常使用的布局,我们可以利用 grid 和 fr 单位完美实现。
- <div class="parent">
- <header class="blue section" contenteditable>Headerheader>
- <main class="coral section" contenteditable>Mainmain>
- <footer class="purple section" contenteditable>Footer Contentfooter>
- div>
- .parent {
- display: grid;
- /* 分三行,1,3行auto,2行自适应 */
- grid-template-rows: auto 1fr auto;
- }
可以轻松地使用 Grid 布局来实现圣杯布局,并且是弹性的。
- <div class="parent">
- <header class="pink section">Headerheader>
- <div class="left-side blue section" contenteditable>Left Sidebardiv>
- <main class="section coral" contenteditable> Main Contentmain>
- <div class="right-side yellow section" contenteditable>Right Sidebardiv>
- <footer class="green section">Footerfooter>
- div>
- .parent {
- display: grid;
- grid-template: auto 1fr auto / auto 1fr auto;
- }
-
- header {
- padding: 2rem;
- grid-column: 1 / 4;
- }
-
- .left-side {
- grid-column: 1 / 2;
- }
-
- main {
- grid-column: 2 / 3;
- }
-
- .right-side {
- grid-column: 3 / 4;
- }
-
- footer {
- grid-column: 1 / 4;
- }
可以使用该repeat()功能在 CSS 中快速编写网格。使用:repeat(12, 1fr),对于网格模板,将会为你提供 12 列1fr
使用span关键字。你可以设置起始线,然后设置从该起点跨越的列数。在这种情况下,grid-column: 1 / span 12将等价于grid-column: 1 / 13,grid-column: 2 / span 6将等价于grid-column: 2 / 8
- <div class="parent white">
- <div class="span-12 green section">Span 12div>
- <div class="span-6 coral section">Span 6div>
- <div class="span-4 blue section">Span 4div>
- <div class="span-2 yellow section">Span 2div>
- div>
- .parent {
- display: grid;
- grid-template-columns: repeat(12, 1fr);
- }
-
- .span-12 {
- grid-column: 1 / span 12;
- }
-
- .span-6 {
- grid-column: 1 / span 6;
- }
-
- .span-4 {
- grid-column: 4 / span 4;
- }
-
- .span-2 {
- grid-column: 3 / span 2;
- }
-
- /* centering text */
- .section {
- display: grid;
- place-items: center;
- text-align: center
- }
2022-03-02 09:08·前端开发
超级居中(Super Centered)
- <div class="parent" >
- <div class="box coral" contenteditable>
- 测试
- div>
- div>
- .parent {
- display: grid;
- /*等价于align-items和justify-items都设置为center*/
- place-items: center;
- }
关于flex,前置知识有一定的介绍。
- <div class="parent white">
- <div class="box green">1div>
- <div class="box green">2div>
- <div class="box green">3div>
- div>
- .parent {
- display: flex;
- flex-wrap: wrap;
- justify-content: center;
- }
-
- .box {
- flex: 1 1 150px; /* Stretching: */
- flex: 0 1 150px; /* No stretching: */
- margin: 5px;
- }
同样使用 grid layout,可以结合 minmax() 实现弹性的 sidebar(这在你要适应大屏幕的时候很有用)。minmax(,) 就是字面意思。结合单位,非常优雅,避免了数学计算宽度等不灵活的手段(比如我们设置 gap 的时候)。
- <div class="parent">
- <div class="section yellow" contenteditable>
- Min: 150px / Max: 25%
- div>
- <div class="section purple" contenteditable>
- This element takes the second grid position (1fr), meaning
- it takes up the rest of the remaining space.
- div>
- div>
- .parent {
- display: grid;
- /分两列,一列最小150px,最大占比25%, 一列自适应*/
- grid-template-columns: minmax(150px, 25%) 1fr;
- }
固定高度的 header 和 footer,占据剩余空间的 body 是经常使用的布局,我们可以利用 grid 和 fr 单位完美实现。
- <div class="parent">
- <header class="blue section" contenteditable>Headerheader>
- <main class="coral section" contenteditable>Mainmain>
- <footer class="purple section" contenteditable>Footer Contentfooter>
- div>
- .parent {
- display: grid;
- /* 分三行,1,3行auto,2行自适应 */
- grid-template-rows: auto 1fr auto;
- }
可以轻松地使用 Grid 布局来实现圣杯布局,并且是弹性的。
- <div class="parent">
- <header class="pink section">Headerheader>
- <div class="left-side blue section" contenteditable>Left Sidebardiv>
- <main class="section coral" contenteditable> Main Contentmain>
- <div class="right-side yellow section" contenteditable>Right Sidebardiv>
- <footer class="green section">Footerfooter>
- div>
- .parent {
- display: grid;
- grid-template: auto 1fr auto / auto 1fr auto;
- }
-
- header {
- padding: 2rem;
- grid-column: 1 / 4;
- }
-
- .left-side {
- grid-column: 1 / 2;
- }
-
- main {
- grid-column: 2 / 3;
- }
-
- .right-side {
- grid-column: 3 / 4;
- }
-
- footer {
- grid-column: 1 / 4;
- }
可以使用该repeat()功能在 CSS 中快速编写网格。使用:repeat(12, 1fr),对于网格模板,将会为你提供 12 列1fr
使用span关键字。你可以设置起始线,然后设置从该起点跨越的列数。在这种情况下,grid-column: 1 / span 12将等价于grid-column: 1 / 13,grid-column: 2 / span 6将等价于grid-column: 2 / 8
- <div class="parent white">
- <div class="span-12 green section">Span 12div>
- <div class="span-6 coral section">Span 6div>
- <div class="span-4 blue section">Span 4div>
- <div class="span-2 yellow section">Span 2div>
- div>
- .parent {
- display: grid;
- grid-template-columns: repeat(12, 1fr);
- }
-
- .span-12 {
- grid-column: 1 / span 12;
- }
-
- .span-6 {
- grid-column: 1 / span 6;
- }
-
- .span-4 {
- grid-column: 4 / span 4;
- }
-
- .span-2 {
- grid-column: 3 / span 2;
- }
-
- /* centering text */
- .section {
- display: grid;
- place-items: center;
- text-align: center
- }
RAM,`repeat、auto-(fit|fill)和minmax()三个的简写,这在弹性布局 图片/box 这类非常有用(一行可以排放的卡片数量自动适应)。
- <div class="parent white">
- <div class="box pink">1div>
- <div class="box purple">2div>
- <div class="box blue">3div>
- <div class="box green">4div>
- div>
- .parent {
- display: grid;
- grid-gap: 1rem;
- /*核心*/
- grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
- }
当前宽度是 160px(不考虑 gap),那么上面四个 box 的宽度会自适应为 160px,并且分为 4 行
当前宽度是 310px (不考虑 gap),上面四个 box 分成两行,两个 box 平分宽度
当满足一行放下 3 个box 时,第三个 box 自动到第一行
当满足一行放下 4 个 box 时,第四个 box 自动到第一行
无论是宽度或高度的收缩还是延展,都可以完美地展现 card 的布局。
- <div class="parent white">
- <div class="card yellow">
- <h3>Title - Card 1h3>
- <p contenteditable>Medium length description with a few more words here.p>
- <div class="visual pink">div>
- div>
- <div class="card yellow">
- <h3>Title - Card 2h3>
- <p contenteditable>Long Description. Lorem ipsum dolor sit, amet consectetur adipisicing elit.p>
- <div class="visual blue">div>
- div>
- <div class="card yellow">
- <h3>Title - Card 3h3>
- <p contenteditable>Short Description.p>
- <div class="visual green">div>
- div>
- div>
- .parent {
- height: auto;
- display: grid;
- grid-gap: 1rem;
- /*核心*/
- grid-template-columns: repeat(3, 1fr);
- }
-
- .visual {
- height: 100px;
- width: 100%;
- }
-
- .card {
- display: flex;
- flex-direction: column;
- padding: 1rem;
- justify-content: space-between;
- }
-
- h3 {
- margin: 0
- }
使用最新的 clamp() 方法可以一行实现 fluid typography。提高 UX,非常适合包含阅读内容的 card,因为我们不希望一行字太短或太长。
- <div class="parent white">
- <div class="card purple">
- <h1>Title Hereh1>
- <div class="visual yellow">div>
- <p>Descriptive Text. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Sed est error repellat veritatis.p>
- div>
- div>
- .parent {
- display: grid;
- place-items: center;
- }
-
- .card {
- /*核心*/
- width: clamp(23ch, 50%, 46ch);
- display: flex;
- flex-direction: column;
- padding: 1rem;
- }
-
- .visual {
- height: 125px;
- width: 100%;
- }
aspect-ratio属性,当我调整卡片的大小时,绿色的视觉块会保持这个 16 x 9 的纵横比。长宽比由于aspect-ratio属性而固定设置为: 16 / 9。
- <div class="parent white">
- <div class="card blue">
- <h1>Video Titleh1>
- <div class="visual green">div>
- <p>Descriptive text for aspect ratio card demo.p>
- div>
- div>
- .parent {
- display: grid;
- place-items: center;
- }
-
- .visual {
- /*核心*/
- aspect-ratio: 16 / 9;
- }
-
- .card {
- width: 50%;
- display: flex;
- flex-direction: column;
- padding: 1rem;
- }
在展现 CMS 或其他设计内容时,我们会期望图片、video、卡片能够按照固定的比例进行布局。而最新的 aspect-ratio 就能优雅地实现该功能(使用 chrome 84+)