38

I don't understand this:

I have tried to center the text-align of a HTML <span> tag but it did nothing... With the <div> tag, everything worked. Same sittuation with setting margin: 0px auto; in css.

Why does the span tag not support the text-align; function?

2
  • 8
    It's better to say that 'what is the difference between inline element & block element with text-align:center'
    – sandeep
    Commented Oct 13, 2011 at 16:06
  • text-align is not a function, but a CSS property. Commented Oct 22, 2017 at 15:21

6 Answers 6

117

the difference is not between <span> and <div> specifically, but between inline and block elements. <span> defaults to being display:inline; whereas <div> defaults to being display:block;. But these can be overridden in CSS.

The difference in the way text-align:center works between the two is down to the width.

A block element defaults to being the width of its container. It can have its width set using CSS, but either way it is a fixed width.

An inline element takes its width from the size of its content text.

text-align:center tells the text to position itself centrally in the element. But in an inline element, this is clearly not going to have any effect because the element is the same width as the text; aligning it one way or the other is meaningless.

In a block element, because the element's width is independent of the content, the content can be positioned within the element using the text-align style.

Finally, a solution for you:

There is an additional value for the display property which provides a half-way house between block and inline. Conveniently enough, it's called inline-block. If you specify a <span> to be display:inline-block; in the CSS, it will continue to work as an inline element but will take on some of the properties of a block as well, such as the ability to specify a width. Once you specify a width for it, you will be able to center the text within that width using text-align:center;

7
  • 2
    very well explained. Cheers!
    – frank
    Commented Aug 2, 2014 at 13:40
  • Excellent explanation. Thanks.
    – Debbie A
    Commented Apr 4, 2017 at 15:27
  • A more appropriate terminology would be “inline-level” and “block-level” elements. Commented Oct 22, 2017 at 15:22
  • This answer is not really correct. Although the logic makes sense, it doesn't accurately explain how the text-align property works. Commented Oct 22, 2017 at 15:53
  • "The difference in the way text-align:center works between the two is down to the width." ... No. The reason text-align doesn't work on an inline-level element has nothing to do with width. It has to do with the definition of text-align. Commented Oct 22, 2017 at 15:53
10

Like other have said, span is an in-line element.

See here: http://www.w3.org/TR/CSS2/visuren.html

Additionally, you can make a span behave like a div by applying a

style="display: block; margin: 0px auto; text-align: center;"
0
4

Spans are inline, divs are block elements. i.e. spans are only as wide as their respective content. You can align the span inside the surrounding container (if it's a block container), but you can't align the content.

Span is primarily used for formatting purposes. If you want to arrange or position the contents, use div, p or some other block element.

4

A span tag is only as wide as its contents, so there is no 'center' of a span tag. There is no extra space on either side of the content.

A div tag, however, is as wide as its containing element, so the content of that div can be centered using any extra space that the content doesn't take up.

So if your div is 100px width and your content only takes 50px, the browser will divide the remaining 50px by 2 and pad 25px on each side of your content to center it.

2

Span is considered an in-line element. As such is basically constrains itself to the content within it. It more or less is transparent.

Think of it having the behavior of the 'b' tag.

It can be performed like <span style='font-weight: bold;'>bold text</span>

div is a block element.

2

It might be, because your span element sets is side as width as its content. if you have a div with 500px width and text-align center, and you enter a span tag it should be aligned in the center. So your problem might be a CSS one. Install Firebug at Firefox and check the style attributes your span or div object has.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.