Am I using the best way to center a single div in HTML?


is it the best way to center ( horizontally and vertically) a single div in html file ? (no more content will be added to my html).

<!doctype html>
<html lang="fr">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="dropdown.css">
    <title>Dropdown menu</title>
<div class="container">
.container {
    height: 100vh;
    display: flex;
    align-items: center;

nav {
    display: flex;
    justify-content: center;
    margin: auto;
    background: red;
    width: 30%;
    height: 30%;


There are a lot of ways to center something in HTML whether it be a single item or multiple so you shouldn’t worry about if it is the best way.

If you want to read other ways to center stuff with CSS check out this link:

1 Like

You can use 5 simple code in any div to center it.

.container {

I forgot to say but I want my div responsive.

Actually for responsive issue you can use bootstrap or flexbox.

Try this one.

.div { 
margin: auto;
width: 50%