tags:

views:

534

answers:

15

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?


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

That will do the trick for you

jmein
+12  A: 

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>
Clayton
I feel that this is a sloppy way of doing things.
Chris B.
If you use a CSS Stylesheet, you can clean it up much nicer. The idea of CSS is so you don't have all these style tags strewn throughout HTML. Simply tag the <p> with a class and define the style in an external stylesheet.
Kekoa
On second thought, I can make a class for it. It could be <p class="center"></p>. Thank you.
Chris B.
this is *absolutely* how you do it. You can also decide to automatically center elements in a style block }
Ben Scheirman
class="center" is not a good idea... You need to separate the "what" from the "how it looks"For example, these are poor choices for class names:leftDivblueBgborderRedThese would be better:headertaglinenavigationexternal_linkthe class should specify the "what" and the style should target those and give appropriate CSS to style it.
Ben Scheirman
Thank you. For some reason <h> tags that are within the <p> tags will not center, how would I go about centering the <h> tags without changing my current css for them?
Chris B.
Adding the style="text-align: center" attribute to the h tags will center them as well
Clayton
+2  A: 

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.

Kekoa
+2  A: 

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.

A: 

Hehe, well it's not as easy as it may seem. What I usually do is have all text inside divs, and then use the "text-align" css tag. As for the center tag, no idea.

Good luck :-)

A: 

If you want center text you can do it by using the "text-align: center" css declaration:

<div style="text-align:center;">Centered Text</div>

If you want to center an element you would give it a width and set the "margin: 0 auto" like this:

<div style="width: 100px; margin: 0 auto;>whole div will be centered</div>
Jimmie R. Houts
A: 

I know you specified "HTML" in your question, so this may not be what you are asking... but have you considered the CSS styles:

style="text-align:center;"

or

style="margin-left:auto;margin-right:auto;"
devSolo
There seems to be a consensus w/ all these answers, lol
devSolo
A: 

I highly recommend you read CSS Mastery by Andy Budd. It's a great book, a quick read, and it will definitely change your mind about how you view CSS.

Given your comments about CSS I think you'd get a lot out of this book.

Ben Scheirman
A: 

It was deprecated because it's a style that can't be controlled dynamically. It also doesn't let you separate your markup from your style.

Text can only be centered in the box element it resides in so text that you want centered has to be in a <p> or <div> or <td>, etc. You can't center text in a for instance.

text-align is the css attribute that replaced

Your other questions seem to have been adequately answered.

rvarcher
A: 

The best and most versatile way to handle this is for your < p> tags to inherit from their parent class, like so:

<style>
.paragraph p{
    text-align: center;
}
</style>

<div class="paragraph">
   <p>Text</p>
</div>

That way, when you want to style your website differently later on, you have a centralized location to change all < p> tags within the "paragraph" class and don't have to change them individually.

hypoxide
+1  A: 

To address the implied second question, the reason you have validation issues when you have a <h#> inside a <p> is because nesting headers inside of paragraphs is not allowed in HTML 4.0 and XHTML.

To expound, headings and paragraphs are semantic concepts, meant to inform the structure of your document and not imply anything about the appearance of their content (granted, they do appear different by default but that's not the point of their usage). It follows that a heading shouldn't be contained within a paragraph.

If your implementation calls for nesting headers inside of paragraphs you might want to rethink your approach to what you're trying to accomplish. If the intent is to indicate structurally "this is a heading", consider why a heading would be inside a paragraph. If the intent is to make certain text within a paragraph look different, then you would want to use one of the inline elements such as a span or em, with associated CSS styling.

I Have the Hat
+1  A: 

Beyond the question of separating (... or not) styling from content, it's worth mentioning that "official" deprecation from HTML does not mean that it does not work as well as it has. Given the way browsers work and get developed, your center tag will keep on working for a while, possibly until end of time HTML itself is used. :-)

That is, change it if you feel it's the right thing to do, but not if you think it doesn't work any more. Plus: removing it if it works and alternatives don't... well, I wouldn't change it myself.

StaxMan
+2  A: 

Regarding a <h#> tag within a <p> tag, that is indeed a validation issue.

<h#> tags are meant to mark up headings. <p> tags are meant to mark up paragraphs.

So, if I was marking up your question, I might start like this:

<h2>Centering Text in HTML</h2>

<p>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.</p>

If I wanted the heading to be center aligned, I’d again use CSS:

<h2 style="text-align: center;">Centering Text in HTML</h2>

<p>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.</p>
Paul D. Waite
A: 

You should be transitioning to CSS for style. HTML / XHTML are intended to be purely for content.

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

Also, I don't believe that <h#> tags should be nested within <p> tags.

For more info, see w3schools.com

1moreriley
A: 
<head>
    <style type="text/css"> 
    <!-- 

    div.content,
    div.content h1,
    div.content h2,
    div.content h3,
    div.content h4,
    div.content h5,
    div.content h6 {
        text-align:center;
    }
    --> 
    </style>
</head>
<body>
  <div class="content"><h2>Text</h2></div>
</body>

you could do this if you wanted the h tags to be centered too, and they are correct... Don't put H tags in a P tag.

Chad Scira