The fiddle: http://jsfiddle.net/uK7w6/1/
<h1>this is a really, really long sentence</h1> h1 { min-width:100px; max-width:100px; text-align:center; }
If you get rid of the max and min width, the sentence centers fine, but I want it to center as well as not take up the whole width. Why does the text align left when I add width constraints?
Technique 1 The max-width does exactly what it says, it sets a maximum width for the center content. The margin declaration is what centers the content in the browser window. The "0" sets the top and bottom margin to "0" and the "auto" sets the right and left margin to "auto" which is what actually centers the content.
Short answer: your text isn't centered because the elements are floated, and floated elements "shrink" to the content, even if it's a block level element.
This is because when you center your text, the starting place of each line changes. This forces your users to work harder to find where each line begins to continue reading. Without a straight left edge, there is no consistent place where users can move their eyes to when they complete each line.
The text within the h1
is actually centered, but it is centered within a 100px width box
. If you want the h1 to float in the middle of the window
then add a margin:0 auto;
. The primary reason that the h1
is aligned to the left has to do with the display:box
property.
h1 { min-width:100px; max-width:100px; text-align:center; margin:0 auto; }
<h1>this is a really, really long sentence</h1>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With