How to center div in the page? (Height & Width)?

后端 未结 2 1440
星月不相逢
星月不相逢 2021-01-24 00:40

silly question, but I can\'t find answer on it as all answers assume centering div in terms of width.

What I need is to center div in means of height and width so in a v

相关标签:
2条回答
  • 2021-01-24 01:07
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -250px; /*(half of width)*/
    margin-top: -250px;  /*(half of height)*/
    

    that should work.

    0 讨论(0)
  • 2021-01-24 01:10

    Create a wrapper with ID wrapper around the #myDiv element, and apply this CSS code:

    #wrapper{
        display: table;
    }
    #myDiv{
        display: table-cell;
        vertical-align: middle; /*Vertically centered*/
        text-align: center; /* Horizontally centered */
    }
    

    This code centers elements of any width/height.

    0 讨论(0)
提交回复
热议问题