20

一.浮动布局

1.先让固定宽度的div浮动!使其脱离文档流。
2.margin-left的值等于固定div的宽度相等。

   .aside{
        float: left;
        width: 200px;
        background-color: red;
    }
    .content{
        margin-left: 200px;
        background-color: blue;
    }
    
<div class="aside">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.
</div>
<div class="content">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.
</div>

二.margin的负值(3个div)

  1. 是固定宽度的div脱离文档流。
  2. 利用marin负值可以使得,后面的div可以与前面的div 保持同行显示。
  3. 给包裹内容的div加margin-left 可以使得与左边的文字不重叠

    .aside{
        float: left;
        margin-right: -200px;
        width: 200px;
        background-color: red;
    }
    .content{
        float: right;
    }
    .content .inner{
        margin-left: 200px;
        background-color: blue;
    }
    
     <div class="aside">
       Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.
    </div>
    <div class="content">
       <div class="inner">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.
       </div> 

    </div>

三.calc()计算属性

注意:使用calc计算属性的时候 运算符(- +等等)两边必须有空格

  1. 注意两个div必须一左一右浮动。
  2. calc的宽度必须要减去的宽度要与固定宽度保持一致。

      .aside{
          float: left;
          width: 200px;
          background-color: red;
      }
      .content{
          calc:(100% - 200px);
          background-color: blue;
      }

    <div class="aside">

      Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.
       </div>
     <div class="content">
         Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.        
      </div>
    

四.flex布局

  1. 需要给父级div设置display: flex属性。
  2. 固定宽度的div设置flex: 0 0 200px即可。
  3. 内容区域的div直接写出flex: 1即可。

    .container{
        display: flex;
    }
    .aside{
        flex: 0 0 200px;
        background-color: red; 
    }
    .content{
        flex: 1;
        background-color: blue;
    }
    <div class="container">
        <div class="aside">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.
        </div>
        <div class="content">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.
        </div>
    </div>
    

五.左侧浮动,右侧overflow:hidden

.aside{
    width: 200px;
    float: left;
    background-color: red;
}
.content{
    overflow: hidden;
    background-color: blue;
}
<div class="aside">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.
    </div>
    <div class="content">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore provident dolor in voluptatum tempore non, placeat obcaecati totam ut delectus libero excepturi distinctio asperiores odit nobis soluta, esse aspernatur beatae.
    </div>

阮一峰老师写的教程权威传送门:http://www.ruanyifeng.com/blo...


李钢铁
830 声望47 粉丝

简单粗暴