Skip to main content

HTML Tags

HTML <center> Tag

Written by Updated HTML <center> Tag exercises

The center tag still centers text in every browser, but it was removed from HTML years ago. Here is what it did, and the CSS that replaces it for text, images and whole boxes.

What the center tag did

<center> centered everything inside it horizontally: text, images, tables. It came from the 1990s, when HTML handled looks as well as structure. HTML5 made it obsolete because looks belong in CSS. Browsers still support it so that old pages do not break, but you should not use it in new code.

Syntax

html

<center>Content to be centered</center>

It has no attributes of its own.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Center Tag Example</title>
</head>
<body>

<h2>Deprecated &lt;center&gt; Tag Example</h2>

<center>
  <p>This paragraph is centered using the deprecated &lt;center&gt; tag.</p>
</center>

<p>Modern alternative: use CSS <code>text-align: center;</code></p>

</body>
</html>

The text inside appears centered. That is exactly why people still find it: it works. The problem is not that it fails, it is that you cannot change it from your stylesheet, and validators flag it as an error.

How to center things with CSS today

Different things need different properties. This is where most beginners get stuck, so here are the three cases:

Center text and inline content

css

.box {
  text-align: center;
}

text-align: center centers text, links and images inside a box. It is the direct replacement for <center>.

Center a block, like a card or a container

css

.card {
  width: 300px;
  margin: 0 auto;
}

margin: 0 auto centers the box itself. It only works when the box has a width smaller than its parent.

Center something both ways

css

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

Flexbox centers the child horizontally and vertically, which <center> could never do.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Every browser still centers the content, but only for backward compatibility. Use CSS instead.

HTML <center> Tag exercises

Centre with CSS

Write a paragraph that says Centred text with the class mid, and a style block that centres the text of that class.

Result

Check your answer to see it here.