How to make a left floating div for 100% of the remaining space?

If I have a container with a width of 75% and two columns inside, left and right, and left - 10 m wide, how can I get the right container to occupy 100% of the remaining space?

I tried this with no luck:

html, body {
    margin:0;
    padding:0;
    width:100%;
    height:100%;
}
#container {
    position:relative;
    width:75%;
    margin:0 auto;
    background:blue;
}
#left-container {
    position:relative;
    float:left;
    height:100%;
    width:10em;
    background:red;
}
#right-container {
    position:relative;
    float:left;
    height:100%;
    width:100%;
    background:yellow;
}
<div id="container">
    <div id="left-container">Left</div>
    <div id="right-container">Right</div>
</div>
Run code

I can do it easily with interest, but I need to leave a fixed width of 10 m.

+5
source share
4 answers

You can make a box element that takes up the rest of the space by deleting float: left, deleting, widthand adding overflow:hiddento the rightdiv

Working example

#right-container {
    position:relative;
    overflow: hidden;
    height:100%;
    background:yellow;
}
+9
source

Another option is to put the left margin in the div #right-container:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

<style media="all">
html, body {
    margin:0;
    padding:0;
    width:100%;
    height:100%;
}
#container {
    position:relative;
    width:75%;
    margin:0 auto;
    background:blue;
}
#left-container {
    position:relative;
    float:left;
    height:100%;
    width:10em;
    background:red;
}
#right-container {
    position:relative;
    margin-left: 11em;
    height:100%;
    background:yellow;
}
</style>

</head>
<body>

<div id="container">
    <div id="left-container">
        Left
    </div>
    <div id="right-container">
        Right
    </div>
</div>

</body>
</html>
+1
source

(2016) 2 CSS3, , , "" overflow:hidden

  • flexbox

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}
#container {
  width: 75%;
  margin: 0 auto;
  background: blue;
  display: flex
}
#left-container {
  height: 100%;
  width: 10em;
  background: red;
}
#right-container {
  height: 100%;
  flex:1;
  background: yellow;
}
<div id="container">
  <div id="left-container">Left</div>
  <div id="right-container">Right</div>
</div>

  • calc()

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}
#container {
  width: 75%;
  margin: 0 auto;
  background: blue;
}
#left-container {
  float: left;
  height: 100%;
  width: 10em;
  background: red;
}
#right-container {
  float: left;
  height: 100%;
  width: calc(100% - 10em);
  background: yellow;
}
<div id="container">
  <div id="left-container">Left</div>
  <div id="right-container">Right</div>
</div>
+1

I would like to add another option as shown below:

#header-left-section {
    float: left;
    position: absolute;
    z-index: 1000;
}
#header-right-section {
    height: 90px;
    width:100%;
    position: relative;
    overflow: hidden;
}
#header-right-section ins,
#header-right-section div{float:right}

The left div just takes up enough space for its inner stuff. The right side is 100% of the width, and the left side remains above it only on the left (by z-index). The last line of css is just to make the inner stuff if the right div will float to the right.

0
source

All Articles