Profile image of netCoder78401
Submitted by netCoder78401
almost 11 years

style="text-align:center" vs align="center" vs <center>

I know of three different methods to align text in html.

  1. <p style="text-align:center">text</p>
  2. <p align="center">text</p>
  3. <p><center>text</center></p>

I am wondering if one is considered best practice or if there are good reasons to use one over the other or if there are different scenarios when one should be used vs the other.

I know the first example is using inline css (as is anything that uses the “style” attribute. I was under the impression that using inline css will-nilly was generally not considered best practice and I assume it’s being used in the lessons mainly because it is an easy way to teach about attributes and the css best practices will probably be covered later on in the actual css lessons. Someone correct me if I’m wrong.

The second one appears to be just a straight html attribute. It makes the most sense to use to me since alignment is an “attribute” of text and it doesn’t involve sticking another type of code (css) into it. Although in practice it seems like it shouldn’t really make much difference than using the inline css.

The third one seems like it only makes sense to use if for some reason I only want to change the alignment of a part of a paragraph while keeping the rest the same. However, I can’t think of a practical situation in which I would be changing the alignment without starting a new paragraph so for structural purposes it seems like it would be best just to have a new paragraph tag with its own align attribute.

Answer 539958267c82cae402002317

0 votes

Permalink

the last one is really outdated. center tags are for html4, you need the first one. the second one is less outdated, and not to bad to use, but the best is the first one. it is the html5 (the newest html) and yes, inline css is bad practice, but it is good practice to start somewhere. so the ideal syntax would be: <p>some text</p>

p { text-align: center; }

Profile image of stetim94
Submitted by stetim94
almost 11 years