1. Home
css

CSS Tutorial: A Comprehensive Guide

Master CSSS with our in-depth tutorials. From beginner to advanced topics, explore essential concepts and upskill yourself.

  • 23
  • 4
right-top-arrow
7

How to Center a Div with CSS

Updated on 18/09/2024238 Views

Alignment and positioning of items are important in web design. This helps to create user-friendly and aesthetic interfaces. 

One of the most popular HTML elements is the div. Centering the div helps create visually appealing and highly functional websites. To nail a flawless centered div look, you need to master your CSS skills. In this article, we will explore different aspects of centering a div with CSS. We will cover the basics and move on to advanced techniques to teach you how to center a div correctly in your web projects.

Understanding CSS Centering: The Basics 

Before getting into specific techniques, it's important to lay the foundation and understand the basics of CSS centering and how to center a div in CSS.

CSS Div Align Center

The text-align attribute is utilized to horizontally align inline and inline-block elements within respective parent containers. These are powerful techniques for centering divs that include text or inline items, although it is most commonly linked with text alignment.

Characteristics: 

  • Horizontal alignment of items that are inline and inline-block. 
  • Applicable to divs that include text, graphics, or elements that are inline. 

Advantages: 

  • Easy to execute. 
  • Align center is consistent with a number of different CSS layouts. 

Real-world application: 

  • Navigation menus that are centered. 
  • Buttons and inline items that are aligned within a div.

Example:

.center-text {

    text-align: center;

}

The “.center-text” class selection is a CSS align class selector that targets items to align the text horizontally. This particular selector is given the property text-align: center; which centers the CSS center text within the elements.

Explanation

  • HTML elements with the class name "center-text" are targeted by this class selector, denoted by ".center-text." position the text in the center. 
  • Text contained within the elements being targeted is aligned horizontally to the center using this CSS attribute.

Usage:

Implement the CSS class to a div element as follows:

<div class="center-text">

    This text will be centered.

</div>

The result will be a horizontal alignment of the text, “This text will be centered,” within the div.

CSS Vertical-Align Text

The use of CSS vertical-align middle helps guarantee that the text is center-positioned in the container. This improves readability and visual consistency.

Characteristics: 

  • Vertical alignment of inline and inline-block items. 
  • Text alignment within table cells and table divisions. 

Advantages: 

  • Increases readability and visual consistency. 
  • Results in a balanced layout. 

Real-world application: 

  • Aligning text included in headings, paragraphs, and buttons. 
  • Centering text within cells of a table.

Example:

.vertical-align-text {

    display: inline-block;

    vertical-align: middle;

}

With the.vertical-align-text class option, you can make the text inside an element stand out vertically. This selector is given the traits display: inline-block; and vertical-align: middle; to make it line up vertically.

Explanation: 

  • “.vertical-align-text” works on any HTML element that has the "vertical-align-text" class. 
  • The “display: inline-block;” feature changes the way the element is shown from block to inline. This lets the element sit next to other elements and retain block-level features, like being able to set its width and height. 

The content of the selected element gets vertically aligned to the middle of its parent container when using this feature.

Methods to Center Div Using CSS

You can easily center a division through different approaches. Here’s a look at these approaches in greater depth:

A. Margin Auto 

The ‘margin: 0 auto;’ technique is utilized for horizontally centering block-level elements like divs. 

Characteristics: 

  • Block-level items are centered horizontally. 
  • There must be a predetermined width for the element that is centered. 

Advantages: 

  • User-friendly and easy to navigate. 
  • Acceptable with the majority of CSS layouts. 

Real-world application: 

  • Components and portions of the content are centered. 
  • Divisive elements inside responsive layouts are aligned.

Example:

.centered-div {

    width: 50%; /* or any desired width */

    margin: 0 auto;

}

This is a CSS class selector for block-level items that you want to center horizontally within their parent container. The settings width: 50%; and margin: 0 auto; are used on this selector to make it center horizontally.

Explanation:

  • Centred div: Any block-level HTML element with the class name "centered-div" will be targeted by this.
  • width:50%: You can use this CSS feature to make an element 50% of its parent container's width as wide. You can change this number to get any width you want.
  • Margin: 0 auto: This CSS feature makes the top and bottom margins 0 and figures out the left and right margins automatically. This centers the element horizontally within its parent container.

Usage:

If you give this CSS class to a block-level element like a div, it will center it horizontally inside its parent container.

<div class="centered-div">

    This div will be horizontally centered.

</div>

In this example, the CSS center div has a 50% width compared to its parent container and has a horizontal center alignment with the parent container.

B. Flexbox

Flexbox is a flexible technique that makes complex layouts user-friendly. It is also a useful way to center divs.

Characteristics: 

  • Flexible layout tool. 
  • Allows horizontal and vertical adjustments.

Advantages: 

  • Eases complicated layouts. 
  • Gives you control over how things are spaced and lined.

Real-world applications

  • Smoother navigation across devices. 
  • Set up of flexible grid layouts to ensure information is aligned.

Example:

.flex-container {

    display: flex;

    justify-content: center; /* Horizontal centering */

    align-items: center; /* Vertical centering */

}

You can use “the.flex-container” class selector to target parts you want to style with the Flexbox layout. This selector can be given the values display: flex;, justify-content: center;, and align-items: center; to center it horizontally and vertically within the flex container.

Explanation:

  • .flex-container: Any block-level HTML element with the class name "flex-container" will be targeted by this.
  • display:flex: You can use Flexbox layout features on the element that this CSS property is applied to by turning it into a flex container. 
  • justify-content: center: This CSS property places the flex items horizontally inside the flex container.
  • Align-items: center: With this CSS property, the flex things inside the flex container are centered vertically.

Usage:

You can vertically and horizontally center the child element within the parent container when you use CSS class with div container:

<div class="flex-container">

    <div>Child Element 1</div>

    <div>Child Element 2</div>

</div>

This example shows a classic CSS vertical align text. The elements ‘Child element 1’ and ‘Child element 2’ have horizontal and vertical centering within the flex-container division.

C. Grid Layout

With CSS grid, you can easily arrange elements into rows and columns. This allows precise adjustments and makes it easier to create layouts.

Characteristics

  • Easy control of a plan in two dimensions. 
  • Manage complex grid designs. 

Advantages

  • Easily create complex layouts. 
  • Gain control over grid items. 

Real-world applications: 

  • Layouts with more than one column. 
  • To make applications that use cards.

Example:

.grid-container {

    display: grid;

    place-items: center; /* Center both horizontally and vertically */

}

If you want to use CSS Grid layout to style elements, you can use the.grid-container class identifier. This selector is given the values display: grid and place-items: center to center it both horizontally and vertically within the grid container.

Explanation: 

  • .grid-container: Given this class selection, any block-level HTML element with the class name "grid-container" will be what it goes after.
  • display:grid: This CSS property turns the element you want to target into a grid container, so you can use CSS grid layout features.
  • place-items: center: This is basically an abbreviated version of ‘align-items: center’ and ‘justify-items: center’. It is used to vertically and horizontally align the grid items within the grid container.

Usage:

If you add this CSS class to a div container that has child elements inside it, it will center the child elements both horizontally and vertically inside the grid container.

<div class="grid-container">

    <div>Child Element 1</div>

    <div>Child Element 2</div>

</div>

With CSS Grid style, the child elements "Child Element 1" and "Child Element 2" will be centered both horizontally and vertically inside the.grid-container div. 

Advanced Techniques for Entering a Div

A. Using Padding and Text Alignment

Using space and text-align helps make content that is centered, visually appealing. You can add padding around the content and align text to create a balanced and aesthetically pleasing layout.

Example:

.advanced-center {

    padding: 20px;

    text-align: center;

}

The “.advanced-center” class adds 20 pixels of space around the content. It places the text horizontally within its container.

B. Vertical Alignment with Flexbox

Flexbox helps you line elements vertically. You can set the properties of the flex container. This makes it easy to center parts both horizontally and vertically.

Example:

.vertical-center {

    display: flex;

    justify-content: center; /* Horizontal centering */

    align-items: center; /* Vertical centering */

}

The.vertical-center class in this case uses Flexbox to center the child elements inside the flex container both horizontally and vertically.

Final Words

When you master how to align a div to center, you learn how to use the rules, as well as get creative with them. You can easily learn how to align a div in center, text alignment, Flexbox, and grid layout. This will help you create attractive, well-structured web layouts.

It becomes critical to know how to center a div because it allows you to create aesthetically pleasing web pages. With these techniques, you can transform your web pages and create a better user experience. Experiment with these techniques and practice web design to get better at creating immersive user experiences.

FAQs

1. How do I center a div without flex?

If you don't have Flexbox, you can center a div by using the margin: 0 auto; way and by giving it width.

2. How do I fix a div in the center of the page in CSS?

Apply transform: translate (-50%, -50%); after setting position: fixed; with top: 50%; and left: 50%; to position a div in the page's center.

3. How do I center a div in a column?

To align a div within a column, use the margin: auto; with a specified width; and text-align: center; properties in combination.

image

Mukesh

Working with upGrad as a Senior Engineering Manager with more than 10+ years of experience in Software Development and Product Management.

Talk to Career Expert
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...