Reputation: 13709
I have following code
<div id="main">
<div id="one"> </div>
<div id="two"> </div>
<div id="three"> </div>
<div id="four"> </div>
</div>
I need to align the middle 4 div as below, keeping equal space at each side (top-space = bottom-space and right-space = left-space):
______________________________________
| |
| ________ ________ |
| | || | |
| | one || two | |
| | || | |
| |________||________| |
| ________ ________ |
| | || | |
| | three || four | |
| | || | |
| |________||________| |
| |
|____________________________________|
Four div's equally spaced, please can anyone me help me out here with any css snippet? Also I do see a lot of question over this, but can't get this fixed. Can someone point me to any useful link that explains all the concepts related to the div alignment perfectly ?
(Folks, I know this would be a duplicate, but please help as I am just going round and round by googling.)
Thanks in advance :)
Upvotes: 1
Views: 2803
Reputation: 2210
Centering horizontally is the easy part, but there is a neat trick to get things aligned vertically using absolute positioning and negative margins. Here's a working example I wrote a few years back.
Here's some code and explanation:
<div id="main">
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div id="four"></div>
</div>
CSS
#main {
position: absolute;
top: 50%; /* gets the first pixel in the center of the browser */
left: 50%;
height: 860px;
width: 860px;
margin-top: -430px; /* negative margin half the height of the div to make it appear center */
margin-left: -430px;
border: solid 1px #000;
overflow: visible; /* allows an absolutely positioned element to contain floats */
}
#one, #two, #three, #four {
float: left;
height: 400px;
width: 400px;
background-color: blue;
margin: 20px;
}
#one, #three {
margin-right: 0;
}
#one, #two {
margin-top: 20px;
margin-bottom: 0;
}
Upvotes: 1
Reputation: 3128
I went for a slightly different answer (but @j08691 has a good solution),
#main{
position:absolute;
top:50%;
left:50%;
width:100px;
height:100px;
margin:auto;
}
#one, #two, #three, four{
float:left;
width:50px;
height:50px;
}
Complete working code that I tested it on was,
<html>
<head>
<style media="screen" type="text/css">
#main{
position:absolute;
top:50%;
left:50%;
width:100px;
height:100px;
margin:auto;
}
#one, #two, #three, four{
float:left;
width:50px;
height:50px;
}
.box{
float:left;
width:50px;
height:50px;
}
#one{
background-color:#f00;
}
#two{
background-color:#0f0;
}
#three{
background-color:#00f;
}
#four{
background-color:#000;
}
</style>
</head>
<body>
<div id="main">
<div id="one" class="box"> </div>
<div id="two" class="box"> </div>
<div id="three" class="box"> </div>
<div id="four"class="box" > </div>
</div>
</body>
</html>
Upvotes: 0
Reputation: 2799
@j08691 Has a good example. But here's mine if it's of any use...
<html>
<body>
<div style="width: 960px; margin: 0 auto;">
<div>
<div style="width: 480px; float: left;">
<div style="padding: 10px; border: 1px solid #F00;">
1
</div>
</div>
<div style="width: 480px; float: left;">
<div style="padding: 10px; border: 1px solid #F00;">
2
</div>
</div>
</div>
<div>
<div style="width: 480px; float: left;">
<div style="padding: 10px; border: 1px solid #F00;">
3
</div>
</div>
<div style="width: 480px; float: left;">
<div style="padding: 10px; border: 1px solid #F00;">
4
</div>
<div>
</div>
</div>
</body>
</html>
Upvotes: 1
Reputation: 207881
Here's one way that works in all modern browsers, including IE8: jsFiddle example.
HTML
<div id="main">
<div id="one"></div>
<div id="two"></div><br />
<div id="three"></div>
<div id="four"></div>
</div>
CSS
div {
border:1px solid #999;
}
#main {
width:400px;
height:400px;
display:table-cell;
vertical-align:middle;
text-align:center;
}
#one,#two,#three,#four{
width:100px;
height:100px;
display:inline-block;
}
Note that i did have to add one break tag (<br />
) to your code.
Upvotes: 3