Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between <span> and <div> with text-align:center;?

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?

like image 718
Akos Avatar asked Oct 13 '11 15:10

Akos


People also ask

What is the difference between the div and SPAN text?

A div is a block-level element and a span is an inline element. The div should be used to wrap sections of a document, while use spans to wrap small portions of text, images, etc. The <div> element is used while creating CSS based layouts in html, whereas <span> element is used to stylize texts.

Can you use text align with SPAN?

Use a p or div rather than a span. Text is an inline element and so is a span. For text-align to work, it must be used on a block level element (p, div, etc.) to center the inline content.

What is the difference between span and div tag?

The <div> tag is a block level element. The <span> tag is an inline element. It is best to attach it to a section of a web page. It is best to attach a CSS to a small section of a line in a web page.

How Center align span in div?

The CSS just sizes the div, vertically center aligns the span by setting the div's line-height equal to its height, and makes the span an inline-block with vertical-align: middle. Then it sets the line-height back to normal for the span, so its contents will flow naturally inside the block.


3 Answers

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.

like image 144
Spudley Avatar answered Oct 09 '22 23:10

Spudley


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;"
like image 25
ooPeanutButter Avatar answered Oct 10 '22 00:10

ooPeanutButter


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.

like image 4
Dennis Traub Avatar answered Oct 09 '22 23:10

Dennis Traub