0

How do I make the top div of the full horizontal length of the main container, while keeping the next two div, .left and .right in flex to each other?

To look like this - enter image description here

.main {
  border: 1px solid red;
  display: inline-flex;
}

.main div.top {
  border: 1px solid orange;
  width: auto;
  display: inline-block;
}

.main div.left {
  border: 1px solid blue;
}

.main div.right {
  border: 1px solid green;
}
<html>

<body>
  <div class="main">
    <div class="top">
      <h1>top</h1>
    </div>

    <div class="left">
      <h1>left</h1>
    </div>

    <div class="right">
      <h1>right</h1>
    </div>
  </div>
</body>

</html>
Johannes
  • 64,305
  • 18
  • 73
  • 130
Majoris
  • 2,963
  • 6
  • 47
  • 81

5 Answers5

1

Another way to do this is with grid:

.main  {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.main  .top {
  grid-column: 1/3;
}

.main  {
  border: 1px solid red;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 4px;
  padding: 4px;
}

.main  .top {
  border: 1px solid orange;
  grid-column: 1/3;
}

.main  .left {
  border: 1px solid blue;
}

.main .right {
  border: 1px solid green; 
}
<div class="main">
  <div class="top">   
    <h1>top</h1>
  </div>
  <div class="left">    
    <h1>left</h1>
  </div>
  <div class="right">    
    <h1>right</h1>
  </div>   
</div>
symlink
  • 11,984
  • 7
  • 29
  • 50
1

Use display: flex; and flex-wrap: wrap on the parent container, 100% width on the first child and flex-grow: 1 on the other children, or also use percentage widths on the second and third DIVs.

* {
box-sizing: border-box;
}
.main  {
  border: 1px solid red;
  display: flex;
  flex-wrap: wrap;
}

.main  div.top {
  border: 1px solid orange;
  width:100%;
}

.main  div.left {
  border: 1px solid blue;
  width: 40%;
}

.main div.right {
  border: 1px solid green; 
  width: 60%;
}
<html>
<body>
    <div class="main">
<div class="top">   <h1>top</h1>
   </div>
      
 <div class="left">    <h1>left</h1>
</div>

<div class="right">    <h1>right</h1>
</div>
     
    </div>
</body>
</html>
Johannes
  • 64,305
  • 18
  • 73
  • 130
1

Any width of .left and .right

.main {
      border: 1px solid red;
      display: flex;
      flex-wrap: wrap;
    }
    
    .main div.top {
      border: 1px solid orange;
      width: auto;
      display: inline-block;
      flex: 1 1 100%;
    }
    
    .main div.left {
      border: 1px solid blue;
      flex: 1 1 auto;
    }
    
    .main div.right {
      border: 1px solid green;
      flex: 1 1 auto;
    }
    <div class="main">
      <div class="top">
        <h1>top</h1>
      </div>
  
      <div class="left">
        <h1>left 11111111</h1>
      </div>
  
      <div class="right">
        <h1>right</h1>
      </div>
    </div>
focus.style
  • 6,612
  • 4
  • 26
  • 38
0

You can two div to create additional sections.

.main  {
  border: 1px solid red;
  display: flex;
flex-direction:column;
}

.main  div.top {
  border: 1px solid orange;
  width:auto; 
}

.main  div.left {
  border: 1px solid blue;
  flex:1
}

.main div.right {
  border: 1px solid green;
  flex:1 
}
   .main__section2 {
display:flex
   }
<html>
<body>
  <div class="main">
    <div class="main__section1">
      <div class="top">
        <h1>top</h1>
      </div>
    </div>
    <div class="main__section2">
      <div class="left">
        <h1>left</h1>
      </div>
      <div class="right">
        <h1>right</h1>
      </div>
    </div>
  </div>
</body>
</html>
Utsav Patel
  • 2,789
  • 1
  • 16
  • 26
0

You can try something like this:

I just added 2 extra divs if that is not an issue?

#MainDiv {
  width: 200px;
  border: 1px solid red;
}

.main {
  width: auto;
  display: flex;
  flex-direction: column;
}

#lower {
  display: flex;
  flex-direction: row;
}

.left,
.right {
  width: 100px;
  border: 1px solid black;
}
<html>

<body>
  <div id="MainDiv">
    <div class="main">
      <div class="top">
        <h1>top</h1>
      </div>
      <div id="lower">
        <div class="left">
          <h1>left</h1>
        </div>

        <div class="right">
          <h1>right</h1>
        </div>
      </div>


    </div>
  </div>

</body>

</html>
Manjuboyz
  • 6,978
  • 3
  • 21
  • 43