我想在窗口中央放置一个 div
(带有 position:absolute;
)元素。但是我在这样做时遇到了问题,因为宽度未知。
我尝试了以下 CSS 代码,但需要进行调整,因为 width 是响应式的。
.center {
left: 50%;
bottom: 5px;
}
我怎样才能做到这一点?
最佳答案
这对我有用:
#content {
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
width: 100px; /* Need a specific value to work */
}
<body>
<div>
<div id="content">
I'm the content
</div>
</div>
</body>
关于css - 如何在 div 中将绝对定位的元素居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43528666/