简体   繁体   English

CSS HTML Selector vs Class选择器

[英]CSS HTML Selector vs Class selector

Hello, I have seen a strange behavior while using CSS HTML selector and class selector. 您好,我在使用CSS HTML选择器和类选择器时看到了一个奇怪的行为。 in HTML file I have this code: 在HTML文件中我有这个代码:

<div class="content">
    <h1>Registration Form</h1>
</div>

And in Css file I have: 在Css文件中我有:

.content
{
    margin:auto;
    width:600px;
    border:solid 1px black;
    background-color:White;
    padding:10px;
}

h1
{
    color:Gray;
    font-size:18px;
    border-bottom:solid 1px orange;
}

Above code works perfectly. 上面的代码完美无缺。 When I changed h1 HTML selector to Class selector by writing .h and h1 class="h" STILL it worked perfect. 当我通过编写.hh1 class="h"将h1 HTML选择器更改为Class选择器时,它仍然完美无缺。

BUT when I changed .content class selector to div (ie I converted class selector of div tag to DIV HTML selector ITSELF) then the output changed. 但是当我将.content类选择器更改为div (即我将div标签的类选择器转换为DIV HTML选择器ITSELF)时,输出已更改。 It did not show me the text Registration form at all and showed horizontal lines above and below the area where Registration form text would be present. 它没有向我显示文本注册表格,并在注册表格文本所在区域的上方和下方显示水平线。 why is this strange behavior? 为什么这种奇怪的行为?

Does it proves that class selector and HTML selector behave differently even if applied with SAME style rule effect? 是否证明了类选择器和HTML选择器即使应用了SAME样式规则效果也会表现不同?

A class selector is more specific than a type selector. 类选择器比类型选择器更具体。

When you change the type selector to a class selector, the first selector still has precedence, just because it comes first. 当您将类型选择器更改为类选择器时,第一个选择器仍然具有优先权,因为它首先出现。

When you change the first class selector to a type selector, the second selector becomes more specific, and takes precedence. 将第一个类选择器更改为类型选择器时,第二个选择器将变得更具体,并且优先。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM