Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS-only: How can I center fixed width div between two divs that fill the remaining space?

JSFIDDLE

EDIT: Trying to achieve the opposite of this.
(instead of 2 fixed-width divs on the sides and one fluid-width div in the middle,
I'm trying to get 2 fluid-width divs on the sides and one div centered in the middle)

I have 3 divs: A, B, and C.

B needs to sit centered between A and C.

What I need to do

What I'm currently doing is pretty much matching what's going on above. But, if A, B, and C's container has an odd width, some browsers are rounding A and C's widths down and others up (leaving C 1px too long and 1px too short, respectively).

notice the extra pixel to the right of C

notice the space to the right of C is a pixel thinner.

I don't care how many nested divs I need, but I've been spending way too much time on this! If someone already has a sure-fire solution to this problem, please share!

notes:
- A, B, and C's parent can't have overflow hidden.
- A, B, and C can't overlap (they have translucent png's)

This is my starting point: JSFIDDLE

like image 686
MicronXD Avatar asked May 15 '12 02:05

MicronXD


People also ask

How do I fill a div with remaining space?

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 keep two side by side div elements the same width?

The two or more different div of same height can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format.


1 Answers

Here are two ways that work, both with caveats (warning, both require a wrapper):

HTML

<section class="wrapper">
    <div class="column" id="a"></div>
    <div class="column" id="b"></div>
    <div class="column" id="c"></div>
</section>​​​​​​​​​​​

Base CSS

.column {
    height: 3em;
}

#a {
 background-color: red;   
}
#b {
 background-color: green;
}
#c {
 background-color: blue;   
}

#b {
  width: 50px;   
}

CSS3 Approach:

.wrapper {
    display:box;
    box-orient:horizontal;
    box-align:stretch;    
}

#a, #b {
    box-flex:1.0;
}

Caveat: Only supported (so far) in Firefox and Webkit (Chrome/Safari), both requiring prefixed rules. The above is what it will be someday.

Here is a demo with prefixes: http://jsfiddle.net/crazytonyi/cBVTE/

Table-Display approach

.wrapper {
    display: table;
    width: 100%;
    table-layout: fixed;

}    
.column {
    display: table-cell;
}

Caveats: IE didn't start supporting this display type until 7 or 8. On the other hand, worrying about users on older versions of IE is like worrying about people who still turn off cookies and javascript. Either they catch up or get used to pages breaking. End the pandering!

Here's a demo using the above: http://jsfiddle.net/crazytonyi/kM46h/

In both cases, no floats or positioning needed, just willingness to give the middle finger to older browsers. How old depends on which method you choose.

like image 96
Anthony Avatar answered Oct 11 '22 17:10

Anthony