I want to have 3 divs aligned inside a container div, something like this:
[[LEFT] [CENTER] [RIGHT]]
Container div is 100% wid
You can try this:
Your html code like this:
<div id="container">
<div id="left"></div>
<div id="right"></div>
<div id="center"></div>
</div>
and your css code like this:
#container{width:100%;}
#left{float:left;width:100px;}
#right{float:right;width:100px;}
#center{margin:0 auto;width:100px;}
so, it's output should be get like this:
[[LEFT] [CENTER] [RIGHT]]
HTML:
<div id="container" class="blog-pager">
<div id="left">Left</div>
<div id="right">Right</div>
<div id="center">Center</div>
</div>
CSS:
#container{width:98%; }
#left{float:left;}
#center{text-align:center;}
#right{float:right;}
text-align:center;
gives perfect centre align.
JSFiddle Demo
This can be easily done using the CSS3 Flexbox, a feature which will be used in the future(When <IE9
is completely dead) by almost every browser.
Check the Browser Compatibility Table
HTML
<div class="container">
<div class="left">
Left
</div>
<div class="center">
Center
</div>
<div class="right">
Right
</div>
</div>
CSS
.container {
display: flex;
flex-flow: row nowrap; /* Align on the same line */
justify-content: space-between; /* Equal margin between the child elements */
}
Output:
.container {
display: flex;
flex-flow: row nowrap; /* Align on the same line */
justify-content: space-between; /* Equal margin between the child elements */
}
/* For Presentation, not needed */
.container > div {
background: #5F85DB;
padding: 5px;
color: #fff;
font-weight: bold;
font-family: Tahoma;
}
<div class="container">
<div class="left">
Left
</div>
<div class="center">
Center
</div>
<div class="right">
Right
</div>
</div>
Here are the changes that I had to make to the accepted answer when I did this with an image as the centre element:
#center
in this case). If it isn't, you'll have to set display
to block
, and it seems to centre relative to the space between the floated elements.Make sure to set the size of both the image and its container:
#center {
margin: 0 auto;
}
#center, #center > img {
width: 100px;
height: auto;
}
There are several tricks available for aligning the elements.
01. Using Table Trick
.container{
display:table;
}
.left{
background:green;
display:table-cell;
width:33.33vw;
}
.center{
background:gold;
display:table-cell;
width:33.33vw;
}
.right{
background:gray;
display:table-cell;
width:33.33vw;
}
<div class="container">
<div class="left">
Left
</div>
<div class="center">
Center
</div>
<div class="right">
Right
</div>
</div>
02. Using Flex Trick
.container{
display:flex;
justify-content: center;
align-items: center;
}
.left{
background:green;
width:33.33vw;
}
.center{
background:gold;
width:33.33vw;
}
.right{
background:gray;
width:33.33vw;
}
<div class="container">
<div class="left">
Left
</div>
<div class="center">
Center
</div>
<div class="right">
Right
</div>
</div>
03. Using Float Trick
.left{
background:green;
width:100px;
float:left;
}
.center{
background:gold;
width:100px;
float:left;
}
.right{
background:gray;
width:100px;
float:left;
}
<div class="container">
<div class="left">
Left
</div>
<div class="center">
Center
</div>
<div class="right">
Right
</div>
</div>
I like my bars tight and dynamic. This is for CSS 3 & HTML 5
First, setting the Width to 100px is limiting. Don't do it.
Second, setting the container's width to 100% will work ok, until were talking about it being a header/footer bar for the whole app, like a navigation or credits/copyright bar. Use right: 0;
instead for that scenario.
You are using id's (hash #container
, #left
, etc) instead of classes (.container
, .left
, etc), which is fine, unless you want to repeat your style pattern elsewhere in your code. I'd consider using classes instead.
For HTML, no need to swap order for: left, center, & right. display: inline-block;
fixes this, returning your code to something cleaner and logically in order again.
Lastly, you need to clear the floats all up so that it doesn't mess with future <div>
. You do this with the clear: both;
To summarize:
HTML:
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
<div class="clear"></div>
</div>
CSS:
.container {right: 0; text-align: center;}
.container .left, .container .center, .container .right { display: inline-block; }
.container .left { float: left; }
.container .center { margin: 0 auto; }
.container .right { float: right; }
.clear { clear: both; }
Bonus point if using HAML and SASS ;)
HAML:
.container
.left
.center
.right
.clear
SASS:
.container {
right: 0;
text-align: center;
.left, .center, .right { display: inline-block; }
.left { float: left; }
.center { margin: 0 auto; }
.right { float: right; }
.clear { clear: both; }
}