垂直水平居中

宽高定不定看的是子元素

定宽高

absolute + 负 margin

  1. .father {
  2. width: 400px;
  3. height: 400px;
  4. position: relative;
  5. }
  6. .child {
  7. width: 200px;
  8. height: 200px;
  9. position: absolute;
  10. top: 50%;
  11. left: 50%;
  12. margin-top: -100px;
  13. margin-left: -100px;
  14. }

absolute + margin auto

  1. .father {
  2. width: 400px;
  3. height: 400px;
  4. position: relative;
  5. }
  6. /* 子元素要定宽高,不然就会占领整个父元素 */
  7. .child {
  8. width: 200px;
  9. height: 200px;
  10. position: absolute;
  11. top: 0;
  12. left: 0;
  13. bottom: 0;
  14. right: 0;
  15. margin: auto;
  16. }

absolute + calc

  1. .father {
  2. width: 400px;
  3. height: 400px;
  4. position: relative;
  5. }
  6. .child {
  7. width: 200px;
  8. height: 200px;
  9. position: absolute;
  10. top: calc(50% - 100px);
  11. left: calc(50% - 100px);
  12. }

不定宽高

子元素不定宽高

absolute + transform

  1. .father {
  2. width: 400px;
  3. height: 400px;
  4. position: relative;
  5. }
  6. .child {
  7. position: absolute;
  8. top: 50%;
  9. left: 50%;
  10. transform: translate(-50%, -50%);
  11. }

flex

  1. .father {
  2. width: 400px;
  3. height: 400px;
  4. display: flex;
  5. justify-content: center;
  6. align-items: center;
  7. }
  8. .child {
  9. }

grid

  1. .father {
  2. width: 400px;
  3. height: 400px;
  4. display: grid;
  5. }
  6. .child {
  7. align-self: center;
  8. justify-self: center;
  9. }

实现一个三角形

首先,实现一个这个效果:
image.png

  1. div {
  2. height: 0;
  3. width: 0;
  4. border-top: 100px solid green;
  5. border-left: 100px solid blue;
  6. border-right: 100px solid red;
  7. border-bottom: 100px solid yellow;
  8. }

然后变成三角形,就是颜色问题,将不需要的border的边颜色设置为 transparent 透明。

  1. div {
  2. height: 0;
  3. width: 0;
  4. border-style: solid;
  5. border-width: 100px;
  6. border-color: red transparent transparent transparent;
  7. font-size: 0; // 兼容低版本
  8. line-height: 0; // 兼容低版本
  9. }

image.pngimage.png
可以看到,其实还是占了这么多空间,只是其他部分设置成透明看不见了而已
最后我们指定三角的对立边 border 宽度为0

  1. div {
  2. width: 0;
  3. height: 0;
  4. border-style: solid;
  5. border-width: 0 50px 50px;
  6. border-color: transparent transparent skyblue;
  7. }

image.png
实现一个直角边,只指定邻边border

两栏布局

image.png
image.png

  1. <div class="box">
  2. <div class="left"></div>
  3. <div class="right"></div>
  4. </div>

浮动

  1. .box {
  2. overflow: hidden;
  3. }
  4. .left {
  5. float: left;
  6. width: 200px;
  7. background: gray;
  8. height: 400px;
  9. }
  10. .right {
  11. margin-left: 210px;
  12. background-color: lightgreen;
  13. height: 400px;
  14. }

Flex(推荐)

  1. .box{
  2. display: flex;
  3. }
  4. .left {
  5. width: 100px;
  6. }
  7. .right {
  8. flex: 1;
  9. }

三栏布局

image.png
image.png

  1. <div class="outer">
  2. <div class="left"></div>
  3. <div class="center"></div>
  4. <div class="right"></div>
  5. </div>

绝对定位

左右设置为绝对定位,中间设置margin-left为左边的宽度、margin-right为右边的宽度

  1. .outer {
  2. position: relative;
  3. height: 100px;
  4. }
  5. .left {
  6. position: absolute;
  7. width: 100px;
  8. height: 100px;
  9. background: tomato;
  10. }
  11. .right {
  12. position: absolute;
  13. top: 0;
  14. right: 0;
  15. width: 200px;
  16. height: 100px;
  17. background: gold;
  18. }
  19. .center {
  20. margin-left: 100px;
  21. margin-right: 200px;
  22. height: 100px;
  23. background: lightblue;
  24. }

flex(推荐)

外部设置为 flex,左右设置flex-grow(放大比例)、flex-shrink(收缩比例)为 0,flex-basis为宽度。中间设置flex:auto(== flex: 1 1 auto

  1. .outer {
  2. display: flex;
  3. height: 100px;
  4. }
  5. .left {
  6. flex: 0 0 200px;
  7. background: tomato;
  8. }
  9. .right {
  10. flex: 0 0 100px;
  11. background: gold;
  12. }
  13. .center {
  14. flex: auto;
  15. background: lightblue;
  16. }
  17. 或者
  18. 只需要设置父组件 flex & flex: space-between。然后左右两边设置固定宽度,中间宽度设置100%

Float

左右设置为对应方向的浮动,然后中间设置水平方向的 margin 值。这种方式center盒子必须放在最后

  1. .outer {
  2. height: 100px;
  3. }
  4. .left {
  5. float: left;
  6. width: 100px;
  7. height: 100px;
  8. background: tomato;
  9. }
  10. .right {
  11. float: right;
  12. width: 200px;
  13. height: 100px;
  14. background: gold;
  15. }
  16. .center {
  17. height: 100px;
  18. margin-left: 100px;
  19. margin-right: 200px;
  20. background: lightgreen;
  21. }

圣杯布局

  1. .outer {
  2. height: 100px;
  3. padding-left: 100px;
  4. padding-right: 200px;
  5. }
  6. .left {
  7. position: relative;
  8. left: -100px;
  9. float: left;
  10. margin-left: -100%;
  11. width: 100px;
  12. height: 100px;
  13. background: tomato;
  14. }
  15. .right {
  16. position: relative;
  17. left: 200px;
  18. float: right;
  19. margin-left: -200px;
  20. width: 200px;
  21. height: 100px;
  22. background: gold;
  23. }
  24. .center {
  25. float: left;
  26. width: 100%;
  27. height: 100px;
  28. background: lightgreen;
  29. }

双飞翼布局

  1. <style>
  2. .outer {
  3. height: 100px;
  4. }
  5. .left {
  6. float: left;
  7. margin-left: -100%;
  8. width: 100px;
  9. height: 100px;
  10. background: tomato;
  11. }
  12. .right {
  13. float: left;
  14. margin-left: -200px;
  15. width: 200px;
  16. height: 100px;
  17. background: gold;
  18. }
  19. .wrapper {
  20. float: left;
  21. width: 100%;
  22. height: 100px;
  23. background: lightgreen;
  24. }
  25. .center {
  26. margin-left: 100px;
  27. margin-right: 200px;
  28. height: 100px;
  29. }
  30. </style>
  31. <body>
  32. <div class="outer">
  33. <div class="wrapper">
  34. <div class="center"></div>
  35. </div>
  36. <div class="left"></div>
  37. <div class="right"></div>
  38. </div>
  39. </body>

本质和圣杯布局差不多,只是双飞翼是通过中间的设置margin而不是外部设置padding

Grid布局(推荐)

只需要设置 grid & grid-template-columns: 左宽度 auto 右宽度