Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML, flex, two divs in a row, force one div to have size of smaller [duplicate]

Tags:

html

css

flexbox

Subj.

I have two divs (left and right). And I want to make left div something like main one, and force right div to have the same height as a left one, even if it will have more content (and do overflow: hidden). I will appreciate if there is a way to make it with css and html without js.

Here is a little snippet, how my markup is looking:

.parent {
  display: flex;
  flex-direction: row;
  align-items: stretch;
}

.children-1 {
  width: 66.6%;
  background: green;
}

.children-2 {
  width: 33.4%;
  background: yellow;
}
<div class="parent">
  <div class="children-1">
    <p>My content</p>
  </div>
  
  <div class="children-2">
    <p>a lot of content<p>
    <p>a lot of content</p>
  </div>
</div>
like image 703
Andrey Drozdov Avatar asked Dec 04 '17 23:12

Andrey Drozdov


1 Answers

Apply a fixed height to the container and use overflow-y: hidden; on the second child DIV:

.parent {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  height: 200px;
}

.children-1 {
  width: 66.6%;
  background: green;
}

.children-2 {
  width: 33.4%;
  background: yellow;
  overflow-y: hidden;
}
<div class="parent">
  <div class="children-1">
    <p>My content</p>
  </div>
  
  <div class="children-2">
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
  </div>
</div>

Here's a second version without fixed height, but the HTML structure is changed: There is only one child (the right one), and the contents which were in the first child are now in the parent. It uses absolute positioning for that child element and hides the overflow of the child element - see the actual settings below:

html,
body {
  margin: 0;
}

.parent {
  position: relative;
  padding: 1em;
  background: green;
  overflow-y: hidden;
}

.children-2 {
  width: 33.4%;
  position: absolute;
  right: 0;
  top: 0;
  background: yellow;
}
<div class="parent">
  <p>My content</p>
  <p>My content</p>

  <div class="children-2">
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
    <p>a lot of content</p>
  </div>

</div>
like image 63
Johannes Avatar answered Nov 01 '22 16:11

Johannes