The Wayback Machine - https://web.archive.org/web/20241126213557/https://www.geeksforgeeks.org/css-align/
Open In App

CSS Align

Last Updated : 23 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CSS alignment techniques are essential for positioning items and distributing space between content. These techniques include horizontal and vertical alignment using various properties such as margin: auto, position: absolute, text-align, and padding. This article covers multiple methods to achieve alignment in CSS.

Methods of Aligning in CSS:

1. Center Align Using Margin:

The margin: auto property centers a block element horizontally.

Note: Using margin: auto will not work in IE8 unless a !DOCTYPE is declared.

Example 1: This example describes the CSS align using the margin: auto property.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
    .center {
        margin: auto;
        width: 60%;
        border: 3px solid #73AD21;
        padding: 10px;
    }
    </style>
</head>

<body>
    <h1 style="color:green;">  
            GeeksforGeeks  
        </h1>
    <h2>Center Align Elements</h2>
    <div class="center"> 
        This is div element on which
        margin auto is used to horizontally
        align it into center 
    </div>
</body>

</html>

Output:

2. Align Using Position:

The position: absolute property aligns elements relative to their containing block.

Note: The position properties  top, bottom, left, and right will not work unless the position property is set first.

Example 2: This example describes the CSS align using the position: absolute; property.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
    .right {
        position: absolute;
        right: 0px;
        width: 300px;
        border: 3px solid #73AD21;
        padding: 10px;
    }
    </style>
</head>

<body>
    <h1 style="color:green;">  
            GeeksforGeeks  
        </h1>
    <h2>Right Align</h2>
    <div class="right">
        <p> 
            Absolute positioned elements 
            can overlap other elements. 
        </p>
    </div>
</body>

</html>

Output:

3. Text Align:

The text-align: center property centers text within its container

Example 3: This example describes the CSS align using the text-align: center; property.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
    .center {
        text-align: center;
        border: 3px solid green;
    }
    </style>
</head>

<body>
    <h1 style="color:green;
               text-align: center;">  
            GeeksforGeeks  
        </h1>
    <h2>BOTH TEXTS ARE AT CENTER</h2>
    <div class="center">
        <p>This text is centered.</p>
    </div>
</body>

</html>

Output:

4. Vertical Align Using Padding:

The padding property can be used to center elements vertically.

Example 4: This example describes the CSS align using the padding property.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
    .center {
        padding: 70px 0;
        border: 3px solid green;
    }
    </style>
</head>

<body>
    <h1 style="color:green;
               text-align:center;">  
            GeeksforGeeks  
        </h1>
    <h2>Center Vertically</h2>
    <div class="center">
        <p>This is vertically centered.</p>
    </div>
</body>

</html>

Output:

5. Combined Vertical and Horizontal Align:

Combining padding and text-align: center to center elements both vertically and horizontally.

Example 5: This example describes the CSS align using the padding & text-align properties.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
    .center {
        padding: 70px 0;
        border: 3px solid green;
        text-align: center;
    }
    </style>
</head>

<body>
    <h1 style="color:green;">  
          GeeksforGeeks  
        </h1>
    <p>
        Here we use padding and 
        text-align to center the
        div element vertically 
        and horizontally:
    </p>

    <div class="center">
        <p>
          This text is vertically 
          and horizontally centered.
        </p>
    </div>
</body>

</html>

Output:

CSS alignment techniques provide a range of options for positioning elements and managing the space between them. Understanding and using properties like margin, position, text-align, and padding effectively can significantly enhance the layout and design of web pages.

Supported Browser:

  • Google Chrome 95.0
  • Microsoft Edge 95.0
  • Firefox 93.0
  • Opera 80.0
  • Safari 15.0

CSS Align – FAQs

What is CSS alignment?

CSS alignment refers to the positioning and distribution of elements within their container using CSS. It controls how content is laid out horizontally and vertically.

What CSS align method vertically centers content?

For vertical centering, CSS align techniques often involve using flexbox or grid display settings, which provide specific properties to align items perfectly in the middle.

How does the CSS align property align-items function?

The align-items property in CSS is crucial for vertical alignment within flexbox containers, determining the perpendicular alignment of items relative to their container’s cross axis.

What CSS align properties are used for text alignment?

For text alignment, the text-align property in CSS aligns text within its containing element, with options like left, right, center, and justify.

How can you center an element horizontally in CSS?

To center an element horizontally using CSS align methods, you might adjust margins or employ flexbox settings to precisely position the element in the center of its container.



Previous Article
Next Article

Similar Reads

Difference between align-content and align-items
Both align-content and align-items function on the cross-axis.Cross-axis in flexbox is dependent on the flex-direction and runs perpendicular to the main-axis, if flex-direction is either row or row-reverse then the cross-axis is vertical, if flex-direction is either column or column-reverse then the cross-axis is horizontal. align-content align-it
4 min read
How To Use Grid-Row-Align And Grid-Column-Align?
The grid-row-align and grid-column-align properties don't exist in CSS. Instead, CSS Grid uses properties like "align-items" and "justify-items" for controlling alignment across rows and columns, allowing precise positioning of grid items within their cells. so we will go to see how can we align the grid items. These are the following ways: Table o
3 min read
Primer CSS Flexbox Align Items
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Primer CSS Flexbox Align Items used to align items on the cross axis. The cross axis al
3 min read
CSS Layout - Horizontal & Vertical Align
The Layout in CSS is used to control the flow of element inside another element. It sets the position of element in the web page. The position of element can be set by using horizontal and vertical alignment. There are many ways to set the position of element which are listed below: Using Position Property: Use position property to absolute to set
4 min read
How to align objects vertically when working with grids in CSS ?
CSS grid layout is one of the strongest layout of CSS. The grid layout is 2-D which means it can handle both rows and columns, unlike flexbox which is 1-D. To align objects apply CSS to the parent element which becomes the grid container and the element's child which becomes the items in the grid. Approach: Use the align-content property of CSS gri
2 min read
Differences between HTML <center> Tag and CSS "text-align: center;" Property
If you are designing a simple web page, then not much difference is noticeable, but it is very necessary to understand the basic differences between these two. Since we are only concerned with the text, these elements do not have separate meanings.HTML <center> tag: The <center> tag in HTML is used to set to align of text into the cente
3 min read
How to align checkboxes and their labels on cross-browsers using CSS ?
For aligning the checkboxes or radio buttons with their labels can be achieved in many ways. Some of the simplest methods to achieve this are described below with proper code and output in different browsers. Now styling can be done in various ways to align the checkboxes and their labels. For this article, we are using internal stylesheet which is
2 min read
How to align text in CSS where both columns are straight?
Introduction: To align text in CSS there are lots of options available. Basically columns are straight always in CSS or in HTML, but here the columns are straight means that the gap between columns and the number of lines in each column should be equal. Approach: In CSS each column act as a box, so how alignment works and controlled is totally depe
2 min read
How to align the last line of a paragraph element to the right using CSS?
In this article, we will set the alignment of last paragraph element to the right by using CSS property. To set the alignment of last paragraph to right, we use CSS text-align-last property. The text-align-last is used to set the last line of the paragraph just before the line break. The line break may be due to the natural ending of a paragraph, o
2 min read
How to use the position property in CSS to align elements ?
In this article, we will learn how to use the position property in CSS to align elements. We can align elements using position property using CSS with some helper property of it. Approach: The position property is used to set the position of the element. We can align elements using position property with some helper property left | right | top | bo
2 min read
How to align item set to its default value in CSS ?
In this article, we will learn how to align an item set to its default value in CSS. Approach: The align-items property is used to set the alignment of items inside the flexible container or in the given window. It takes value. The initial value is used to set this property value to the default value. So we set the align-items property value to ini
2 min read
How to set default value to align content in CSS ?
In this article, we will learn how to set the default value to align content to its default value in CSS. The align-content property is used to align content within a specified container. Approach: The normal value of the align-content property is used to set the default value in CSS. It sets its default value to initial which is stretch for align-
2 min read
How to align image vertically in a division that spans vertically on the whole webpage using CSS ?
In this article, we will discuss the vertical alignment of images in a single division that spans vertically on the whole webpage. Vertical alignment of images refers to placing the images in a column. We often need to align the image vertically in order to display it properly on the mobile version or sometimes, we don't want to insert a horizontal
3 min read
Primer CSS Flexbox Align Self
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Primer CSS Flexbox Align Self classes are used to adjust the alignment of an individual
3 min read
Primer CSS Flexbox Align Content
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Primer CSS Flexbox Align Content is used to create multiple main axis lines and adds ex
4 min read
How to use text-align property inside a table in CSS ?
Text align is a CSS (Cascading Style Sheets) property that is used to specify the horizontal alignment of text within an HTML element. It is commonly used to align text within a block-level element such as a paragraph, heading, or table cell. The "text-align" property accepts several values. left: It aligns the text to the left edge of the element.
5 min read
How to Align Items to End Position using CSS ?
To align elements to the end, you can use the CSS property justify-content: flex-end; in a flexbox container, and for aligning elements to the end vertically, you can use the CSS property align-items: flex-end; in a flexbox container. This property aligns the flex items along the cross-axis (vertical axis) of the container. Here we are using these
2 min read
How to align a logo image to center of navigation bar using HTML and CSS ?
The navigation bar is an essential component of a website that helps users to navigate between different pages. It is usually located at the top of the page and consists of a list of horizontal links, logos, and other elements. The alignment of logos can be adjusted using CSS. In this tutorial, we will focus on how to align the logos to the center
2 min read
How to Align a Div to Middle (horizontally/width) of Page using CSS ?
We are going to see how we can align a div horizontally using CSS. Centering content both vertically and horizontally is one of the most important functions that needs to be done. Here horizontally align refers to the same amount of space to the left and right of the div inside the parent div. Three approaches can be used to achieve this layout and
5 min read
How to Vertically Align Text Next to an Image using CSS ?
Adding images into our websites is a common practice, and there are situations where text must be vertically aligned alongside an image. For example, a user’s name should appear immediately next to their profile picture, vertically aligned for optimal readability . In this article, we will see how to align text next to an image using various method
2 min read
How to align item to the flex-end in the container using CSS ?
Aligning items to the flex-end in a container using CSS helps create visually appealing layouts by ensuring content is positioned at the end of the container. To align items to the flex-end within a container using CSS, apply the justify-items: flex-end; property to the container. This shifts items to the end of the main axis, aligning them to the
3 min read
CSS text-align-last Property
The text-align-last property in CSS is used to set the alignment of the last line in a block or paragraph, just before the line break. This property can be applied to control how the final line of text is aligned within its containing element, whether it ends naturally or due to the use of a <br> tag. By leveraging the text-align-last propert
7 min read
How to align items at the center of the container using CSS ?
Centering items in a container using CSS involves aligning elements horizontally and vertically within a parent element. This can be done using different CSS properties and techniques, ensuring that the content appears visually centered and balanced within its container. Here are some common approaches Table of Content Centering with text-align, di
2 min read
How to align block elements to center using CSS ?
Block elements are known for taking up the full line space, forcing other elements to start on a new line. This means they have a width of 100% of the webpage or container holding the block. In this article, we will explore how block elements usually behave and how to center them using CSS. Table of Content Understanding Block ElementsHow to Center
3 min read
How to Center an Image using text-align Property in CSS ?
Aligning an image horizontally within a container can sometimes be challenging. In this article, we will explore how to center an image using the text-align property in CSS. Understanding the text-align PropertyThe text-align property in CSS is used to specify the horizontal alignment of text within an element. It can also be used to center inline
2 min read
How to vertically align text inside a flexbox using CSS?
Vertically aligning text inside a flexbox involves positioning the text centrally within the vertical space of a flex container. This can be achieved using CSS properties such as align-items and justify-content to control the vertical alignment and centering of the text. Here we have some common approaches: Table of Content Using align-items on the
2 min read
Tailwind CSS Align Content
This class accepts lots of values in Tailwind CSS. It is the alternative to the CSS Align Content property. This class is used for changing the behavior of the flex-wrap property. It aligns flex lines. It is used to specify the alignment between the lines inside a flexible container. This property defines how each flex line is aligned within a flex
4 min read
Tailwind CSS Align Self
This class accepts lots of values in tailwind CSS. It is the alternative to the CSS Align Self property. This class is used to control how an individual flex or grid item is positioned along its container's cross axis. Align Self Classes: self-auto self-start self-end self-center self-stretch self-autoThis class is used to inherit its parent contai
3 min read
Tailwind CSS Align Items
This class accepts a lot of values in tailwind CSS. It is the alternative to the CSS Align Items property. This class is used to set the alignment of items inside the flexible container or in the given window. It aligns the flex Items across the axis. The align-self class is used to override the align-items class. Align Items Classes: items-start i
3 min read
How to align div vertical across full screen in Tailwind CSS ?
You can easily align div vertical across the full screen using flex property in Tailwind CSS. Tailwind uses justify-center and items-center property which is an alternative to the flex-property in CSS. Syntax: <div class="flex h-screen justify-center items-center"> . . . </div>flex property: CSS ClassDescriptionh-screenMakes an element
2 min read
three90RightbarBannerImg