HTML Center

Using CSS text-align Property

The text-align property is one of the simplest ways to center text or inline elements within their containing block elements.

Example: Centering Text in a Paragraph

<style>
    p {
        text-align: center;
    }
</style>
<p>This paragraph will be centered.</p>

This CSS rule applies to all <p> elements, making the text within them centered horizontally.

Using CSS Flexbox

Flexbox is a powerful layout tool in CSS that provides more control over alignment and distribution of space among items in a container.

Example: Centering a Button Vertically and Horizontally

<style>
    .container {
        display: flex;
        justify-content: center; /* Center horizontally */
        align-items: center; /* Center vertically */
        height: 100vh; /* Full viewport height */
    }
</style>
<div class="container">
    <button>Click Me!</button>
</div>

This setup centers the button both vertically and horizontally within the .container div.

Using CSS Grid

CSS Grid layout also allows for easy centering in both dimensions, providing even more layout control than Flexbox.

Example: Centering Content with Grid

<style>
    .grid-container {
        display: grid;
        place-items: center; /* Shorthand for align-items and justify-items */
        height: 100vh;
    }
</style>
<div class="grid-container">
    <div>Content to be centered</div>
</div>

This centers the inner div both vertically and horizontally using CSS Grid.

Deprecated Method: The <center> Tag

The <center> tag, which was commonly used in older HTML documents to center content, is now obsolete in HTML5. Using this tag is not recommended as it does not comply with modern web standards.

Block-Level vs. Inline Elements

In HTML, elements are broadly categorized into two types: block-level elements and inline elements. This classification is based on how these elements behave in the flow of the document, their default formatting, and how they interact with surrounding elements.

Block-Level Elements

Characteristics:

  • Start on a new line and consume the full width available, stretching out to the left and right as far as they can.
  • Can contain other block-level elements or inline elements.
  • Commonly used to structure major parts of a webpage.

Examples:

<div>, <h1> - <h6>, <p>, <form>

Use Case:

<div>
    <h1>This is a heading</h1>
    <p>This is a paragraph inside a div block.</p>
</div>

Inline Elements

Characteristics:

  • Do not start on a new line; they appear within the normal flow of text.
  • Only take up as much width as necessary.
  • Cannot contain block-level elements.

Examples:

<span>, <a>, <strong>, <img>

Use Case:

<p>This is a <span>span element</span> inside a paragraph.</p>

Deprecated <center> Tag and Modern CSS Alternatives

The <center> tag was traditionally used in HTML to center-align content horizontally within its container. However, it has been deprecated in HTML5 due to its presentation-focused nature, which goes against the modern web development practice of separating content from styling.

Example Using <center>:

<center>This text is centered using the center tag.</center>

Modern CSS Approach

Today, CSS provides robust and flexible ways to handle positioning and alignment, adhering to the best practices of web standards.

CSS Example for Centering Content:

.centered-text {
    text-align: center;
}

HTML Using CSS:

<p class="centered-text">This text is centered using CSS.</p>

To center block-level elements like a <div>, you would use a different set of CSS properties.

Example Using Flexbox:

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

HTML Using Flexbox:

<div class="centered-div">This div is centered using Flexbox.</div>

Centering Block-Level Elements

Using Margin Auto

To horizontally center a block-level element, you can set the left and right margins to auto while specifying a width.

Example:

cssCopy code

.centered {
    width: 50%; /* Define width */
    margin: 0 auto; /* Auto margins for horizontal centering */
}

Using Flexbox

Flexbox is a modern layout model that provides more control over alignment and space distribution between elements within a container.

Example:

.flex-container {
    display: flex;
    justify-content: center; /* Center children horizontally */
}

Using CSS Grid

CSS Grid is another powerful layout technique for both horizontal and vertical centering.

Example:

.grid-container {
    display: grid;
    place-items: center; /* Center items both horizontally and vertically */
}

Centering Inline Elements

Unlike block-level elements, inline elements do not start on a new line and only take up as much space as their content. Centering inline elements often involves manipulating their container.

Using Text-Align

The simplest way to center inline elements such as text, links, or inline-blocks within their container is using the text-align property on the parent.

Example:

.text-center {
    text-align: center;
}

Inline-Block Display

Change the display property to inline-block and use text-align for horizontal centering.

Example:

.text-center {
    text-align: center;
}

Practical Example

Here is a practical example incorporating both block-level and inline centering techniques:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Centering Elements</title>
    <style>
        .container {
            width: 80%;
            margin: 0 auto; /* Centers the container */
            text-align: center; /* Centers inline elements inside the container */
            display: flex;
            justify-content: center; /* Flexbox centering for block elements inside */
            border: 2px solid #000; /* Visual aid */
            padding: 20px;
        }
        .block {
            width: 50%;
            padding: 10px;
            border: 1px solid blue; /* Visual aid */
        }
        .inline-block {
            display: inline-block;
            width: 100px;
            border: 1px solid red; /* Visual aid */
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="block">Block Level Centered</div>
        <span class="inline-block">Inline Block</span>
    </div>
</body>
</html>

Applying the margin Property to Auto

When designing a webpage, one important aspect to consider is the layout and positioning of the elements on the page. The margin property is a CSS attribute that allows developers to control the space between elements and the border of their container. By applying the margin property with the value of “auto,” developers can effortlessly center an element horizontally within its container. This feature is particularly useful for creating responsive designs or aligning images and text in a visually appealing way. In this article, we will explore the various uses and benefits of applying the margin property to auto, providing clear instructions on how to implement it effectively. Whether you are a beginner or an experienced developer, understanding how to apply this property will enhance your web design skills and contribute to creating more engaging and balanced layouts.

Centering Inline Elements

To center inline elements, we need to understand the difference between block and inline elements and how they affect the flow of a page.

Block elements, such as paragraphs and headings, start on a new line and take up the full width available. They create a block of content and typically have a defined width and height. Inline elements, on the other hand, do not start on a new line and only take up as much width as necessary. Examples of inline elements include images, links, and text spans.

To center inline elements, we can use special code that alters their display property. By default, inline elements can't be centered like block elements. However, changing the display property to “block” or “inline-block” allows them to be centered.

Centering an Image

img {
    display: block;
    margin: 0 auto;
}

By setting the display property of the image to block and applying a margin of 0 auto, the image will be centered both horizontally and vertically within its container.

Similarly, other inline elements like links or text spans can be centered by modifying their display property similarly.

Techniques for Centering Inline Elements on a Webpage

Using Text-Align

By applying the “text-align: center” style to the parent element, the inline elements within that container will be centered. This technique is commonly used for simple scenarios where the elements are meant to be centered within their parent container.

Using Flexbox

By setting the parent container's display property to “flex” and then using the “justify-content: center” property, the inline elements within the container will be horizontally centered. This technique is especially useful when dealing with multiple inline elements that need to be centered together.

Using the display Property with Value Set to “inline-block”

To address the next heading, you can utilize the display property with a value set to “inline-block”. This method is particularly useful when dealing with elements that are neither block nor inline elements.

By setting the display property to “inline-block”, you can effectively center elements on a webpage. Normally, inline elements cannot be centered using traditional methods, such as text-align, but with “inline-block” they can be.

The display property essentially allows you to control how an element is rendered on the webpage. Inline-block elements are treated as inline elements, meaning they flow within the surrounding content, but they also allow for width and height to be set. This enables you to create a block-like element with dimensions while still maintaining the characteristics of an inline element.

Example:

.element {
    display: inline-block;
}

By applying this CSS to the next heading element, it will be displayed as an inline-block, allowing you to center it using methods like text-align: center. This provides more flexibility in positioning and styling elements that may not conform to traditional inline or block behaviors.

Applying the text-align Property to a Parent Container Element

To apply the text-align property to a parent container element, you can follow these steps. This method is useful when you want to center multiple elements within a container.

  1. Identify the parent container element: Determine which element will act as the parent container that will hold the elements you intend to center.
  2. Add a class or ID to the parent container: To apply the text-align property specifically to the parent container, it is recommended to add a class or ID to the element. This will make it easier to target in your CSS stylesheets.
  3. Apply the text-align property: In your CSS stylesheet, target the parent container element using the class or ID you added in the previous step. Use the property “text-align” followed by the value “center” to horizontally center the elements within the parent container.
  4. Save and refresh: Save your changes in the CSS file, then refresh the web page to see the updated alignment of the elements within the parent container.

It's important to note that the text-align property is applied to the parent element, not the individual elements inside it. This means that all the text and elements within the parent container will be centered horizontally. If you only want to center specific elements within the parent container, you may need to use alternative methods such as flexbox or CSS grid.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate