我有this ul包含四个li。我希望 li 在 ul 内居中,特别是为了响应目的。我添加了一些媒体查询,但这并没有使 ul 中的 li 居中。
@media only screen and (max-width : 589px) {
#gallery li {float:left; display:inline-block; margin: 0 auto; margin-bottom: 40px;}
}
@media only screen and (min-width : 590px) {
#gallery li {float:left; margin: 20px;}
}
最佳答案
如果你想对齐元素,去掉 float 。
#gallery {
width:100%;
margin:2em auto;
list-style: none;
position:relative;
text-align: center; /* <----- added text-align */ }
@media only screen and (max-width : 589px) {
#gallery li {
/* <----- removed float */
display:inline-block;
margin: 0 auto;
margin-bottom: 40px;}
}
关于html - CSS 在 ul 中水平居中 li,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41599296/