style="text-align:center" vs align="center" vs <center>
I know of three different methods to align text in html.
<p style="text-align:center">text</p>
<p align="center">text</p>
<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
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; }
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons