ul 嵌套列表第一层没有边距?
我想要一个样式列表,该列表在第一级上没有左边距。但在后续的分级子列表上有。有人知道如何做到这一点吗?
<ul>
<li>no margin</li>
<li>
<ul>
<li>yes margin</li>
</ul>
</li>
</ul>
(无需将 css 类添加到 ul 的第一级或更改任何代码)
I want a styled list that doesn't have a margin-left on the first level.. But does on subsequent, leveled sublists.. Anyone know how to do this?
<ul>
<li>no margin</li>
<li>
<ul>
<li>yes margin</li>
</ul>
</li>
</ul>
(without adding css classes to the first level of ul or changing any of the code)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您希望将其应用于应用样式表的每个页面上的所有
,则可以这样做:
但是如果您只想将其应用于某些列表,则需要添加一些内容HTML 来识别这些列表(正如其他答案所建议的那样)。
If you want this to apply to all
<ul>
s on every page the stylesheet is applied to, this works:But if you only want it to apply to certain lists, you need to add something to the HTML to identify those lists (as the other answers have suggested).
第一个 ul 是 body 的直系后代吗?我知道你说过不要在代码中添加 css 类,但我认为向样式表添加内容也不是问题?
如果是这样,您可以使用如下样式声明:
或者如果您知道它确实位于其中的 div:
您还可能需要考虑默认填充。
谢谢
Is the first ul a direct descendant of the body? I know you said not adding css classes to the code but I presume adding stuff to a stylesheet is not out the question?
If so you could use a style declaration like:
or if you know a div it does sit within:
There may also be default padding you need to consider.
Thanks
您可能应该清除主
的边距,然后为
设置边距/代码> 元素。
You should probably clear the margin of the main
<ul>
, and then set the margin for a<ul>
that's in an<li>
element.确实如此,但对 TommyBs 的答案有一个小小的补充:您需要应用在 html 列表项标记中使用的 id,如下所示:
在 css 中,相应的行将是
Indeed, but a small complementation to TommyBs's answer: you need to apply the id you use in the html list item tag, like so:
And in the css the corresponding line would be
就我而言,有默认边距,我将它们设置为 0
-webkit-margin-之前:0;
-webkit-margin-after:0;
-webkit-padding-start: 0;
In my case there was default margins, and I set them 0
-webkit-margin-before: 0;
-webkit-margin-after: 0;
-webkit-padding-start: 0;