Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The "text-align: center" isn't working in a span element

I haven't done HTML and CSS for a while so I may be forgetting something, but for some reason a "style" tag with the "text-align" property set isn't working even in the simplest context. I'm about to show you the whole, entire file that I have but my problem is only in the two comments I have. Don't worry about the other stuff; it's for a little passion project I'm working on.

So here is the whole file. I have a lot of stuff in it that isn't relevant nor important; just focus on the code in the two comments.

<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>JSON Generator</title>
<link rel="stylesheet" href="web_mod.css"></link>
</head>
<body bgColor="#E3E3E3">
<!--Start here-->
<span style="text-align: center">Coded by AnnualMelons</span><br>
<!--Finish here-->
<span style="color: red; background-color: #2CE65A">Use this generator to generate the code required to create a JSON message.<br>
Fill in the blanks to generate the code. The generator will guide you through it as you go along. Have fun!</span>
<script>

</script>
</body>
</html>

The "Coded by AnnualMelons" part is supposed to be in the center but it's not. At least for me it's not.

I know that the other part of the file isn't relevant but I figured I might as well show you as it may be an external problem.

I'm sure I'm just making a silly mistake because I haven't done this for a while, but it's not working... so yeah. I'm using Firefox as my web browser in case that helps.

Thanks!

like image 927
KarteMushroomPandas Avatar asked Aug 25 '14 01:08

KarteMushroomPandas


People also ask

Does text Align Center work on span?

Text AlignTo center an inline element like a link or a span or an img, all you need is text-align: center . For multiple inline elements, the process is similar. It's possible by using text-align: center .

Why is text align center not working?

This is because text-align:center only affects inline elements, and <aside> is not an inline element by default. It is a block-level element. To align it, we will have to use something other than text-align , or turn it into an inline element.

How do you center align a span inside a 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.

How do I align text to the right span?

If you want to align a <span> element to the right of the <div>, you can use some CSS. Particularly, you need to use the float property with the “right” and “left” values.


2 Answers

The <span> Element is, by default, an "inline" element. Meaning unlike block level elements (<div> <h1> <p> etc.) the span only takes up as much horizontal space as its content.

text-align: center IS working, but you're applying it to an element that doesn't have a width greater than its content (as all block elements do).

I recommend either changing the span to a <p> element, or specifying the display: block property on your span.

Here's a JSfiddle to demonstrate that both a <span> with display: block; text-align: center and a <p> with text-align: center; achieve the same effect.

Hope that helps!

like image 163
Morklympious Avatar answered Sep 27 '22 19:09

Morklympious


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.

example:

<div style="text-align: center">Coded by AnnualMelons</div><br>
like image 21
David Delač Avatar answered Sep 27 '22 19:09

David Delač