CSS 布局

1、水平居中和垂直居中

结合 gridplace-items优雅的实现同时水平居中垂直居中

  1. <div class="parent" >
  2. <div class="child" contenteditable>:)</div>
  3. </div>
  1. .parent {
  2. display: grid;
  3. place-items: center;
  4. background: lightblue;
  5. width: 500px;
  6. height: 500px;
  7. resize: both;
  8. overflow: auto;
  9. }
  10. .child {
  11. // etc.
  12. padding: 0.5rem;
  13. border-radius: 10px;
  14. border: 1px solid red;
  15. background: lightpink;
  16. font-size: 2rem;
  17. text-align: center;
  18. }
  19. body {
  20. font-family: system-ui, serif;
  21. }

CSS实现现代的10种布局 - 图1
源码地址:https://codepen.io/una/pen/YzyYbBx

2、可解构的自适应布局(The Deconstructed Pancake)

这种布局经常出现在电的网站:

  1. 在视口足够的时候,三个框固定宽度横放
  2. 在视口不够的时候(宽度在移动上面),宽度仍然固定,,但是自动解构(原谅我的中文水平),不在同一水平面上
    1. <div class="parent">
    2. <div class="child">1</div>
    3. <div class="child">2</div>
    4. <div class="child">3</div>
    5. </div>
    ```css .parent { display: flex; flex-wrap: wrap; }

.child { // flex: none | [ <’flex-grow’> <’flex-shrink’>? || <’flex-basis’> ] // If we don’t want the items to stretch: flex: 0 1 300px; // If we do want the items to stretch: flex: 1 1 300px;

// etc. border: 1px solid red; background: lightpink; font-size: 2rem; text-align: center; }

body { font-family: system-ui, serif; }

  1. ![01.gif](https://cdn.nlark.com/yuque/0/2020/gif/396745/1598243540712-2d0a5eaa-a7cd-4a86-b9e3-b84534fa5bc9.gif#align=left&display=inline&height=1185&originHeight=1185&originWidth=1656&size=2076000&status=done&style=shadow&width=1656)<br />当设置`flex: 1 1 150px;`时候:<br />![02.gif](https://cdn.nlark.com/yuque/0/2020/gif/396745/1598254738271-32e0a855-f8bf-470f-822f-4b8e224cf395.gif#align=left&display=inline&height=1264&originHeight=1264&originWidth=1720&size=2999341&status=done&style=shadow&width=1720)<br />源码地址:[https://codepen.io/una/pen/WNQdBza](https://codepen.io/una/pen/WNQdBza)
  2. <a name="SLsWy"></a>
  3. ### 3、经典的侧边栏
  4. 同样使用`grid`布局,结合可以`minmax()`实现弹性的伸缩,这在要适应大屏幕的时候很有用)。`minmax(<min>, <max>)`就是字面意思。结合`<flex>`单位,非常优雅,避免了数学计算宽度等不灵活的手段(设置间隙手段的时候)。
  5. ```html
  6. <div class="sidebar" contenteditable>
  7. Min: 150px
  8. <br/>
  9. Max: 25%
  10. </div>
  11. <p class="content" contenteditable>
  12. Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nulla architecto maxime modi nisi. Quas saepe dolorum, architecto quia fugit nulla! Natus, iure eveniet ex iusto tempora animi quibusdam porro?
  13. </p>
  1. body {
  2. display: grid;
  3. grid-template-columns: minmax(150px, 25%) 1fr;
  4. padding: 0;
  5. margin: 0;
  6. }
  7. .sidebar {
  8. height: 100vh;
  9. // etc.
  10. background: lightpink;
  11. font-size: 2rem;
  12. text-align: center;
  13. }
  14. .content {
  15. padding: 2rem;
  16. }
  17. body {
  18. font-family: system-ui, serif;
  19. }

03.gif
源码地址:https://codepen.io/una/pen/gOaNeWL

4、固定的页眉和页脚

固定高度的页眉和页脚,并保留剩余空间的主体是经常使用的布局,可以利用grid和fr单位完子实现。

  1. <header><h1>Header.com</h1></header>
  2. <main></main>
  3. <footer>Footer Content — Header.com 2020</footer>
  1. body {
  2. display: grid;
  3. height: 100vh;
  4. grid-template-rows: auto 1fr auto;
  5. }
  6. // etc
  7. header {
  8. background: lightpink;
  9. padding: 2rem;
  10. }
  11. main {
  12. background: coral;
  13. }
  14. footer {
  15. background: wheat;
  16. padding: 2rem;
  17. text-align: center;
  18. }
  19. body {
  20. font-family: system-ui, sans-serif;
  21. }

04.gif
源码地址:https://codepen.io/una/pen/bGVXPWB

5、经典的圣杯布局(古典圣杯布局)

  1. <header><h1 contenteditable>Header.com</h1></header>
  2. <div class="left-sidebar" contenteditable>Left Sidebar</div>
  3. <main contenteditable></main>
  4. <div class="right-sidebar" contenteditable>Right Sidebar</div>
  5. <footer contenteditable>Footer Content — Header.com 2020</footer>
  1. body {
  2. display: grid;
  3. height: 100vh;
  4. grid-template: auto 1fr auto / auto 1fr auto
  5. }
  6. // etc
  7. header {
  8. background: lightpink;
  9. padding: 2rem;
  10. grid-column: 1 / 4;
  11. }
  12. .left-sidebar {
  13. background: lightblue;
  14. grid-column: 1 / 2;
  15. }
  16. main {
  17. background: coral;
  18. grid-column: 2 / 3;
  19. }
  20. .right-sidebar {
  21. background: yellow;
  22. grid-column: 3 / 4;
  23. }
  24. footer {
  25. background: wheat;
  26. padding: 2rem;
  27. text-align: center;
  28. grid-column: 1 / 4;
  29. }
  30. body {
  31. font-family: system-ui, sans-serif;
  32. }
  33. .left-sidebar,
  34. .right-sidebar {
  35. padding: 1rem;
  36. }

05.gif
源码地址:https://codepen.io/una/pen/mdVbdBy

6、叠块

使用grid-template-columns状语从句:grid-column可以实现如下图产品所示的布局。说明进一步了repeat状语从句:fr的便捷性。

  1. <div class="span-12">Span 12</div>
  2. <div class="span-6">Span 6</div>
  3. <div class="span-4">Span 4</div>
  4. <div class="span-2">Span 2</div>
  1. body {
  2. display: grid;
  3. height: 100vh;
  4. grid-template-columns: repeat(12, 1fr);
  5. }
  6. // etc
  7. div {
  8. display: grid;
  9. place-items: center;
  10. }
  11. .span-12 {
  12. background: lightpink;
  13. grid-column: 1 / 13;
  14. }
  15. .span-6 {
  16. background: lightblue;
  17. grid-column: 1 / 7;
  18. }
  19. .span-4 {
  20. background: coral;
  21. grid-column: 4 / 9;
  22. }
  23. .span-2 {
  24. background: yellow;
  25. grid-column: 3 / 5;
  26. }
  27. body {
  28. font-family: system-ui, sans-serif;
  29. }

image.png
源码地址:https://codepen.io/una/pen/eYJOYjj

7、RAM技巧

这在弹性布局图片/ box这种非常有用(行可以排放的卡片数量自动适应)
其效果是如果确保能够满足多个盒的最小宽度(例如上面的150px),则自动弹性适应放置多行。

  1. 当前宽度是160px(不考虑gap),那么上面四个box的宽度会适应为160px,并且分为4行
  2. 当前宽度是310px(不考虑间隙),上面四个box分段两行,两个box平分宽度
  3. 当满足满足一行放下3个box时,第三个box自动到第一行
  4. 当满足满足一行放下4个box时,第四个box自动到第一行
    1. <div>1</div>
    2. <div>2</div>
    3. <div>3</div>
    4. <div>4</div>
    ```css body { display: grid; height: 100vh; grid-gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

// etc

div { display: grid; place-items: center; background: lightpink; }

body { font-family: system-ui, sans-serif; }

  1. ![01.gif](https://cdn.nlark.com/yuque/0/2020/gif/396745/1598256388595-5726cca4-60a9-4eec-926c-f5cffa734c4c.gif#align=left&display=inline&height=1083&originHeight=1083&originWidth=1650&size=2437890&status=done&style=shadow&width=1650)<br />如果将`auto-fit`对划线`auto-fill`:<br />![02.gif](https://cdn.nlark.com/yuque/0/2020/gif/396745/1598256407084-4f306cad-16da-4c82-83f2-93252f540b8f.gif#align=left&display=inline&height=1078&originHeight=1078&originWidth=1650&size=2424989&status=done&style=shadow&width=1650)<br />源码地址:[https://codepen.io/una/pen/oNbvNQv](https://codepen.io/una/pen/oNbvNQv)
  2. <a name="96JwR"></a>
  3. ### 8、卡片弹性适应性
  4. `justify-content: space-between`,结合`grid``flex`实现完的布局。
  5. ```html
  6. <div class="card">
  7. <h1>Title - Card 1</h1>
  8. <p>Medium length description. Let's add a few more words here.</p>
  9. <div class="visual"></div>
  10. </div>
  11. <div class="card">
  12. <h1>Title - Card 2</h1>
  13. <p>Long Description. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Sed est error repellat veritatis.</p>
  14. <div class="visual"></div>
  15. </div>
  16. <div class="card">
  17. <h1>Title - Card 3</h1>
  18. <p>Short Description.</p>
  19. <div class="visual"></div>
  20. </div>
  1. body {
  2. display: grid;
  3. grid-gap: 1rem;
  4. grid-template-columns: repeat(3, 1fr);
  5. }
  6. // etc
  7. .visual {
  8. height: 100px;
  9. width: 100%;
  10. background: wheat;
  11. margin: 0.5rem 0;
  12. }
  13. .card {
  14. display: flex;
  15. flex-direction: column;
  16. justify-content: space-between;
  17. background: lightpink;
  18. padding: 1rem;
  19. }
  20. body {
  21. font-family: system-ui, sans-serif;
  22. }
  23. h1 {
  24. font-size: 1.5rem;
  25. }

03.gif
无论是宽度或高度的收缩还是延展,都可以完子的展现卡的布局。
源码地址:https://codepen.io/una/pen/ExPYomq

9、使用夹具实现流体印刷

最新使用的clamp()方法可以一行实现流体排版。提高UX,非常适合包含阅读内容的卡,因为不希望一行字太短或太长。

  1. <div class="card">
  2. <h1>Title Here</h1>
  3. <div class="visual"></div>
  4. <p>Descriptive Text. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Sed est error repellat veritatis.</p>
  5. </div>
  1. body {
  2. display: grid;
  3. place-content: center;
  4. height: 100vh;
  5. }
  6. .visual {
  7. height: 100px;
  8. width: 100%;
  9. background: wheat;
  10. margin: 0.5rem 0;
  11. }
  12. .card {
  13. width: clamp(23ch, 50vw, 46ch);
  14. display: flex;
  15. flex-direction: column;
  16. background: lightpink;
  17. padding: 1rem;
  18. }
  19. body {
  20. font-family: system-ui, sans-serif;
  21. }
  22. h1 {
  23. font-size: 1.5rem;
  24. }

04.gif
源码地址:https://codepen.io/una/pen/QWyLxaL

10、比例伸缩

在展现CMS或其他设计内容时,会期待图片,视频,卡片能够按照固定的比例进行布局。而最新的aspect-ratio可以优雅的实现该功能(使用chrome 84+)

  1. <div class="card">
  2. <h1>Title Here</h1>
  3. <div class="visual"></div>
  4. <p>Descriptive Text. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Sed est error repellat veritatis.</p>
  5. </div>
  1. body {
  2. display: grid;
  3. place-items: center;
  4. height: 100vh;
  5. }
  6. .visual {
  7. aspect-ratio: 16/9;
  8. background: wheat;
  9. margin: 0.5rem 0;
  10. }
  11. .card {
  12. width: 80%;
  13. display: flex;
  14. flex-direction: column;
  15. background: lightpink;
  16. padding: 1rem;
  17. }
  18. body {
  19. font-family: system-ui, sans-serif;
  20. }
  21. h1 {
  22. font-size: 1.5rem;
  23. }

05.gif
源码地址:https://codepen.io/una/pen/xxZKzaX