
- 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
How to center a <div> using the Flexbox property of CSS?
We can center a <div> in css using justify-content property and align-item property of flexbox. They are popular because they are responsive and easy to use. In this article, we shall learn how to center <div> using the Flexbox properties.
Use The align-items And The justify-content Property
The most popular method to center the div using the flexbox is the combination of justify-content and align-items properties of flexbox.
The justify-content property would center align the div horizontally.
The align-items property center aligns the div vertically.
If you want to center align only in one direction, i.e., either vertically or horizontally, we should use only one among the properties we mentioned.
Use the align-items and the justify-contents to center align the child horizontally and vertically.
Example
<!DOCTYPE html> <html lang="en"> <style> .parent{ border: 2px solid green; height:30vh; width:30vw; display: flex; flex-direction: row; justify-content: center; align-items: center; background-color: orange; } .child{ border: 2px solid red; height:10vh; width:10vw; background-color: yellow; } </style> </head> <body> <div class="parent"> <div class="child"> This is the child element. </div> </div> </body> </html>
In the code, the parent element is a green-bordered orange box with a height of 30% of the viewport height and a width of 30% of the viewport width.The parent is centered both horizontally and vertically with the help of "display: flex", "flex-direction: row", "justify-content: center" and "align-items: center."
To use the align-items and justify-content, we first need to declare the element to be a flexbox using the display property. The child element is a red-bordered yellow box with a height of 10% of the viewport height and a width of 10% of the viewport width, which is placed inside the parent element.
Conclusion
In this article, we have understood how to center a <div> using the flexbox property of CSS. We only need to use the justify-content and the align-content property of flexbox to achieve the same.
- Related Articles
- How to center a <div> using CSS grid Property?
- <p>When a <b>sportsman</b> runs, he often gets <b>muscle cramps</b>. Give reason.</p>
- How do I style a <select> dropdown with only CSS?
- How to align flexbox container into center using JavaScript?
- How to center a div on the screen using jQuery?
- How can I vertically center a div element for all browsers using CSS?
- Center alignment using the margin property in CSS
- How to change the href value of <a> tag after clicking on button using JavaScript?
- How to center a div within another div?
- Center Triangle at Bottom of Div in HTML with CSS
- How to Make a DIV 100% of the Window Height using CSS
- How to tell if tag <script> failed to load?
- How to specify the HTML content of the page to show in the <iframe> in HTML?
- Usage of CSS align-content property center value
- Usage of CSS align-items property center value
