我在将属性position设置为absolute的元素居中时遇到问题。有人知道为什么图像没有居中吗?
body {
text-align: center;
}
#slideshowWrapper {
margin-top: 50px;
text-align: center;
}
ul#slideshow {
list-style: none;
position: relative;
margin: auto;
}
ul#slideshow li {
position: absolute;
}
ul#slideshow li img {
border: 1px solid #ccc;
padding: 4px;
height: 450px;
}<body>
<div id="slideshowWrapper">
<ul id="slideshow">
<li><img src="https://source.unsplash.com/random/300*300?technology" alt="Dummy 1" /></li>
<li><img src="https://source.unsplash.com/random/301*301?technology" alt="Dummy 2" /></li>
</ul>
</div>
</body>
发布于 2013-08-28 19:17:46
如果您设置了宽度,则可以使用:
position: absolute;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
text-align: center;发布于 2011-12-15 00:52:50
在CSS中,将absolutely定位的内容居中相当复杂。
ul#slideshow li {
position: absolute;
left:50%;
margin-left:-20px;
}将margin-left更改为试图居中的元素宽度的一半(负数)。
发布于 2014-01-28 18:22:42
垂直和水平对齐中心的分割
top: 0;
bottom: 0;
margin: auto;
position: absolute;
left: 0;
right: 0;注意:要设置元素的宽度和高度
https://stackoverflow.com/questions/8508275
复制相似问题