HTML span align center not working?

David19801 picture David19801 · Dec 5, 2011 · Viewed 552.6k times · Source

I have some HTML:

<div align="center" style="border:1px solid red">
This is some text in a div element!
</div>

The Div is changing the spacing on my document, so I want to use a span for this instead.

But span is not centralizing the contents:

<span style="border:1px solid red;align=center">
This is some text in a div element!
</span>

How do I fix this?

EDIT:

My complete code:

<html>
<body>

<p>This is a paragraph. This text has no alignment specified.</p>

<span style="border:1px solid red;text-align=center">
  This is some text in a div element!
</span>

</body>
</html>

Answer

Will picture Will · Dec 5, 2011

A div is a block element, and will span the width of the container unless a width is set. A span is an inline element, and will have the width of the text inside it. Currently, you are trying to set align as a CSS property. Align is an attribute.

<span align="center" style="border:1px solid red;">
    This is some text in a div element!
</span>

However, the align attribute is deprecated. You should use the CSS text-align property on the container.

<div style="text-align: center;">
    <span style="border:1px solid red;">
        This is some text in a div element!
    </span>
</div>