Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML / CSS : Fixed Margin & Fluid Width

How should I make this with CSS:

I would like to have 2 divs or more and their width should be in percent, but the margin between the divs should be fixed, in this example 30px Fluid Divs

The problem for me is the margin between the two divs because I can put the divs into a bigger div and set left and right padding to 30px and thats ok, but what should I do with the margin between the two divs?

If I try to add for example to the first div margin-right:30px; then the width of the div will be 70% + 30px what will be overall greater than 100% and the second div will fall down.

So what is the solution for this?

like image 527
Adam Halasz Avatar asked Mar 01 '11 10:03

Adam Halasz


People also ask

How do you set a fixed margin in CSS?

Values. The size of the margin as a fixed value. The size of the margin as a percentage, relative to the inline size (width in a horizontal language, defined by writing-mode ) of the containing block. 0 , except if both margin-left and margin-right are set to auto .

How do you give a position to margin fixed?

You can use margin: 0 auto with position: fixed if you set left and right . This also works with position: absolute; and vertically.

What does margin 10px 5px 15px 20px mean?

margin: 10px 5px 15px 20px; top margin is 10px. right margin is 5px. bottom margin is 15px. left margin is 20px.

Does margin work with position fixed?

Margin does not work because position of the header is fixed. You have to use padding-top on your contentwrap. Save this answer.


2 Answers

Is this close enough?

Live Demo

HTML:

<div id="container">
    <div id="left"><div id="left2">leftgggg</div></div>
    <div id="right">right</div>
</div>

CSS:

#container {
    margin: 0 30px 0 30px;
    overflow: hidden;
    background: #f3c
}
#left {
    float: left;
    width: 70%;
    position: relative;
    left: -30px;
}
#left2 {
    height: 200px;
    margin: 0 0 0 30px;
    background: #ccc
}
#right {
    height: 200px;
    float: right;
    width: 30%;
    background: #666
}
like image 164
thirtydot Avatar answered Oct 18 '22 13:10

thirtydot


Calc support is decent now, so you can use that to mix and match units. Using that, you can come up with something that works pretty well:

http://jsfiddle.net/CYTTA/1/

#a {
    margin-left: 30px;
    width: calc(70% - 30px - 15px);
}

#b {
    margin-left: 30px;
    margin-right: 30px;
    width: calc(30% - 30px - 15px);
}

You may have to prefix calc with -webkit or -moz.

The width of the first one is 70% minus the left margin (30px) and minus half the middle margin (15px). The second one is 30% minus the right margin (30px) and minus half the middle margin (15px).

While the widths won't be exactly equal to 70% and 30%, you'll have a fluid layout with fixed margins.

like image 33
ahuth Avatar answered Oct 18 '22 13:10

ahuth