Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - 3 div's , 1 with fixed size, others fill space

Tags:

html

css

I have 3 divs on my webpage horizontally next to eachother. The middle one is of fixed size. I want the others to fill up the remaining space on the page.

I made this snippet which is a simplified version of my problem:

#left {
  background: red;
  float: left;
}
#middle {
  background: blue;
  margin-left: auto;
  margin-right: auto;
  width: 500px;
}
#right {
  background: green;
  float: right;
}
<div id='right'>groen</div>
<div id='left'>rood</div>
<div id='middle'>fixed px blauw</div>

I can't use percentages, because of the middle div having fixed size.

left and right div should each have a width of (100%-1170px)/2

Is there an easy way to make css fill up the extra space? Or if this isn't an option is there a way to do it programmatically?

like image 914
user2800581 Avatar asked Dec 24 '14 14:12

user2800581


People also ask

How do you set a fixed size in CSS?

To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.

How do I fill available space in CSS?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do I make a div fill remaining vertical space?

Another way of making a <div> fill the remaining space is to use the CSS position property. Just set the position to “absolute” to stretch the <div>.


1 Answers

Demo - http://jsfiddle.net/mpsrcmgg/1/

You can use table layout display:table for parent and display:table-cell for children but you will need to change the html markup

.cont {
  display: table;
  width: 100%;
}
.cont div {
  display: table-cell;
}
#left {
  background: red;
}
#middle {
  background: blue;
  margin-left: auto;
  margin-right: auto;
  width: 500px;
}
#right {
  background: green;
}
<div class="cont">
  <div id='right'>groen</div>
  <div id='middle'>fixed px blauw</div>
  <div id='left'>rood</div>
</div>
like image 123
Vitorino fernandes Avatar answered Oct 12 '22 12:10

Vitorino fernandes