How to Center a DIV Horizontally and Vertically Using Flexbox in CSS
justify-content
align-items
You can simply use the CSS justify-content and the align-items properties with the value center to horizontally and vertically center a <div> using flexbox in CSS.
center
<div>
Let's try out the following example to understand how it basically works:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Flexbox: Center Horizontally and Vertically</title> <style> .flex-container { margin: 30px; min-height: 300px; display: flex; border: 1px solid black; justify-content: center; /* align item horizontally */ align-items: center; /* align item vertically */ } .item { width: 100px; padding: 20px; background: #b4bac0; } </style> </head> <body> <div class="flex-container"> <div class="item">Centered Item</div> </div> </body> </html>
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Use CSS
justify-contentandalign-itemsPropertyYou can simply use the CSS
justify-contentand thealign-itemsproperties with the valuecenterto horizontally and vertically center a<div>using flexbox in CSS.Let's try out the following example to understand how it basically works:
need an explanation for this answer? contact us directly to get an explanation for this answer