What is the difference between defining CLASS in CSS with spaces and without spaces?

黄舟
Release: 2017-07-22 09:42:04
Original
2740 people have browsed it

.example .pp{  color: orange;
}.example.pp2 {  color: green;
}
Copy after login

For example, the two definitions above are one with spaces in the middle and the other without spaces in the middle.

第一个class要这样写生效:<p class="example">文字文字<span class="pp">pp这个class生效</span>....</p>
第二个class要这样写生效:<p class="example pp2">pp2这个class生效</p>
Copy after login

Excuse me, why is this? ?

.example .pp = E F is the descendant selector.

.example.pp2 is on an element, and this element will have an effect only if it includes these two classes.

There is a difference between the two, a big difference!

.example .pp is separated by a space, indicating the descendant selector, and the .pp in .example is selected.
For example:

<div class="example">
    <div class="pp">被选择的元素</div>
</div>
Copy after login

.example.pp selects elements that contain both example and pp in class.
For example:

<div class="example pp">
被选择的元素
</div>
Copy after login

This is the format of the css selector, which stipulates that the selection conditions without spaces are "and" relationships, and those with spaces are "parent-child" relationships, and can be indirect " "Father and son" relationship

<style>
.e1.e2{
background-color:yellow;
}
.father .e3{
background-color:green;
}
</style>
</head>
<body>
<div class="father">
<p class="e1 e2">我住在 Duckburg。</p>
<p class="e3">我也住在 Duckburg。</p>
<div>
<p class="e3">他也住在 Duckburg。</p>
</div>
</div>
</body>
Copy after login

The first paragraph p in the above code has two classes, e1 and e2. Note that the spaces in the class attribute and the spaces in css have different meanings. The spaces in the class attribute are "and " relationship, and the space in the css just mentioned is the relationship of "parent and descendant".

The first p is rendered in yellow because css uses the .e1.e2 selector to set this color for it. That is, this paragraph has both classes. Of course, the same effect can be achieved by using only .e1 instead of .e1.e2 in CSS, but sometimes we don’t want the CSS selector to accidentally hit other tags, so we just write it in detail.

The second p is rendered green by .father .e3 because the class of p is e3 and it is the descendant of the tag with class father, so it is dyed green.

Similarly, the third p, although not a direct subclass of father, also turns green, indicating that this selector with spaces can include indirect descendants.

The difference has been explained clearly above. Let’s talk about why sometimes multiple class selectors are needed to select an element.
1. The Chinese translation of css is cascading style sheet, and its style is based on Inherit and override to produce the final style.
2. There is a set of rules for CSS style weight calculation. The one with the greater weight is the final style, so sometimes in order to overwrite the previous style, we increase the weight by adding a class selector to represent this element.

The above is the detailed content of What is the difference between defining CLASS in CSS with spaces and without spaces?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template