How to horizontally center a div using CSS?
To Horizontaly centered the <div> Element :
- We can use the property of margin set to auto i.e margin:auto;.
- The <div> Element takes up its specified width and divide equally the remaining space by the left and right margins.
Example
<!DOCTYPE html> <html> <head> <title> How to horizontally center a div using CSS </title> <style> div { width:300px; margin:auto; border: 3px solid green; color:red; } h1, h2{ text-align:center; color:green; font-style:italic; } </style> </head> <body> <h1>GeeksForGeeks</h1> <h2>margin:auto</h2> <div> It is a computer science portal for Geeks. GeeksForGeeks ia a good website for learning computer science; </div> </body> </html> |
chevron_right
filter_none
Output:

Supported Browsers: The browser supported by margin:auto property are listed below:
- Apple Safari
- Google Chrome
- Firefox
- Opera
- Internet Explorer
Recommended Posts:
- How to make an image center-aligned (vertically & horizontally) inside a bigger div using CSS?
- HTML | center Tag
- How to vertically center text with CSS?
- How to make elements float to center?
- How to center a popup window on screen?
- Graph measurements: length, distance, diameter, eccentricity, radius, center
- How to disable tabs in Bootstrap ?
- Page Object Model (POM)
- Difference between background and background-color
- How to get card vertically centered between search bar and bottom of the viewport?
- What is the difference between parseInt() and Number()?
- How to display bootstrap carousel with three post in each slide?
- HTML | <iframe> name Attribute
- HTML | <map> name Attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



