
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Center Alignment Using the Margin Property in CSS
We can center horizontally a block-level element by using the CSS margin property, but CSS width property of that element should be set. The auto value is set for the margin property.
Let's see some examples to center an element using the CSS margin property −
Create a Symbol and Align to the Center
In this example, we have created a symbol with CSS. The alignment is done to the center using the margin property with value auto −
div { width: 50%; margin: 10px auto; border:4px solid black; }
Example
Let us see an example to align a symbol to the center using the margin property −
<!DOCTYPE html> <html> <head> <title>Center Alignment using CSS Margin</title> <style> #yinyangSymbol { width: 100px; height: 50px; background: #fff; border-color: #000; border-style: solid; border-width: 2px 2px 50px 2px; border-radius: 100%; position: relative; } #yinyangSymbol::before { content: ""; position: absolute; top: 50%; left: 0; background: #fff; border: 18px solid #000; border-radius: 100%; width: 14px; height: 14px; } #yinyangSymbol::after { content: ""; position: absolute; top: 50%; left: 50%; background: #000; border: 18px solid #fff; border-radius:100%; width: 14px; height: 14px; } div{ width: 50%; margin: 10px auto; border:4px solid black; } #text { border: 4px solid black; background-color: grey; color: white; text-align: center; } </style> </head> <body> <div id="main"> <div> <div id="yinyangSymbol"></div> </div> <div id="text">Be Centered & Balanced</div> </div> </body> </html>
Let's see another example to center an element using CSS margin property −
Center Divs to the Center
In this example, we have centered div using the margin property with the value auto. We have 3screens i.e. div −
<div class="screen screen1">Screen 70%</div> <div class="screen screen2">Screen 50%</div> <div class="screen screen3">Screen 30%</div>
All of the above are aligned using the margin property −
.screen { padding: 10px; margin: 10px auto; text-align: center; color: white; border-radius: 0 0 50px 50px; border: 4px solid #000; }
Example
Let us see an example to center align divs on an HTML web page −
<!DOCTYPE html> <html> <head> <title>Center Alignment using CSS Margin</title> <style> .screen{ padding: 10px; margin: 10px auto; text-align: center; color: white; border-radius: 0 0 50px 50px; border: 4px solid #000; } .screen1 { background-color: #f06d06; width: 70%; } .screen2 { background-color: #48C9B0; width: 50%; } .screen3 { background-color: #DC3545; width: 30%; } </style> </head> <body> <div> <div class="screen screen1">Screen 70%</div> <div class="screen screen2">Screen 50%</div> <div class="screen screen3">Screen 30%</div> </div> </body> </html>