david2tdw / blog

学习记录
1 stars 1 forks source link

[css] 多种方案实现单列等宽,其他多列自适应均匀布局 #129

Open david2tdw opened 4 years ago

david2tdw commented 4 years ago

实现

  1. display: grid 实现
  2. display: flex + calc实现
  3. display: flex + 非calc实现
  4. position: aboslute + float 实现
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>多种方案实现单列等宽,其他多列自适应均匀布局</title>
    <style>
      h2 {
        font-size: 28px;
        text-align: center;
        color: #fff;
        background-color: #009688;
        line-height: 2; /* 2被font-size的高度 */
      }
      .g-grid {
        display: grid;
        grid-template-columns: 200px 1fr 1fr 1fr; /* 没有逗号 */
        /* grid-template-columns: 200px repeat(3, 1fr); */ /* 或者这种写法 */
        grid-column-gap: 10px;
        height: 200px;
      }
      .g-grid .g-item {
        line-height: 200px;
        background: #3f51b5;
        text-align: center;
      }

      .g-flex {
        display: flex;
        justify-content: space-between; /* 让间隙出现在间隔处 */
      }
      .g-flex .f-item {
        /* 中间没有逗号 */
        /* flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。 */
        /* flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。 */
        /* flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。 */
        /* 通过calc() 空出间距 */
        flex: 0 1 calc((100% - 200px - 30px) / 3);
        line-height: 200px;
        background: #3f51b5;
        text-align: center;
      }
      .g-flex .f-item:first-child {
        flex: 0 1 200px;
      }
      .g-flex1 {
        display: flex;
      }
      .g-flex1 .f1-item {
        flex: 1;
        line-height: 200px;
        background: #3f51b5;
        text-align: center;
        margin-left: 10px;
      }
      .g-flex1 .f1-item:first-child {
        flex: 0 1 200px;
        margin-left: unset;
      }
      .g-position {
        height: 200px;
      }
      .g-position .p-item {
        float: left;
        width: calc((100% - 200px - 30px) / 3); /* 宽度 100% - 200px - 间隙30px */
        text-align: center;
        background: #3f51b5;
        line-height: 200px;
        margin-left: 10px; /* 空出元素左侧的间距 */
      }
      .g-position .p-item:first-child {
        width: 200px;
        margin-left: unset; /* 第一个元素左侧不需要margin */
      }
    </style>
  </head>
  <body>
    <!--要求,共4列,首列宽度固定为200px,其余3列均分剩余宽度,每列间距10px-->
    <h2>display: grid 实现, 宽度自适应, 间隙grid-column-gap</h2>
    <div class="g-grid">
      <div class="g-item">1</div>
      <div class="g-item">2</div>
      <div class="g-item">3</div>
      <div class="g-item">4</div>
    </div>
    <h2>display: flex 实现, 宽度calc(), 间隙space-between</h2>
    <div class="g-flex">
      <div class="f-item">1</div>
      <div class="f-item">2</div>
      <div class="f-item">3</div>
      <div class="f-item">4</div>
    </div>
    <h2>display: flex 实现, 宽度自适应, 间隙margin-left</h2>
    <div class="g-flex1">
      <div class="f1-item">1</div>
      <div class="f1-item">2</div>
      <div class="f1-item">3</div>
      <div class="f1-item">4</div>
    </div>
    <h2>position: aboslute + float 实现, 宽度calc(), 间隙margin-left</h2>
    <div class="g-position">
      <div class="p-item">1</div>
      <div class="p-item">2</div>
      <div class="p-item">3</div>
      <div class="p-item">4</div>
    </div>
  </body>
</html>

多种方案实现单列等宽,其他多列自适应均匀布局

david2tdw commented 4 years ago

unset: 去掉默认样式。如果可以继承样式,则继承父元素的样式,如果不能继承,则用默认样式。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .father {
        color: red; /* 可继承属性 */
        border: 1px solid green; /* 不可继承属性 */
        padding: 20px;
      }
      .children {
        color: green;
        border: 1px solid blue;
      }
      .unset {
        color: unset; /* 继承父类 */
        border: unset; /* 初始状态 */
      }
    </style>
  </head>
  <body>
    father: 文字红色,边框绿色。
    <div class="father">
      <div class="children">child 属性为非unset时。</div>
      <div class="children unset">
        child属性为unset时。color:inherit, 继承父元素,红色。 border: initial, 默认 border 样式,无边框.
      </div>
    </div>
  </body>
</html>

css-----unset