# CSS
14 August, 2023

How to center a div 😄

background

There are many way to center a div

1. Using flexbox

 .parent {
    display: flex;
    justify-content: center;
    align-items: center;
 }

2. Using grid

.parent {
    display: grid;
    place-content: center;
}

3. Using Position

.parent {
    position: relative;
}

.child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

4. Using Flex & Margin

.parent {
    display: flex;

}

.child {
    margin: auto;
}

5. Using Grid & Margin

.parent {
    display: grid;
}

.child {
    margin: auto;
}

I think the best way to center a div is #1 and the easiest is #2, hope you like that 😄.