
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
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.
Let’s see an example to center an element using CSS margin property −
Example
<!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>
Output
This will produce the following output
Let’s see another example to center an element using CSS margin property −
Example
<!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 class="screen screen1">Screen 70%</div> <div class="screen screen2">Screen 50%</div> <div class="screen screen3">Screen 30%</div> </div> </body> </html>
Output
This will produce the following output
- Related Articles
- The margin Shorthand Property in CSS
- Left and Right Alignment using the float Property in CSS
- Animate CSS margin property
- Animate CSS margin-bottom property
- Animate CSS margin-left property
- Role of margin property with value inherit using CSS
- Role of margin property with value auto using CSS
- Usage of margin property with CSS
- Usage of margin-top property with CSS
- Usage of margin-bottom property with CSS
- Usage of margin-right property with CSS
- Perform Animation on CSS margin-right property
- Set Text Alignment using CSS
- Setting Text Alignment using CSS
- How does auto property work in margin: 0 auto in CSS?

Advertisements