项目符号列表项是否总是缩进 1.8em?

发布于 2024-12-14 07:23:27 字数 1331 浏览 0 评论 0原文

我制作了一个纯 CSS 下拉菜单。要求是有一个水平的项目栏,每个项目都可以下拉一个垂直的菜单。此外,这些项目不应删除第三级菜单,而应仅显示项目符号列表。我的 html 有三个嵌套的 ul 并且菜单在所有现代浏览器中都能完美运行。它看起来像这样:

original

但是,我不喜欢链接后面的深色框从项目符号的右侧开始,并且不会延伸到整个菜单宽度,所以我玩了一下,最后进行了这个调整:

#nav li ul li ul li a {
    padding-left:1.8em;
    margin-left:-1.8em;
}

现在项目符号菜单项看起来就像我想要的:

working

由于 em 相对于字体大小的性质,它的工作方式与字体大小无关,如此处使用较大字体大小所示:

working (larger font)

我在 Internet Explorer 8+9+10(开发者预览版)、Firefox 3+7、最新的 Chrome、Opera 和 Safari 上对此进行了测试它就像一个魅力。

但是,我只是不明白为什么正是 1.8em 完成这项工作。为什么每个浏览器都会将项目符号项目缩进这么远?我在互联网上搜索了这个主题,但没有发现任何有用的信息。我可以确定这适用于未来的浏览器吗? HTML 标准中是否指定了 1.8em?

预先感谢您的任何提示!

编辑:

DisgruntledGoat的答案:如果我使用1em/-1em或20px,它将不起作用/-20 像素。使用这种样式:

#nav li ul li ul li a {
    padding-left:20px;
    margin-left:-20px;
}

我得到不同字体大小的结果(显然不随字体大小缩放):

pixel based

类似地, 1em/-1em 也关闭,看起来如上图右侧所示,但随字体大小缩放。由于某种原因,1.8em 看起来仍然是神奇的距离......

I made a css-only dropdown menu. The requirement was to have a horizontal bar of items that can each drop down a vertical menu. Furthermore, those items should not drop a tertiary menu, but instead just show bulleted lists. My html has three nested ul and the menu is working perfectly in all modern browsers. It looks like this:

original

However, I did not like how the darker box behind the link is starting right of the bullet and does not stretch over the whole menu width, so I played around a bit and finally came to this tweak:

#nav li ul li ul li a {
    padding-left:1.8em;
    margin-left:-1.8em;
}

Now the bulleted menu item looks just like I wanted:

working

And due to the nature of em beeing relative to the font size, it works independently of the font size, like shown with a larger font size here:

working (larger font)

I tested this on Internet Explorer 8+9+10(developer preview), Firefox 3+7, latest Chrome, Opera and Safari and it works like a charm.

However, I just dont understand why it is exactly 1.8em that does the job. How come every browser indents the bullet items exactly this far? I searched the internet on this topic, but I did not find anything helpful. Can I be sure this works on future browsers? Are those 1.8em specified in the HTML standard?

Thanks in advance for any hint!

Edit:

To DisgruntledGoat's answer: It would not work if I used 1em/-1em or 20px/-20px. With this style:

#nav li ul li ul li a {
    padding-left:20px;
    margin-left:-20px;
}

I get this (obviously not scaling with the font size) result for different font sizes:

pixel based

Similarly, 1em/-1em is also off and looks like on the right in the picture above but scaling with the font size. It still looks like 1.8em is the magic distance for some reason...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云仙小弟 2024-12-21 07:23:27

根据您的代码,您已经设置了 ul,使其没有边距或填充。但是,您已经设置了 li,使其具有 margin-left: 1.8em

#nav li ul li ul li {
    display: list-item;
    margin-left:1.8em;
    list-style:disc outside none;
}

#nav li ul li ul li {
    margin-left: 1.8em;
    padding-left: 0;
}

就是这样。

Given your code, you've set up your ul such that it has no margin or padding. However, you've set up your li's such that they have margin-left: 1.8em:

#nav li ul li ul li {
    display: list-item;
    margin-left:1.8em;
    list-style:disc outside none;
}

#nav li ul li ul li {
    margin-left: 1.8em;
    padding-left: 0;
}

And there it is.

弃爱 2024-12-21 07:23:27

您绝对应该重置 CSS,然后按照您需要的方式设置属性。永远不要相信浏览器是一致的。它增加了一些编码,但同时也为您的代码提供了面向未来的证明。

You should definitely do a CSS reset and then set the properties the way you need them. Never trust browsers to be consistent. It adds a bit of coding but at the same time future proofs your code.

2024-12-21 07:23:27

基于多年来不一致的浏览器 - 我想说你不能相信它们永远是一致的。最好的选择就是自己强行控制。

您可以通过同时设置 li 的 padding-left 和 margin-left 来使用它。例如:

li {
  margin-left: 1.8em;
  padding-left: 0;      
}

显然一些(主要是较旧的)浏览器使用填充和一些边距 - 所以一定要设置两者。

Based on many years of inconsistent browsers - I'd say you can't trust them to ever be consistent. The best option is to forcibly control it yourself.

You can use that by simultaneously setting the padding-left and margin-left of an li. eg:

li {
  margin-left: 1.8em;
  padding-left: 0;      
}

Apparently some (mainly older) browsers use padding and some margin - so be sure to set both.

最舍不得你 2024-12-21 07:23:27

要回答问题和您的评论:您的解决方案有效,因为您用完全相同的边距大小否定了填充。但随着字体大小的增大,列表左侧的间距也会变大。使用 1em 填充和 -1em 边距或 20px 和 -20px 会得到相同的结果。

正如我在评论中提到的,列表的实际默认填充是 40px。更令人困惑的是,在检查用户代理样式表(在 Chrome 开发工具和 Firefox 的 Firebug 中)时,它们会报告独特的 CSS 属性:-webkit-padding-start-moz-padding -start 分别。

我假设这些特殊属性用于代替常规填充,因为列表是 HTML 中的特殊情况 - 它们具有不计入填充的悬挂项目符号/数字。

To answer the question and your comment: your solution works because you negate the padding with the exact same size margin. But the spacing to the left of the list is larger with the larger font size. You would get the same result with 1em padding and -1em margin or 20px and -20px.

As I mentioned in the comment, the actual default padding for lists is 40px. To make things even more confusing, on checking the user agent stylesheets (in Chrome Dev Tool and Firebug for Firefox) they report unique CSS properties: -webkit-padding-start or -moz-padding-start respectively.

I assume that these special properties are used in place of regular padding due to lists being a special case in HTML - they have hanging bullets/numbers that don't count in the padding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文