【问题标题】:Trying to center UL试图以 UL 为中心
【发布时间】:2015-06-28 19:26:38
【问题描述】:

我正在尝试正确居中 UL。即使我使用了 display:table 和正确的 margin: 0 auto... 仍然无法正常工作。它不去中心。

我做错了什么?

UL 和 LI 的 CSS

.container { 
    position: relative; 
    display:table; 
    margin-left:auto;
    margin-right:auto;
}


.container li {
    margin: 30px 30px;
    padding: 0;
    display: inline;
    clear: none;
    float: left;
    width: 310px;
    height: 370px;
    position: relative;
    list-style: none;
}

这是现场版。调整页面时,您会注意到 UL 停靠在页面的左侧。

更新 http://codepen.io/mariomez/pen/doJvJz?editors=010

注意:问题基本解决了。唯一的问题是 LI 组件也是居中对齐的,这对我来说在视觉上并不好。如何将它们向左对齐并保持 UL 居中?

【问题讨论】:

    标签: css alignment html-lists centering


    【解决方案1】:

    .containertext-align: center; 放在一起,禁用浮动列表元素并为它们使用display: inline-block; 而不是display: inline;

    .container {
        margin-left: auto;
        margin-right: auto;
        padding: 0;
        text-align: center;
    }
    
    
    .container li {
        margin: 30px 30px;
        padding: 0;
        display: inline-block;
        clear: none;
        width: 310px;
        height: 370px;
        position: relative;
        list-style: none;
    }
    

    演示:JSFiddle

    【讨论】:

    • 非常感谢!唯一的问题是 li 组件也与中心对齐,这对我来说在视觉上并不好。我怎样才能将它们对齐到左侧并且仍然使整个 UL 居中?
    • 那你的目标是什么?您想要将一个元素 (<ul>) 居中,该元素的宽度为窗口的 100%,因此它通常永远不会真正居中。你不能居中一个元素,它有 100% 的窗口宽度。
    • 类似这样的东西:stackoverflow.com/questions/3355058/…
    • 我尝试应用该解决方案,但它弄乱了隐藏元素。
    • 我的目标是让 ul 列表居中 - 与页面相关......但让 li 向左对齐 - 与 ul 相关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2019-01-06
    • 2017-01-01
    • 2020-02-19
    • 2017-01-03
    • 1970-01-01
    相关资源
    最近更新 更多