How to center div?

前端 未结 6 437
抹茶落季
抹茶落季 2021-01-19 02:01

I have a problem with centering div in HTML (vertical & horizontal). My code looks something like this:

SOME HTML
6条回答
  •  忘掉有多难
    2021-01-19 02:27

    You could use:

    #container {
        // Your other values, but remove position: absolute;
        margin: 0 auto;
    }
    

    Alternatively, you can do:

    #wrapper, #container {
        border: 1px solid red;
        height: 500px;
        width: 600px;
    }
    
    #wrapper {
        bottom: 50%;
        right: 50%;
        position: absolute;
    }
    
    #container {
        background: yellow;
        left: 50%;
        padding: 10px;
        position: relative;
        top: 50%;
    }
    

    And you're HTML code:

    Centered Div

    This div has been centered within your browser window.

    That will center the

    in the middle of the browser window.

提交回复
热议问题