Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering Text in HTML

I know at first this seems like a very stupid question, but I have a good reason for asking it. Even though I might just be misled.

For EVER I have been using the <center>Text</center> tag to have text appear centered horizontally.

Apparently this tag is depreciated, and is no longer used. So I have tried using <p align="center">Text</p> although it seems to be buggy and I have read that It does not work in all browsers.

On top of that, when I have a header <h#> tag within a <p> tag there is also a validation issue.

The point is, I want to do this right, and although the center tag is tolerable, it is apparently not the best way to go. Why has it been depreciated? And what is your alternative? I honestly have no idea where to go from here.

P.S. Asking this question kills me inside.

EDIT::

Even while using the accepted answer;

<p style="text-align:center"></p>

I am not able to center <h> tags within the <p> tags. I have tried and read that <h# align="center"> does not work in all browsers and I have tried applying the style to the header tag with no avail. What do you think?


like image 709
Chris Bier Avatar asked May 08 '09 20:05

Chris Bier


4 Answers

The text alignment needs to be declared in CSS. You can do this in a CSS section at the top of the file, in a separate file, or in the element itself. The simplest method would be the latter (note that this method is not generally considered a good practice):

<p style="text-align: center">Text</p>

If you want to put it at the top of the html file, it would look like this:

<head>
<style type="text/css"> 
<!-- 
p.centered
{
  text-align:center;
}

--> 
</style>
</head>
<body>
...
  <p class="centered">Text</p>
</body>

The best method would be to have a separate CSS stylesheet containing the CSS. Then, add a link to the CSS in the <head></head> section of your html:

<head>
<link href="path/to/file/name.css" rel="stylesheet" type="text/css" />
</head>
like image 63
Clayton Avatar answered Oct 17 '22 22:10

Clayton


<p style="text-align:center;">Some Text Here</p>

That will do the trick for you

like image 43
Josh Mein Avatar answered Oct 17 '22 22:10

Josh Mein


try <p style="text-align: center;">Text</p>

or at the top of your html you can declare:

<style type="text/css">
.center { text-align: center; }
</style>

then later just apply the class:

<p class="center">Text</p>

just remember the text-align css property will work for block elements.

like image 23
user95144 Avatar answered Oct 18 '22 00:10

user95144


The politically correct way to do it is CSS.

Try to do something like:

<p style="text-align:center">Text</p>

Or even better, use a CSS stylesheet.

Really, you should not be using in-line CSS either, it is just as sloppy. You can define a style for all <p> tags, or attach a class like <p class="header" and define the header class in CSS like:

p.header { text-align: center }

This way your style is separated from your HTML, producing a much cleaner HTML file.

like image 2
Kekoa Avatar answered Oct 17 '22 23:10

Kekoa