Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

horizontally aligning multiple divs (CSS) [duplicate]

I need to align these divs so that the space between "content1" and the red div equals the space between "content4" and the red div. I don't mind changing the blue-div's margin but this should work for any width.

I used to achieve this by making the width of the 4 blue divs + their left and right margins = 100% but that doesn't seem to work well in this case.

I also tried adding another div inside the red one that contained all the blue divs and giving it margin: 0 auto but that's not working either.

Code in jsfiddle (updated)

PS: if i'm not clear enough, please feel free to edit my question.

like image 536
FranLegon Avatar asked Dec 11 '22 00:12

FranLegon


1 Answers

You can use the incredible property box-sizing: border-box; supported by all modern browser, IE8 included! And set the width and margin on % :

.red, .blue {
    -moz-box-sizing:    border-box;
    -webkit-box-sizing: border-box;
    box-sizing:        border-box;
}

.red {
    width:650px;
    height:1000px;
    background:red;
    padding: 1% 0 0 1%; // Space top and left of red
}

.blue {
    height:200px;
    width: 24%;
    background:blue;
    display:inline-block;
    float: left;
    margin: 0 1% 1% 0; // Space bottom and right of each blue
}

http://jsfiddle.net/Pik_at/L7qpgdkk/3/

like image 77
Pik_at Avatar answered Jan 01 '23 16:01

Pik_at