Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div width 100% minus fixed amount of pixels

How can I achieve the following structure without using tables or JavaScript? The white borders represent edges of divs and aren't relevant to the question.

Structure 1

The size of the area in the middle is going to vary, but it will have exact pixel values and the whole structure should scale according to those values. To simplify it, I'd need a way to set "100% - n px" width to the top-middle and bottom-middle divs.

I'd appreciate a clean cross-browser solution, but in case it's not possible, CSS hacks will do.

Here's a bonus. Another structure I've been struggling with and end up using tables or JavaScript. It's slightly different, but introduces new problems. I've been mainly using it in jQuery-based windowing system, but I'd like to keep the layout out of the script and only control the size of one element (the middle one).

Structure 2

like image 503
Removed_account Avatar asked Mar 16 '09 17:03

Removed_account


People also ask

Does width 100% include padding?

The width and height properties include the content, but does not include the padding, border, or margin.

Is a div 100% width by default?

auto automatically computes the width such that the total width of the div fits the parent, but setting 100% will force the content alone to 100%, meaning the padding etc. will stick out of the div, making it larger than the parent. so setting the 'width' to 'auto' would be better? Yes, but that's the default anyway.

How do you make a div fixed width?

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.

What is the meaning of width 100%?

Width 100% On the other hand, if you specify width:100%, the element's total width will be 100% of its containing block plus any horizontal margin, padding and border (unless you've used box-sizing:border-box, in which case only margins are added to the 100% to change how its total width is calculated).


1 Answers

New way I've just stumbled upon: css calc():

.calculated-width {     width: -webkit-calc(100% - 100px);     width:    -moz-calc(100% - 100px);     width:         calc(100% - 100px); }​ 

Source: css width 100% minus 100px

like image 61
rom_j Avatar answered Oct 05 '22 13:10

rom_j