Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个无序的垂直列表..
<div> <ul> <li>home</li> <li>portfolio</li> <li>about</li> <li>contact</li> </ul> </div>
CSS:
div { width: 600px; } ul { text-align: center; }
我真正想要的是文本左对齐的列表,而 ul 仍然水平居中。有办法吗?
您可以执行以下操作来居中动态生成的不同宽度的内容:
div { text-align: center; } ul { display: inline-block; } li { text-align: left; }
jsFiddle 示例
或者,您可以在父级上设置定义的宽度,并使用margin:0px auto,假设它是一个block级别元素。
margin:0px auto
block
ul { width: 100px; margin: 0px auto; }