Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two divs side by side inside another div

Tags:

html

css

I have the following simple html code.

.body-content {
    width: 100%;
    padding-left: 15px;
    padding-right: 15px;
}

.left-menu {
    background-color: red;
    float: left;
    width: 50px;
}

.right-container {
    background-color: blue;
    width: 100%;
}

.middle-view {
    width: 70%;
    float: left;
    background-color: blueviolet;
}

.right-view {
    width: 30%;
    float: left;
    background-color: burlywood;
}
  <div class="body-content">
  <div class="left-menu">
    abc
  </div>
  <div class="right-container">
    <div class="middle-view">
      def
    </div>
    <div class="right-view">
      ghi
    </div>
  </div>
</div>

I am getting the following result: enter image description here

But I would like to 'def' and 'ghi' side by side.

I don't have much experience using HTML and CSS but I thought middle-view and right-view together will fill right-container (70% + 30%). But as I see the width given to left-menu (50px) has impact on it.

like image 404
Ashot Khachatryan Avatar asked Mar 10 '23 21:03

Ashot Khachatryan


2 Answers

Here is the solution..

.body-content {
    width: 100%;
    padding-left: 15px;
    padding-right: 15px;
  float:left;
}

.left-menu {
    background-color: red;
    float: left;
    width: 50px;
}

.right-container {
    background-color: blue;
    width:calc(100% - 50px);
  float:left;
}

.middle-view {
    width: 70%;
    float: left;
    background-color: blueviolet;
}

.right-view {
    width: 30%;
    float: left;
    background-color: burlywood;
}
<div class="body-content">
  <div class="left-menu">
    abc
  </div>
  <div class="right-container">
    <div class="middle-view">
      def
    </div>
    <div class="right-view">
      ghi
    </div>
  </div>
</div>
like image 91
Sahil Dhir Avatar answered Mar 12 '23 12:03

Sahil Dhir


.body-content {
  display: flex;              /* forces children to same row */
  padding-left: 15px;
  padding-right: 15px;
}
.left-menu {
  width: 50px;
  background-color: red;
}
.right-container {
  display: flex;              /* forces children to same row */
  flex: 1;                    /* consume remaining space on the row */
  background-color: blue;
}
.middle-view {
  width: 70%;
  background-color: blueviolet;
}
.right-view {
  width: 30%;
  background-color: burlywood;
}
<div class="body-content">
  <div class="left-menu">abc</div>
  <div class="right-container">
    <div class="middle-view">def</div>
    <div class="right-view">ghi</div>
  </div>
</div>
like image 44
Michael Benjamin Avatar answered Mar 12 '23 10:03

Michael Benjamin