As a CSS enthusiast, one of the questions I have heard a lot over the past few years has been, “How do I float center?” To that, I have always replied simply, “you don’t,” and then I delve into what my colleague is actually trying to do. (Really, when was the last time you wanted content to flow around both sides of a block?)
Today my answer has changed.
Alternatives
First off, let’s not use a cannon to kill a mosquito. If you need to center a single line of text (styling not withstanding), just set text-align: center
on the containing block. If you really need to center a block, set its left and right margins to identical values. If your block has an explicit width, set the left and right margins to auto
.
What if you do not have (or desire) explicit margins or an explicit width? What if you want several sibling blocks to have the same width? I’m glad you asked…
The simple, three-step process
Consider the following HTML fragment in this example.
<div class="rowWrapper">
<div class="row">Here are three block-display elements.</div>
<div class="row">There are no explicit widths set.</div>
<div class="row">Siblings have the same effective width and are centered.</div>
</div>
For the sake of the example, I’ll give the wrapper a red border and the rows a blue border.
1. Float the wrapper
.rowWrapper {
border: 1px solid red;
float: left;
}
Wrap your blocks and float the wrapper. Because your blocks are in normal flow, they will each have the same width by expanding to fill their parent block. Because the parent block is floated, it will have the minimum width necessary to contain the child blocks. This results in the following:
2. Position the wrapper
.rowWrapper {
border: 1px solid red;
float: left;
position: relative;
left: 50%;
}
This centers the left edge of the wrapper, and thus your soon-to-be-centered, now equal-width blocks.
3. Position the ‘rows’
.row {
border: 1px solid blue;
position: relative;
left: -50%;
}
This puts the center of the equal-width blocks at the left edge of the wrapper.
Naturally, you’ll want to remove any visible signs of the wrapper.
July 14, 2008 at 12:47 pm
Well written and very useful. Thanks Jon!
September 29, 2008 at 3:24 pm
nice trick, already came in handy, ive seen the same idea applied to achieve internet explorer 6 min-width to be completely css.
October 11, 2008 at 12:45 pm
[…] – bookmarked by 4 members originally found by faith4065 on 2008-09-20 float: center https://webmonkeyswithlaserbeams.wordpress.com/2008/07/09/float-center/ – bookmarked by 1 members […]
April 14, 2009 at 1:14 am
Very nice, except that it causes a horizontal scrollbar to blank space. Looking to see if I can fix that, I’ll post back if I figure it out.
July 26, 2009 at 12:47 am
This could probably work too. You can change the display to:
display:inline-block;
This display type will allow you to center the element with:
text-align:center;
August 29, 2009 at 6:00 pm
I tried this and it worked great, but then the body of my main page was centered as well, any ideas??
Scott
September 1, 2009 at 1:36 am
That’s a great idea. It would seem that’s the whole reason for the existence of (CSS2 value) ‘inline-block,’ to use minimum-necessary width, without the mess of floating. We’re still supporting IE6 for daveramsey.com, based on our usage statistics, so we still need a CSS1-only solution.
September 1, 2009 at 1:39 am
The style I used in the example centers everything in the body. This same trick could be used without centering the entire body. You need only set
text-align: center
on the parent of therowWrapper
.October 29, 2009 at 8:52 am
Yeah you can use text-align.. but, what if I want to center the but the text-align still left..
I think that’s a great tips, but still doesn’t fix the empty horizontal scroll bar. Hopefully someday margin and float can work together :))
January 14, 2011 at 4:35 am
In rereading this, the solution for the horizontal scroll bars hit me. You could wrap the whole thing in yet one more div and set overflow: hidden on that outer wrapper.
September 4, 2011 at 7:54 pm
http://jsfiddle.net/mKeHB/