How do I keep two side-by-side divs the same height?

后端 未结 22 2759
自闭症患者
自闭症患者 2020-11-21 07:56

I have two divs side by side. I\'d like the height of them to be the same, and stay the same if one of them resizes. I can\'t figure this one out though. Ideas?

To c

22条回答
  •  余生分开走
    2020-11-21 08:17

    I like to use pseudo elements to achieve this. You can use it as background of the content and let them fill the space.

    With these approach you can set margins between columns, borders, etc.

    .wrapper{
      position: relative;
      width: 200px;
    }
    .wrapper:before,
    .wrapper:after{
      content: "";
      display: block;
      height: 100%;
      width: 40%;
      border: 2px solid blue;
      position: absolute;
      top: 0;
    }
    .wrapper:before{
      left: 0;
      background-color: red;
    }
    .wrapper:after{
      right: 0;
      background-color: green;
    }
    
    .div1, .div2{
      width: 40%;
      display: inline-block;
      position: relative;
      z-index: 1;
    }
    .div1{
      margin-right: 20%;
    }
    Content Content Content Content Content Content Content Content Content
    Other

提交回复
热议问题