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?

Akos
  • 1,997
  • 6
  • 27
  • 40

6 Answers6

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;

starball
  • 20,030
  • 7
  • 43
  • 238
Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 2
    very well explained. Cheers! – frank Aug 02 '14 at 13:40
  • Excellent explanation. Thanks. – Debbie A Apr 04 '17 at 15:27
  • A more appropriate terminology would be “inline-_level_” and “block-_level_” elements. – Sebastian Simon Oct 22 '17 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. – Michael Benjamin Oct 22 '17 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`. – Michael Benjamin Oct 22 '17 at 15:53
  • According to the [**spec**](https://www.w3.org/TR/CSS22/text.html#alignment-prop), the `text-align` property applies only to ***inline-level content of a block container***. That's it. Plain and simple. – Michael Benjamin Oct 22 '17 at 15:54
  • Thanks for the comments. I think you're being a bit pedantic though. :-) The logic makes sense because it's right. Yes, the spec specifies that *`text-align` property applies only to inline-level content of a block container*, and that would be a valid answer too, but it is specified that way *because* it wouldn't make sense to apply it to an inline-level element due to the lack of a defined width. – Spudley Oct 22 '17 at 18:26
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;"
ooPeanutButter
  • 433
  • 2
  • 9
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.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
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.

slolife
  • 19,520
  • 20
  • 78
  • 121
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.

Dave G
  • 9,639
  • 36
  • 41
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.

longi
  • 11,104
  • 10
  • 55
  • 89