这段CSS代码是什么意思?

发布于 2024-10-15 04:35:22 字数 2918 浏览 5 评论 0原文

我遇到了这段代码,但没有明白。

1>

#nav ul, 
#nav li:hover ul ul,
#nav li:hover li:hover ul ul,
#nav li:hover li:hover li:hover ul ul,
#nav li:hover li:hover li:hover li:hover ul ul{}

2>

#nav li:hover li:hover a.fly,
#nav li:hover li:hover li:hover a.fly,
#nav li:hover li:hover li:hover li:hover a.fly,
#nav li:hover li:hover li:hover li:hover li:hover a.fly{}

这是 html 代码:

<ul id="nav">
    <li class="top"><a href="#" class="top_link"><span>Home</span></a></li>
    <li class="top"><a href="#" id="products" class="top_link"><span class="down">Products</span></a>
        <ul class="sub">                
            <li><a href="#" class="fly">Cameras</a>
                <ul>
                    <li><a href="#">Nikon</a></li>
                    <li><a href="#">Minolta</a></li>
                    <li><a href="#">Pentax</a></li>
                </ul>
            </li>

            <li class="mid"><a href="#" class="fly">Lenses</a>
                <ul>
                    <li><a href="#">Wide Angle</a></li>
                    <li><a href="#">Standard</a></li>
                    <li><a href="#">Telephoto</a></li>
                    <li><a href="#" class="fly">Zoom</a>
                        <ul>
                            <li><a href="#">35mm to 125mm</a></li>
                            <li><a href="#">50mm to 250mm</a></li>
                            <li><a href="#">125mm to 500mm</a></li>
                        </ul>
                    </li>
                    <li><a href="#">Mirror</a></li>
                </ul>
            </li>

            <li><a href="#">Flash Guns</a></li>
            <li><a href="#">Tripods</a></li>
            <li><a href="#">Filters</a></li>
        </ul>
</li>
    <li class="top"><a href="#" id="services" class="top_link"><span class="down">Services</span></a>
        <ul class="sub">
            <li><a href="#">Printing</a></li>
            <li><a href="#">Photo Framing</a></li>
            <li><a href="#">Retouching</a></li>
            <li><a href="#">Archiving</a></li>
        </ul>
  </li>

</ul>

有人能告诉我上面 2 个 css 代码块解决了哪些区域吗?

谢谢

I came across this certain piece of code, and didnt get it.

1>

#nav ul, 
#nav li:hover ul ul,
#nav li:hover li:hover ul ul,
#nav li:hover li:hover li:hover ul ul,
#nav li:hover li:hover li:hover li:hover ul ul{}

2>

#nav li:hover li:hover a.fly,
#nav li:hover li:hover li:hover a.fly,
#nav li:hover li:hover li:hover li:hover a.fly,
#nav li:hover li:hover li:hover li:hover li:hover a.fly{}

And here is the html code:

<ul id="nav">
    <li class="top"><a href="#" class="top_link"><span>Home</span></a></li>
    <li class="top"><a href="#" id="products" class="top_link"><span class="down">Products</span></a>
        <ul class="sub">                
            <li><a href="#" class="fly">Cameras</a>
                <ul>
                    <li><a href="#">Nikon</a></li>
                    <li><a href="#">Minolta</a></li>
                    <li><a href="#">Pentax</a></li>
                </ul>
            </li>

            <li class="mid"><a href="#" class="fly">Lenses</a>
                <ul>
                    <li><a href="#">Wide Angle</a></li>
                    <li><a href="#">Standard</a></li>
                    <li><a href="#">Telephoto</a></li>
                    <li><a href="#" class="fly">Zoom</a>
                        <ul>
                            <li><a href="#">35mm to 125mm</a></li>
                            <li><a href="#">50mm to 250mm</a></li>
                            <li><a href="#">125mm to 500mm</a></li>
                        </ul>
                    </li>
                    <li><a href="#">Mirror</a></li>
                </ul>
            </li>

            <li><a href="#">Flash Guns</a></li>
            <li><a href="#">Tripods</a></li>
            <li><a href="#">Filters</a></li>
        </ul>
</li>
    <li class="top"><a href="#" id="services" class="top_link"><span class="down">Services</span></a>
        <ul class="sub">
            <li><a href="#">Printing</a></li>
            <li><a href="#">Photo Framing</a></li>
            <li><a href="#">Retouching</a></li>
            <li><a href="#">Archiving</a></li>
        </ul>
  </li>

</ul>

Can someone tell me what areas are addressed in the above 2 css code blocks ?

Thanks

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

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

发布评论

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

评论(3

柏林苍穹下 2024-10-22 04:35:22

我想说的是原始开发者,而不是你,没有得到它。

#nav ul, 
#nav li:hover ul ul,
#nav li:hover li:hover ul ul,
#nav li:hover li:hover li:hover ul ul,
#nav li:hover li:hover li:hover li:hover ul ul {

}

没有与 #nav li:hover li:hover li:hover li:hover ul ul 匹配的元素尚未与 #nav ul 匹配。对于另一组选择器也可以这样说。

在 CSS 中,逗号分隔的选择器列表意味着“将其应用于与这些条件中的任何匹配的所有元素”。

现在,对于这两个示例,顶部选择器还将匹配任何后续选择器匹配的任何元素,从而使它们全部冗余。以下选择器专门用于增加特异性。

如果每个选择器后面有不同的 CSS 块,那么代码就会有意义,尽管它会相当难看。我猜测这是基于在不同级别的树中使用不同样式的代码(例如控制文本缩进),然后将其重构为对所有选择器都相同的代码。

然后有人意识到,由于每个块中的样式相同,因此可以组合所有选择器,但没有意识到代码可以进一步重构,以简单地 #nav ul { ... }< /code>

我猜测空块 { } 实际上有一些样式,您为了可读性而忽略了这些样式?当然,如果它们完全是空的(如您的示例所示),则完全删除选择器是安全的。

I'd say it's the original developer, and not you, who's not getting it.

#nav ul, 
#nav li:hover ul ul,
#nav li:hover li:hover ul ul,
#nav li:hover li:hover li:hover ul ul,
#nav li:hover li:hover li:hover li:hover ul ul {

}

There's no element matched by #nav li:hover li:hover li:hover li:hover ul ul that isn't already matched by #nav ul. The same can be said about the other set of selectors.

A comma separated list of selectors, in CSS, means, "apply this for all elements that match any of these criteria".

Now, for both examples, the top selector will also match any element that is matched by any of the subsequent selectors, making them all redundant. The following selectors are exclusively of increasing specificity.

If there were different CSS blocks following each selector, then the code would make sense, althought it'd be rather ugly. I'm guessing that this is based on code that used different styles in different levels of tree (to control text indent, say), which was then refactored to code that can be the same for all selectors.

Somebody then realized that, since the styles in each block are the same, all the selectors can be combined, but didn't realize that the code could be refactored even further, to simply #nav ul { ... }

I'm guessing that the empty blocks { } actually had some styles in them, that you left out, for readability? Of course, if they were completely empty, as in your example, it'd be safe to remove the selectors entirely.

我ぃ本無心為│何有愛 2024-10-22 04:35:22

一个

    ,它是 id="nav" 的后代。

    ,它是

      的后代,它是具有鼠标的

    • 的后代在它上面,它是 id="nav" 的后代。

等等。

正如所指出的,第一行已经匹配其余部分匹配的所有内容。也许子关系 (>) 是指后代关系,而不仅仅是后代关系。

第二个同样是多余的。

A <ul> that's a descendant of something with id="nav".

or

A <ul>that's a descendant of a <ul>, that's a descendent of a <li> that has the mouse over it, that's a descendant of something with id="nav".

Etc.

As pointed out, the first line already matches everything that the rest of it matches. Perhaps the child relationship (>) was meant rather than just descendent.

The second one is similarly redundant.

丶情人眼里出诗心の 2024-10-22 04:35:22

firat 和秒也像查询一样,它们选择项目内的项目,

逗号分隔符允许您为分隔的组添加相同的样式
唯一的空间允许您查找节点中的 shildren 以及为父级中的节点指定的样式

ul

这表示您选择每个 UL 标签

ul li

这表示您仅选择 UL 标签中包含的 li 标签

ul li a

这表示您仅选择a 标签包含在指定的 li tgas 中,该标签包含在 ul 标签中,这不会选择任何叶子中没有 li 父代的 a 标签。例如,这不会选择

但会选择

:hover 是一个指定的事件触发器,当您将鼠标悬停在项目上时会调用该事件。

# 运算符表示您搜索 ID 属性标签
您搜索带有类 stag 的项目的点 . 运算符

不要忘记只有一个项目具有相同的 id ,剪切几个项目可以具有相同的类,并且一个项目可以有 2 个或更多 不同类

添加 id 的

<div id="important-div"></div>

:这意味着该 div 使用 important-div 标识符进行标识,您可以使用该名称查找该节点。就像您的信用卡号码一样,没有人拥有与您相同的号码。

添加 calssnames

<a class="clickable redborder nomargin"></a> 

这意味着你的标签有 3 个类名,你可以使用可点击的类选择器添加样式,也可以使用其他类选择器添加样式

the firat and the seconds also like a query they select items inside an item

the comma separator allows you to add the same style for separated groups
the only space allows you to seek after shildrens in the node and the style specified for the node in the parent

ul

this saysyou select every UL tag

ul li

this says you select only the li tags contained within an UL tag

ul li a

this says you select only the a tags wich contained in a specified li tgas which are contained in an ul tag this will not select the a tags which dont have li parent in any leaf. for example this will not select <body><a></a></body>

butthis will select the <ul><li><div><a></a></div></li></ul>

the :hover is a secified event trigger, which is called when you mouse over the item.

the # operator is says you search for an ID attributed tag
the point . operator you search for an item wich attributed with the class stag

dont forget only one item has tha same id , cut couple of items can have the same class, and one item can have 2 or more different class

adding an id:

<div id="important-div"></div>

this mean the div is identificated with the important-div identificator you can fint this node with this name. like your credit card numer, no one has the same as you.

adding calssnames

<a class="clickable redborder nomargin"></a> 

this means you a tag has 3 classname, you can add styles with the clickable class selector, and with the others also

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