


What is the difference between defining CLASS in CSS with spaces and without spaces?
.example .pp{ color: orange; }.example.pp2 { color: green; }
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>
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>
.example.pp selects elements that contain both example and pp in class.
For example:
<div class="example pp"> 被选择的元素 </div>
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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.
