How to Align a Div to Middle (horizontally/width) of Page using CSS ?
Last Updated :
06 Mar, 2024
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 both of them are listed and demonstrated below.
But first, Let's create a div to place the div into the middle (horizontally) we need to create a fixed-size div and give a background color so that it can be visible. Set the main container width to 100% so the div can be moved around into the container.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to align div to the middle
(horizontally/width) of the page?
</title>
<style>
.container {
height: 200px;
width: 100%;
margin: 0px 15px;
}
.innerDiv {
background-color: black;
width: 400px;
height: 75px;
}
</style>
</head>
<body>
<div class="container">
<div class="innerDiv">
<p> Example to center div horizontally
</p>
</div>
</div>
</body>
</html>
Now that we have seen the structure of the code, let's understand the different approaches to center the div horizontally.
Horizontally Center a Div Using CSS: Flexbox approach
This approach involves the usage of one of the display properties of CSS which is flex. The parent div which will contain the child div which will be centered needs to have its display set to flex and then must have its content justified to center using the justify-content: center property.
Syntax:
parentDiv {
display: flex;
justify-content: center;
}
childDiv {
// Width and height properties
}
Example: The code demonstrates how we can use the display property approach to get a div and event the content inside it to be horizontally centered in the div.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to align div to the middle
(horizontally/width) of the page?
</title>
<style>
.container {
display: flex;
justify-content: center;
/* The properties below are
for better representation */
height: 200px;
width: 100%;
margin: 0px 15px;
}
.innerDiv {
background-color: black;
width: 400px;
height: 75px;
color: azure;
display: flex;
justify-content: center;
}
</style>
</head>
<body>
<div class="container">
<div class="innerDiv">
<p>
This div is at the center,
using display property
</p>
</div>
</div>
</body>
</html>
Output:
Horizontally Center a Div Using CSS: Flexbox MethodExplanation:
We used the display: flex on the parent container (.container) to create a flexible layout. The justify-content: center property centers the child <div> horizontally within the parent and adjusts the .innerDiv dimensions and styling as needed.
Center a Div Horizontally with Margin Auto CSS Styling
Here to make the div horizontally centered the margin in the x-axis needs to be auto which means the margin to the left and right of the div to be the same so put it in the center. Also if you want to make the div vertically centered then make the margins in the x and y axis auto and it will put the div in the center of the parent container.
Syntax:
div {
margin: 0 auto;
// Add your code
}
Example: The code demonstrates how we can use the margin auto technique to center the div horizontally.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to align div to the middle
(horizontally/width) of the page?
</title>
<style>
.container {
margin: 0 auto;
background-color: darkolivegreen;
width: 300px;
height: 75px;
color: azure;
}
p {
/* The inner text of the
div is centered */
padding-top: 1.5rem;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<p>
This div is at the center
using Margin Auto
</p>
</div>
</body>
</html>
Output:
Center a Div Horizontally with Margin AutoExplanation:
We utilized the browser capability by using the CSS margin property, we set the margin value to auto so the browser can adjust with the available space and place the div into the center of the viewport.
Horizontal Centering div using Position Property
This approach includes the usage of the position properties of CSS to make horizontally centered our div. we will make the position of the container absolute.
Syntax:
.container{
position: absolute;
left: 50%;
transform: translate(-50%);
}
Example: The code demonstrates how we can use the position property approach to horizontally center a div.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to align a div to the
middle (horizontally/width)
of the page?
</title>
<style>
.container {
position: absolute;
left: 50%;
transform: translate(-50%);
background-color: darkslategrey;
width: 300px;
height: 75px;
color: whitesmoke;
}
p {
/* The inner text of the
div is centered */
padding-top: 1rem;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<p>
This div is at the center
using Position
</p>
</div>
</body>
</html>
Output:
Position Properties for Horizontal CenteringExplanation:
We used position: absolute to position the <div> absolutely within its containing element and left: 50% places the left edge of the <div> at the horizontal center. At last the transform: translate(-50%) further centers the <div> by moving it back half of its width.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read