37

This has been asked before by other people, but I've never seen an adequate answer. Is there a valid replacement to the <center> tag?

I know there's margin: 0 auto;, but that requires setting a width. There's also align="center", but I believe that that's invalid code as well.

Is there something as simple as <center> that's valid? There are rare occasions where I still end up using it, even though it is deprecated. Just today, I ended up using a <center> to center the one button that needed centered on a web page. Yes, I could have set the width and gave it margin: 0 auto, but that's a lot of work for centering one single element, and it dirties up my code, which I take pride in keeping orderly. I don't really understand why <center> was deprecated in the first place, if nothing has replaced it.

Thanks!

JacobTheDev
  • 17,318
  • 25
  • 95
  • 158

9 Answers9

38

text-align: center is what you should use. In fact, the ideal way is this:

.center {
    text-align: center;
}
.center > div, .center > table /* insert any other block-level elements here */ {
    margin-left: auto;
    margin-right: auto;
}

Obviously, it's not quite as simple as you might hope for.

Personally, I just use:

.center {text-align: center;}
.tmid {margin: 0px auto;}

Then apply classes where needed.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
11

It's not that easy to center elements without the center-tag.

For this you need to do a workaround that works even in IE6:

You need a div wrapper around the element you want to be centered and use text-align:center; width:100%. Inside this div you place another div and set margin to auto and text-align:left;

<div style="text-align:center; width:100%">
    <div style="margin:auto; text-align:left;">
        This Container is centered
    </div>
</div>

If you just want to center text you can use <p style="text-align:center;"></p>

This is the core. To simplify the thing, you can use a class for this (like Kolink wrote):

CSS:

.center {
    text-align:center;
    width:100%;
}
.center:first-child { //doesn't work in IE 6
    margin:auto;
    text-align:left;
}

HTML:

<div class="center">
    <div>
        This Container is centered
    </div>
</div>
WolvDev
  • 3,182
  • 1
  • 17
  • 32
3

to center elements i use this:

.middlr {
   display: block;
   margin: auto;
}

works for every/any element. works for me.

ryanwaite28
  • 1,804
  • 2
  • 24
  • 40
1

The reason why <center> is deprecated is that it is not a semantic tag, rather presentational and we should steer clear of using anything in HTML that isn't semantic in nature.

It's the same reason that other presentational elements such as <b>, <i> etc have been deprecated, as there are ways of achieving the same in the CSS.

Mat Richardson
  • 3,576
  • 4
  • 31
  • 56
1

Replacement for center tag: (You should do both)

Use this style for parent element:

text-align:center;

Use this style for current element:

margin-left: auto; margin-right: auto;

gnath
  • 796
  • 5
  • 8
1
.centered {
  margin-left: auto !important;
  margin-right: auto !important;
}
* > .centered {
  text-align: center !important;
  display: block;
}
0

this is PHP-STORM offer: use this:

<div style="text-align: center;"></div>
javadroid
  • 1,421
  • 2
  • 19
  • 43
-1

text-align: center is the equivalent CSS

Cfreak
  • 19,191
  • 6
  • 49
  • 60
-1

I found this answer on a blog

<img src="bullswithhorns.jpg" alt="Michael with the bull" style="width:150px;height:200px;margin:0px auto;display:block">

Source

l33tHax0r
  • 1,384
  • 1
  • 15
  • 31