三栏布局

浮动布局

  1. <section class="layout float">
  2. <style media="screen">
  3. .layout.float .left {
  4. float: left;
  5. width: 300px;
  6. background: red;
  7. }
  8. .layout.float .center {
  9. background: yellow;
  10. }
  11. .layout.float .right {
  12. float: right;
  13. width: 300px;
  14. background: blue;
  15. }
  16. </style>
  17. <h1>三栏布局</h1>
  18. <article class="left-right-center">
  19. <div class="left"></div>
  20. <div class="right"></div>
  21. <div class="center">
  22. <h2>浮动解决方案</h2>
  23. 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案;
  24. 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案;
  25. 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
  26. </div>
  27. </article>
  28. </section>

绝对布局

  1. <section class="layout absolute">
  2. <style>
  3. .layout.absolute .left-center-right > div {
  4. position: absolute;
  5. }
  6. .layout.absolute .left {
  7. left: 0;
  8. width: 300px;
  9. background: red;
  10. }
  11. .layout.absolute .center {
  12. left: 300px;
  13. right: 300px;
  14. background: yellow;
  15. }
  16. .layout.absolute .right {
  17. right: 0;
  18. width: 300px;
  19. background: blue;
  20. }
  21. </style>
  22. <h1>三栏布局</h1>
  23. <article class="left-center-right">
  24. <div class="left"></div>
  25. <div class="center">
  26. <h2>绝对定位解决方案</h2>
  27. 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案;
  28. 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案;
  29. 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
  30. </div>
  31. <div class="right"></div>
  32. </article>
  33. </section>

flexbox 布局

  1. <section class="layout flexbox">
  2. <style>
  3. .layout.flexbox {
  4. margin-top: 110px;
  5. }
  6. .layout.flexbox .left-center-right {
  7. display: flex;
  8. }
  9. .layout.flexbox .left {
  10. width: 300px;
  11. background: red;
  12. }
  13. .layout.flexbox .center {
  14. flex: 1;
  15. background: yellow;
  16. }
  17. .layout.flexbox .right {
  18. width: 300px;
  19. background: blue;
  20. }
  21. </style>
  22. <h1>三栏布局</h1>
  23. <article class="left-center-right">
  24. <div class="left"></div>
  25. <div class="center">
  26. <h2>flexbox解决方案</h2>
  27. 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案;
  28. 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案;
  29. 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
  30. </div>
  31. <div class="right"></div>
  32. </article>
  33. </section>

表格布局

  1. <section class="layout table">
  2. <style>
  3. .layout.table .left-center-right {
  4. width: 100%;
  5. height: 100px;
  6. display: table;
  7. }
  8. .layout.table .left-center-right > div {
  9. display: table-cell;
  10. }
  11. .layout.table .left {
  12. width: 300px;
  13. background: red;
  14. }
  15. .layout.table .center {
  16. background: yellow;
  17. }
  18. .layout.table .right {
  19. width: 300px;
  20. background: blue;
  21. }
  22. </style>
  23. <h1>三栏布局</h1>
  24. <article class="left-center-right">
  25. <div class="left"></div>
  26. <div class="center">
  27. <h2>表格布局解决方案</h2>
  28. 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案;
  29. 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案;
  30. 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
  31. </div>
  32. <div class="right"></div>
  33. </article>
  34. </section>

网格布局

  1. <section class="layout grid">
  2. <style>
  3. .layout.grid .left-center-right {
  4. width: 100%;
  5. display: grid;
  6. grid-template-rows: 100px;
  7. grid-template-columns: 300px auto 300px;
  8. }
  9. .layout.grid .left-center-right > div {
  10. }
  11. .layout.grid .left {
  12. width: 300px;
  13. background: red;
  14. }
  15. .layout.grid .center {
  16. background: yellow;
  17. }
  18. .layout.grid .right {
  19. background: blue;
  20. }
  21. </style>
  22. <h1>三栏布局</h1>
  23. <article class="left-center-right">
  24. <div class="left"></div>
  25. <div class="center">
  26. <h2>网格布局解决方案</h2>
  27. 1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案;
  28. 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案;
  29. 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
  30. </div>
  31. <div class="right"></div>
  32. </article>
  33. </section>

未知宽高元素水平垂直居中

  1. <div class="father">
  2. <div class="son"></div>
  3. </div>
  4. //第一种
  5. <style>
  6. .father {
  7. position: relative;
  8. width: 500px;
  9. height: 500px;
  10. background-color: red;
  11. }
  12. .son {
  13. position: absolute;
  14. margin: auto;
  15. top: 0;
  16. left: 0;
  17. bottom: 0;
  18. right: 0;
  19. width: 300px;
  20. height: 300px;
  21. background-color: blue;
  22. }
  23. </style>
  24. //第二种
  25. <style>
  26. .father {
  27. position: relative;
  28. }
  29. .son {
  30. margin: auto;
  31. position: absolute;
  32. top: 50%;
  33. left: 50%;
  34. transform: translate(-50%, -50%);
  35. }
  36. </style>
  37. //第三种
  38. <style>
  39. .father {
  40. display: -webkit-flex;
  41. align-items: center;
  42. justify-content: center;
  43. }
  44. .son {
  45. margin: auto;
  46. }
  47. </style>
  48. //第四种,用表格布局
  49. <div class="pe">
  50. <div class="he">
  51. <div class="she"></div>
  52. </div>
  53. </div>
  54. <style>
  55. .pe {
  56. display: table;
  57. width: 50%;
  58. height: 30%;
  59. position: absolute;
  60. }
  61. .he {
  62. display: table-cell;
  63. vertical-align: middle;
  64. text-align: center;
  65. }
  66. .she {
  67. width: 50%;
  68. height: 30%;
  69. display: inline-block;
  70. }
  71. </style>
  72. //第五种,行内块元素
  73. <div class="wrap">
  74. <b class="vamp"></b>
  75. <div class="test">行内块元素</div>
  76. <div>
  77. <style>
  78. .vamp{
  79. width:0
  80. height:100%;
  81. vertical-align:middle;
  82. }
  83. .test{
  84. background:"blue";
  85. display:inline-block;
  86. }
  87. </style>
  88. </div>
  89. </div>