极客时间返利平台,你可以在上边通过山月的链接购买课程,并添加我的微信 (shanyue94) 领取返现。

# 对一个非定长宽的块状元素如何做垂直水平居中

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 284 (opens new window)

Author

回答者: huxiamei (opens new window)

flex 布局

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

css position

        .container {
            position: relative;
        }
        .container .item {
            width: 100px;
            height: 50px;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

css position

        .container {
            position: relative;
        }
        .container .item {
            width: 100px;
            height: 50px;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

非定宽了,你这个不行呀

css position

        .container {
            position: relative;
        }
        .container .item {
            width: 100px;
            height: 50px;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

非定宽了,你这个不行呀

面试官说,做题前要好好读题。😭

Author

回答者: jkLennon (opens new window)

https://github.com/shfshanyue/Daily-Question/issues/10

Last Updated: 2/15/2022, 12:06:24 PM