How can I make a centered, left-aligned, variable-width bullet list with CSS?

*爱你&永不变心* 提交于 2020-01-04 06:27:50

问题


Is it possible somehow to center a left-aligned list? Like, I want the list itself centered, but want the bullet points to line up below each other.

I know I could sort of do this if I give the list a fixed width and margin auto, but I really don't want to set a width on it since I don't know how wide the content will be.


回答1:


Use display: inline-block on the ul, and text-align: center on a parent element.

See: http://jsfiddle.net/thirtydot/E3Yjr/

CSS:

.list-container {
    text-align: center;
}
ul {
    display: inline-block;
    text-align: left;
}

HTML:

<div class="list-container">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item Looooooooooooooooooooooooonggg</li>
    </ul>
</div>

If you need it to work in IE7, here's how: http://jsfiddle.net/thirtydot/E3Yjr/1/

display: inline-block;
*display: inline;
zoom: 1;


来源:https://stackoverflow.com/questions/9018686/how-can-i-make-a-centered-left-aligned-variable-width-bullet-list-with-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!