Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pesky lil' <small> element not working with text-align, needs to be blocked

So you've tried to center the <small> HTML element with the CSS property text-align: center (or right) and it doesn't work?

small element resisting text-align

Well, that could be because your HTML/CSS looks something like this. There's an easy CSS solution...

If you set a small { display: block } property like this then it works like a charm:

small element being good

Horay!

But you can probably tell something's not right... and why does it work anyway?

like image 244
Baumr Avatar asked Nov 03 '12 02:11

Baumr


People also ask

Does text align work on block elements?

Even though the property says “text” align, it affects all elements inside the block-level element that are either inline or inline-block elements. The property only affects the content inside the element to which it is applied, and not the element itself.

Why text Align Center doesn't work?

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.

How do you align a block element?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do you fix aligned text in HTML?

To set text alignment in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property text-align for the center, left and right alignment.


1 Answers

Well, <small> is basically an inline element and the text-align property is to be applied to block elements and passed down to child inline elements (or strings inside).

By making small into a block element we are allowing the text inside it to be styled.


Also, the W3 spec defines the <small> element to be used inside a phrasing context — similarly to elements like <strong>, <b>, <span>, <a>, and so on — which would do the same thing in this case.

W3 wiki examples suggest to put the <small> inside <p> tags and style that — I guess that's the more semantic solution, and here's the code if you need to see it.


Why is this a bit confusing?

Well, some 3rd party resources on the web don't mention it, or at least not explicitly. For example, the HTML5 Doctor's example uses the small element without a block element container around it.

Hope that clarifies things! As always, please let me know if I missed anything. (And I think I did.)

like image 131
Baumr Avatar answered Sep 17 '22 13:09

Baumr