Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two floated divs with a flexible div in-between

I need the following in a header of fixed width:

  1. A div of varying width floated left.
  2. A div of varying width floated right.
  3. An h2 centered between them that takes up any remaining space.

The floated divs contain content that may vary in size.

I've tried various approaches but they have all failed. I know one solution is to absolutely position the outer divs, then stretch the h2 out for the full width and center the text so it sits centrally, but there must be a nicer way to do this.

like image 334
Undistraction Avatar asked Aug 09 '12 18:08

Undistraction


People also ask

How do I flex two divs?

Answer: Use the CSS3 flexbox With CSS3 flex layout model you can very easily create the equal height columns or <div> elements that are aligned side by side. Just apply the display property with the value flex on the container element and the flex property with the value 1 on child elements.

How do I center a div between two divs?

Set the margin property to "auto" to horizontally center the <div> element within the page. The "margin: 0 auto" does the actual centering. Set your preferred colors for the outer and inner <div> elements by using the background-color property.

How do you float divs next to each other?

With CSS properties, you can easily put two <div> next to each other in HTML. Use the CSS property float to achieve this. With that, add height:100px and set margin.

How do you float 3 divs side by side?

Three or more different div 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. float:left; This property is used for those elements(div) that will float on left side.


1 Answers

A basic jsFiddle example with minimal markup.

HTML

<div id="container">
    <div id="left">left</div>
    <div id="right">right</div>
    <h2>H2</h2>
</div>​

CSS

#container {
    border:1px solid #999;
}
#left {
    float:left;
}
#right {
    float:right;
}
h2 {
    text-align:center;
    margin:0;
} 

like image 126
j08691 Avatar answered Sep 28 '22 10:09

j08691