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

后端 未结 22 2738
自闭症患者
自闭症患者 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:18

    Using CSS Flexbox and min-height worked for me

    Say you have a container with two divs inside and you want those two divs to have the same height.

    You would set 'display: flex' on the container as well as 'align-items: stretch'

    Then just give the child divs a 'min-height' of 100%

    See the code below

    .container {
      width: 100%;
      background: linear-gradient(red,blue);
      padding: 1em;
      /* important */
      display: flex;
      /* important */
      align-items: stretch;
      justify-content: space-around;
    }
    
    .child {
      width: 100%;
      background: white;
      color: grey;
      margin: 0 .5em;
      padding: .5em;
      /* important */
      min-height: 100%;
    }

    This is some text to fill the paragraph

    This is a lot of text to show you that the other div will stretch to the same height as this one even though they do not have the same amount of text inside them. If you remove text from this div, it will shrink and so will the other div.

提交回复
热议问题