Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Min and max-width mess up text-align center

Tags:

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?

like image 229
mango Avatar asked Jan 20 '14 22:01

mango


People also ask

How do you center text with max-width in HTML?

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.

Why text Align Center doesn't work for Div?

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.

Why is center aligned text bad?

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.


1 Answers

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>
like image 195
Samuel Cook Avatar answered Sep 27 '22 20:09

Samuel Cook