margin-left: auto; margin-right: auto
当容器是 static
或 relative
定位时,水平居中的方法效果很好,但当它是 absolute< 时似乎失败了
定位。
有没有办法让 auto
边距在这种情况下起作用?出于某些原因,我需要容器为 absolute
。
最佳答案
绝对定位,根据定义,将使用 0,0 作为您定位的引用点。
我的建议是使用 position:absolute 和宽度为 100% 的容器,然后使用 position: 0 auto 指定任何你想要居中的内容。
关于css - 位置 : absolute element 内的水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13665870/