2

I have a box that has 3 divs in it. I made a picture below, the two outside divs I have set widths that I need them to be but the middle div I want to be fluid and fill to what ever the remaining width is.

The code for this will be used on different pages that have different width's so I would like the middle to always adjust based on to fill the remaining width.

enter image description here

3
  • 1
    possible duplicate of CSS 3 Column float (2 fixed, 1 dynamic) Commented Apr 15, 2014 at 1:24
  • That works for Div number 3 being dynamic not the middle div so that doesn't help but thank you. Commented Apr 15, 2014 at 1:28
  • Possible duplication of this question [stackoverflow.com/questions/7292021/… please check the answer Commented Apr 15, 2014 at 1:40

3 Answers 3

2

The way to do this with out breaking a line is to use display: table-cell. To assure the spacing will work properly you should wrap the divs in a container and set a max-width on the container. Then find the remaining width of the middle box: 65+185 = 250. 800 (my max-width example) - 250 = 550. 550/800 = 68.75%. Set that percentage as the middle box and it will be completely fluid. Box 3 won't break to the next line no matter how small the browser gets.

FIDDLE

CSS

.container{ max-width: 800px } .box1{ width: 65px; height: 50px; background: black; display: table-cell; vertical-align: top; } .box2{ width: 68.75%; height: 50px; background: green; display: table-cell; vertical-align: top; } .box3{ width: 185px; height: 50px; background: yellow; display: table-cell; vertical-align: top; } 

HTML

<div class="container"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but the max width is going to be changing depending on which page I send this to which is what I'm trying to figure out, how do I do this when the max width of the overall container is going to change?
look into css calc() but that's not fully supported yet so I tend to stay away from it. Your best bet is to use javascript/jquery
0

Possible solution:

This is the css

main { width:100% } left { display:inline-block; width: 65px; height: 291px; background-color:#0000ff; } middle { position:absolute; display:inline-block; background-color:#ffff00; height: 291px; margin-right:185px } right { float:right; height: 291px; background-color: #ff0000; width: 185px; } 

And the html:

<div class="main"> <div class="left"></div> <div class="middle"> blablabla </div> <div class="right"></div> </div> 

You can find a working sample here: http://jsfiddle.net/mLJLr/1/

Comments

0

Use this css:

 #left { float:left; width:65px; background-color:#ff0000; } #center { float:left; width:100%; max-width: initial; background-color:#00AA00; } #right { float:right; width: 185px; background-color:#00FF00; } 

And this Html:

 <div id="center"> <div id="left"> left </div> center <div id="right"> right </div> </div> 

Test Online: http://jsfiddle.net/9PFPm/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.