常用 CSS 布局

一、 垂直水平居中

    1. 定位方式实现

    设置 父元素 样式

    position: relative;
    

    设置 子元素元素 样式

    width: 200px;
    height: 200px;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    
    1. flex 布局实现

    设置父元素即可

    display: flex;
    justify-content: center;
    align-items: center;
    

二、 两列布局 (一边定宽一边自适应)

1. float + margin 方式实现

    1. HTML
    <div class="left">定宽</div>
    <div class="right">自适应</div>
    
    1. CSS
    .left {
      float: left;
      width: 200px;
      height: 300px;
      line-height: 300px;
      text-align: center;
      background: red;
      color: #fff;
    }
    .right {
      margin-left: 210px;
      height: 300px;
      background: yellow;
      text-align: center;
      line-height: 300px;
    }
    

2. 定位方式实现

    1. HTML
    <div class="box">
      <div class="left">定宽</div>
      <div class="right">自适应</div>
    </div>
    
    1. CSS
    .box {
      position: relative;
    }
    .left {
      position: absolute;
      width: 200px;
      height: 300px;
      line-height: 300px;
      text-align: center;
      background: yellow;
    }
    .right {
      width: 100%;
      height: 300px;
      background: red;
      text-align: center;
      line-height: 300px;
    }
    

2. flex 布局实现

    1. HTML
    <div class="box">
      <div class="left">定宽</div>
      <div class="right">自适应</div>
    </div>
    
    1. CSS
    .box {
      display: flex;
      flex-direction: row;
      text-align: center;
      line-height: 300px;
    }
    .left {
      width: 200px;
      background: red;
    }
    .right {
      flex: 1;
      width: 200px;
      background: yellow;
    }
    
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容