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

1,flex布局

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

/*flex布局*/
  .parent{
     display: flex;
     justify-content: space-between;
     align-items: stretch;
   }
   .left{
     background: red;
   }
   .right{
     margin-left: 110px;
     background: blue;
   }

 2.position

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

 

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

 

3、margin负值

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

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

 

posted @ 2021-06-20 16:45  金陵彭于晏  阅读(2321)  评论(0编辑  收藏  举报