Introduction to Centering with CSS
Aligning elements in the center of a web page or container is a common task in web development. Whether you want to center a div, text, or any other element, CSS provides various techniques to achieve this. In this article, we will explore different methods to center elements using CSS, covering both horizontal and vertical alignment. By the end, you’ll have a solid understanding of how to center anything on your web page.
Centering Horizontally
Using Auto Margins
One of the simplest ways to horizontally center a block-level element, such as a <div>
, is by using auto margins. Here’s an example:
html
<div class="centered-div">Centered Content</div>
css
.centered-div {
margin-left: auto;
margin-right: auto;
}
In this example, the margin-left: auto;
and margin-right: auto;
CSS properties set the left and right margins of the <div>
element to auto. This causes the element to be horizontally centered within its parent container.
Using Flexbox
Flexbox is a powerful CSS layout module that provides flexible and responsive alignment capabilities. To center an element horizontally using flexbox, apply the following CSS to its parent container:
.container {
display: flex;
justify-content: center;
}
In this example, the display: flex;
property creates a flex container, and justify-content: center;
aligns the child elements horizontally at the center of the container.
Centering Vertically
Using Flexbox
Flexbox can also be used to center elements vertically. To center a single element vertically within its parent container, use the following CSS:
.container {
display: flex;
align-items: center;
}
In this example, the display: flex;
property creates a flex container, and align-items: center;
aligns the child elements vertically at the center of the container.
Using CSS Grid
CSS Grid is another powerful layout module that enables complex grid-based designs. To vertically center an element using CSS Grid, apply the following CSS to its parent container:
.container {
display: grid;
place-items: center;
}
In this example, the display: grid;
property creates a grid container, and place-items: center;
centers the child elements both horizontally and vertically within the container.
Centering Text
Using Text Align
To center text horizontally within a block-level element, you can use the text-align
property. Here’s an example:
html
<div class="centered-text">
<h1>Centered Heading</h1>
<p>Some text content here</p>
</div>
css
.centered-text {
text-align: center;
}
In this example, the text-align: center;
CSS property is applied to the <div>
element, causing all text content within the element to be centered horizontally.
Using Line Height and Vertical Align
To center text vertically within a container, you can combine the line-height
and vertical-align
properties. Here’s an example:
html
<div class="centered-text">
<p>Centered Text</p>
</div>
css
.centered-text {
height: 200px;
display:

My name is Mark Stein and I am an author of technical articles at EasyTechh. I do the parsing, writing and publishing of articles on various IT topics.
+ There are no comments
Add yours