如何将“位置:绝对”元素居中我在对具有属性的元素的中心设置一个问题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="img/dummy1.JPG" alt="Dummy 1" /></li> <li><img src="img/piano_unlicened.JPG" alt="Dummy 2" /></li> </ul> </div></body>
3 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
width
/height
.child { position: absolute; top: 50%; /* position the top edge of the element at the middle of the parent */ left: 50%; /* position the left edge of the element at the middle of the parent */ transform: translate(-50%, -50%); /* This is a shorthand of translateX(-50%) and translateY(-50%) */}
解释
top
/left
50%
translate()
-50%
top
/left
translate()
translateX(-50%)
translateY(-50%)
position
static
relative
, absolute
, fixed
慕工程0101907
TA贡献1887条经验 获得超5个赞
absolute
ul#slideshow li { position: absolute; left:50%; margin-left:-20px;}
margin-left
添加回答
举报
0/150
提交
取消