HTML span align center not working?

HtmlCss

Html Problem Overview


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>

Html Solutions


Solution 1 - Html

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>

Solution 2 - Html

Please use the following style. margin:auto normally used to center align the content. display:table is needed for span element

<span style="margin:auto; display:table; border:1px solid red;">
    This is some text in a div element!
</span>

Solution 3 - Html

The align attribute is deprecated. Use CSS text-align instead. Also, the span will not center the text unless you use display:block or display:inline-block and set a value for the width, but then it will behave the same as a div (block element).

Can you post an example of your layout? Use www.jsfiddle.net

Solution 4 - Html

span.login-text {
    font-size: 22px;
    display:table;
    margin-left: auto;
    margin-right: auto;
}

<span class="login-text">Welcome To .....CMP</span>

For me it worked very well. try this also

Solution 5 - Html

On top of all the other explanations, I believe you're using equal "=" sign, instead of colon ":":

<span style="border:1px solid red;text-align=center">

It should be:

<span style="border:1px solid red;text-align:center">

Solution 6 - Html

Span is inline-block and adjusts to inline text size, with a tenacity that blocks most efforts to style out of inline context. To simplify layout style (limit conflicts), add div to 'p' tag with line break.

<p> some default stuff
<br>
<div style="text-align: center;"> your entered stuff </div>

Solution 7 - Html

Just use word-wrap:break-word; in the css. It works.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid19801View Question on Stackoverflow
Solution 1 - HtmlWillView Answer on Stackoverflow
Solution 2 - HtmlChris JView Answer on Stackoverflow
Solution 3 - HtmlJeff CameraView Answer on Stackoverflow
Solution 4 - HtmlAIMABLEView Answer on Stackoverflow
Solution 5 - HtmlnhazeanView Answer on Stackoverflow
Solution 6 - Htmlmark stewartView Answer on Stackoverflow
Solution 7 - HtmlmonView Answer on Stackoverflow