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.
Similar Reads
CSS Tutorial
CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the main three components of a webpage along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
6 min read
CSS Introduction
CSS (Cascading Style Sheets) is a language designed to simplify the process of making web pages presentable. It allows you to apply styles to HTML documents by prescribing colors, fonts, spacing, and positioning.The main advantages are the separation of content (in HTML) and styling (in CSS) and the
5 min read
CSS Syntax
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML. Understanding CSS syntax is fundamental for creating visually appealing and well-structured web pages. Basic CSS SyntaxCSS is written as rulesets. A ruleset consists of a selector a
6 min read
CSS Selectors
CSS selectors are used to target HTML elements on your pages, allowing you to apply styles based on their ID, class, type attributes, and more. There are mainly 5 types of selectors. Basic CSS Selectors: These are used to target elements by tag, .class, or #id for fundamental styling needs.Combinato
9 min read
CSS Comments
CSS comments are used to add notes or explanations to your code, helping you and others understand it better. They start with /* and end with */ and can be used for both single-line and multi-line comments. Note: Comments are ignored by browsers, so they wonât affect how your webpage looks or works.
2 min read
CSS Colors
CSS colors are used to set the color of different parts of a webpage, like text, background, and borders. This helps make the page look more attractive and easier to read. You can define colors using names, hex codes, RGB values, and more. You can try different formats of colors here- #content-ifram
6 min read
CSS Borders
Borders in CSS are used to create a visible outline around an element. They can be customized in terms of Width: The thickness of the border.Style: The appearance of the border (solid, dashed, dotted, etc.).Color: The color of the border.You can try different types of borders here- #custom-iframe{ h
6 min read
CSS Margins
CSS margins are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and readability. Syntax:body { margin: value;}[GFGTABS] HTML <!--Driver C
4 min read
CSS Height and Width
Height and Width in CSS are used to set the height and width of boxes. Their values can be set using length, percentage, or auto. Width and HeightThe width and height properties in CSS are used to define the dimensions of an element. The values can be set in various units, such as pixels (px), centi
4 min read
CSS Outline
CSS outline is a property used to draw a line around an element's border. It does not affect the layout, unlike borders. It's often used to highlight elements, providing a visual emphasis without altering the dimensions of the element. Syntaxselector{ outline: outline-width outline-type outline-colo
4 min read