How to center a div using css

  • 1 min read

There are multiple ways to center a div, among them here are given an example of how to place a div in the center within a page.

Example 1:

div {
    width: 100px;
    height: 100px;
    background-color: red;

    position: absolute;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;

    margin: auto;
}

Example 2:

div {
    width:100%;
    margin:0 auto;
}

Hope it helps!

Leave a Reply