Vertically center ul in div

前端 未结 5 1041
忘掉有多难
忘掉有多难 2020-12-10 14:22

This is what my code looks like.

5条回答
  •  粉色の甜心
    2020-12-10 15:07

    "Centring" a div or other containers vertically is quite tricky in CSS, here are your options.

    You know the height of your container

    If you know the height of the container, you can do the following:

    #container {
        position: absolute;
        top: 50%;
        margin-top: -half_of_container_height_here;
    }
    

    So we essentially place in the middle and then offset it using a negative margin equal to the half of the height. You parent container needs to have position: relative.

    You don't know the exact height of your container

    In this case you need to use JavaScript and calculate the appropriate margins (unfortunately you cannot use margin-top: auto or something similar).

    More info here.

提交回复
热议问题