Solution 1 :

All ways of centering content without using CSS have been deprecated a long time ago (in 1999), already in the previous version of HTML which was 4.01, for example the two ways that have been suggested in other answers:

  • The align attribute, e.g.: <div align="center">...</div>
  • The <center>...</center> element

The basic idea of deprecating these things is that it is not HTML’s job to determine presentational aspects of the document; HTML describes the structural aspects.

CSS describes the presentational aspects; to connect both, you have CSS classes along with the universal class attribute in HTML at your disposal, but that is only one way.

CSS also offers many different types of selectors, among those you also have selectors at your disposal that describe the hierarchic structure of the elements to which the declarations should apply; and some CSS properties, e.g. text-align affect descendant elements too, via CSS inheritance.

See this example:

body { text-align: center }
<h1>This is a header</h1>
<span>This is a span element</span>
  <p>This paragraph is in a div element</p>

Solution 2 :

Use the center tag


Inside which we can place tags and other content of the BODY to get center aligned
Like this

      <h2>HELLO WORLD! </h2>

Problem :

I’m fairly new to HTML and CSS, and basically web-development in general, but I still want to know if there is a way to center any type of element in HTML.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <h1>This is a header</h1>
    <span>This is a span element</span>
    <div><p>This paragraph is in a div element</p></div>

Is there a way to center all of this without using CSS?


Comment posted by connexo

Please explain why you want to not use CSS for this. What do you mean by “absolute way”?

Comment posted by Soham

@connexo I don’t want to use CSS for this since it get’s very messy by creating loads of classes since I generally already use many classes for small things. Oops, sorry, I didn’t mean to say “absolute”, I wanted to know if there is a way to center any element regardless if it is in a div or not.