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?
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;
Hope that helps.