CSS text-align:center, not working! What does “text-align” work on?

When you want to center the text, it is “text-align:center;” which is described as usual, but sometimes, it seems that “text-align:center” does not work.

Especially when there is “div” tag, “text-align” does not seem to work.

As I had a little hardship, I studied the function of “text-align” again, so I will show it.

1. What does “text-align” work on?

“text-align” is CSS to specify for block elements. And it works on inline elements in the block element.

Inline elements and block elements

Elements of html can be classified into block elements and inline elements.

What is a block element?

Block elements are a single block in the page, and heading tags such as div and h1, p tags, ul tags and li tags in ul are also block elements.
Although div tags can nest other block elements, header tags such as h1 and p tags can only contain inline elements inside.

<div><p>texttexttext</p></div> ← This is good!

<p>texttext<div>texttext</div></p> ← This is no good!

What is an inline element?

An inline element is something that is treated as a sentence or part of a sentence, such as a tag, span tag, or strong tag. img tag, input tag, textarea tag etc are also inline elements.

text-align is CSS that can be specified for block elements such as p tags and header tags such as h1 tags and div tags, but the effect appears in inline elements such as text. That is, they work with inline elements within block elements, not block elements themselves.

The following code and display results may be helpful.

See the Pen txt-align01-03 by natsuki (@natsukimemo) on CodePen.

<p class="sample01">
sample<br>
01
</p>
<p class="sample02">
sample<br>
02
</p>
<p class="sample03">
sample<br>
03
</p>
p.sample01{
border:1px solid #f00;
width:250px;
text-align:center;
}
p.sample02{
text-align:center;
}
p.sample03{
border:1px solid #f00;
text-align:center;
}

Sample01 above specified CSS width and CSS border in the p tag which is a block element.
The text in the p tag is centered, but the p tag itself is not centered on the screen.

When you look at sample 02, it looks as if the p tag itself was placed at the center, but this is because the p tag will be 100% of the screen width if you do not specify the width, so only the text in the p tag is the center It is aligned.

It may be understandable to see that the border is specified to p tag of sample03.

So how can you place the block element itself in the center?

How to place block elements in the center.

If you want to place the block element at the center, specify width and then specify auto on the left and right of margin.

How to place block elements in the center.

  1. Specify width in block element.
  2. Furthermore, specify auto for left and right margins.

So, let’s put sample01 in the center of the text and p tag itself.

See the Pen gyaPJE by natsuki (@natsukimemo) on CodePen.

<p class="sample01">
sample<br>
01
</p>
p.sample01{
border:1px solid #f00;
width:250px;
text-align:center;
margin:0 auto;
}

It was placed in the center by specifying auto on the left and right of p tag’s margin.

2. Child elements inherit the text-align of the parent element!

Child elements inherit the text-align of the parent element. However, there are times when it does not inherit.

Let’s nest elements by div tag and see various movements.

2-1. When a child element inherits by specifying text-align in div tag.

See the Pen OGyNLe by natsuki (@natsukimemo) on CodePen.

<div>
<table border="1">
<tr><td width="100">1</td><td width="100">2</td></tr>
<tr><td width="100">3</td><td width="100">4</td></tr>
</table>
</div>
div{
text-align:center;
}

The above has nested table tags in the div. And, the text is in the table. The table tag in the div tag becomes a block element.

In this case, the text is aligned in the center of the cell because text-align acts on the text that is an inline element in the td tag.

The text in this td tag inherits the parent block element div’s text-align.

To center this table tag itself, specify atuo to the left and right of margin.

2-2. When inheriting to a child element by specifying text-align in div tag.

Next, try to nest p tags into div tags.

See the Pen bJVpdG by natsuki (@natsukimemo) on CodePen.

<div class="outBox">
<p class="inBox">sample<br>
04</p>
</div>
div.outBox{
text-align:center;
}

This is also placed in the center by inheriting the text-align of the div tag that is the parent element of the p tag. As width is not specified, it is placed at the center of the screen.

2-3. When specifying text-align in div tag and not inheriting to child element.

The following is an example when specifying text-align in div tag does not inherit to child elements.
html is exactly the same as 2-2 above, and p tags are nested in div.

css specifies text-align for both p tag and div tag.

See the Pen BEoKKB by natsuki (@natsukimemo) on CodePen.

<div class="outBox">
<p class="inBox">sample<br>
05</p>
</div>
p.inBox{
text-align:right;
}
div.outBox{
text-align:center;
}

The above css is right-justified by specifying text-align: right; in the p tag in the second line.
After that, line 5 specifies text-align: center; in the div tag.

In this case, text-align: right is specified in the p tag, so it is working and right-justified.
Text-align of div tag which is parent element does not work. It is not inherited because text-align is specified in p tag.

3. Summary

POINT

  1. text-align should be specified for block elements and works for inline elements in it.
  2. Child element inherits text-align of parent element
  3. If text-align is specified to a child element, it is not inherited the parent element.

thank you for reading.