css 左侧跟随右侧高度自适应

一、position

给父元素设置position:relative,左边的子元素设置position:absulote,且左边元素的高度为100%。CSS代码以下:css

/*position*/
.left{
  height: 100%;
  width: 100px;
  background: aqua;
  position: absolute;
}
.right{
  width: 300px;
  margin-left: 110px;
  background: antiquewhite;
}
.parent{
  position: relative;
}

  

二、margin负值

这种方法的原理实际上是把子元素的实际高度撑开的不少,父元素overflow:hidden起到一个遮罩做用,来保持左右两侧元素高度相等的。css代码以下web

 /*margin负值*/
.parent{
  overflow: hidden;
}
.left,.right{
  margin-bottom: -5000px;
  padding-bottom: 5000px;
}
.left{
  float: left;
  background: aqua;
}
.right{
  float: right;
  background: antiquewhite;
}

  

三、flex布局

flex布局的中align-items的stretch属性能够让内部元素高度铺满。CSS代码以下:布局

 /*flex布局*/
    .parent{
      display: flex;
      display: -webkit-flex;
      display: -o-flex;
      display: -moz-flex;
      display: -ms-flex;
      align-items: stretch;
    }
    .left{
      background: aqua;
    }
    .right{
      margin-left: 110px;
      background: antiquewhite;
    }

  

本文摘自:https://www.jianshu.com/p/8dc3c4976140flex

相关文章
相关标签/搜索