How to Make a Div Fill the Remaining Width

Have you ever needed to make a <div> element fill the remaining width of the container? For this, you can use the float property.

In this snippet, we’re going to demonstrate some ways of making a <div> expand to fill the remaining width.

First, we’ll show the steps needed and then you can see the full example.

Create HTML

  • Use a <body> element.
  • Add a <div> element and place two other <div> elements with id attributes within it.
<body>
  <div id="container">
    <div id="left">
      div 1
    </div>
    <div id="right">
      div 2
    </div>
  </div>
</body>

Add CSS

  • Set the text-align property to “center” for the <body> element.
  • Set the height, border, font-size, font-weight, and color properties for the “container”.
  • Set the float property to “left” and the height to 100% for the “left”. Also, specify the width and add the background-color property.
  • Set the width and height to 100% for the "right" and specify the background-color property.
body {
  text-align: center;
}

#container {
  height: 80px;
  border: 1px solid #000;
  font-size: 20px;
  font-weight: bold;
  color: #fff;
}

#left {
  float: left;
  width: 200px;
  height: 100%;
  background-color: #17ad37;
}

#right {
  width: 100%;
  height: 100%;
  background-color: #969696;
}

Now, you can see the full code.

Example of making a <div> element fill the remaining width:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      body {
        text-align: center;
      }
      #container {
        height: 80px;
        border: 1px solid #000;
        font-size: 20px;
        font-weight: bold;
        color: #fff;
      }
      #left {
        float: left;
        width: 200px;
        height: 100%;
        background-color: #17ad37;
      }
      #right {
        width: 100%;
        height: 100%;
        background-color: #969696;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <div id="left">
        div 1
      </div>
      <div id="right">
        div 2
      </div>
    </div>
  </body>
</html>

Result

div 1

Let’s see other examples, where we also use the overflow property set to “hidden” for the “left” class of the <div>.

Example of making a <div> fill the remaining width from the left:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .container {
        width: 500px;
        height: 100px;
        border: 1px solid #000;
      }
      .left {
        width: auto;
        height: 100px;
        background: #d1d1d1;
        overflow: hidden;
      }
      .right {
        height: 100px;
        width: 100px;
        background: #d69292;
        float: left;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="right"></div>
      <div class="left"></div>
    </div>
  </body>
</html>

Example of making the <div> fill the remaining width from the right:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .container {
        width: 500px;
        height: 100px;
        border: 1px solid #000;
      }
      .left {
        width: auto;
        height: 100px;
        background: #d1d1d1;
        overflow: hidden;
      }
      .right {
        height: 100px;
        width: 100px;
        background: #d69292;
        float: right;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="right"></div>
      <div class="left"></div>
    </div>
  </body>
</html>