Selector usage specification
Selector Types and Specificity
CSS selectors determine the scope of style rules, and their proper use enhances code maintainability. Common selector types include:
- Element Selector: Directly matches HTML elements
p {
color: #333;
}
- Class Selector: Matches via the
class
attribute
.btn-primary {
background: blue;
}
- ID Selector: Matches via the
id
attribute
#header {
height: 80px;
}
- Attribute Selector: Matches based on element attributes
input[type="text"] {
border: 1px solid #ccc;
}
- Pseudo-class Selector: Matches specific states of elements
a:hover {
text-decoration: underline;
}
Selector Combination Methods
Combining selectors effectively allows precise control over style application:
- Descendant Selector (separated by spaces)
.nav li {
display: inline-block;
}
- Child Selector (>)
.menu > .item {
padding: 8px;
}
- Adjacent Sibling Selector (+)
h2 + p {
margin-top: 0;
}
- General Sibling Selector (~)
h1 ~ p {
font-size: 1.1em;
}
- Multiple Selector (comma-separated)
h1, h2, h3 {
font-family: sans-serif;
}
Selector Specificity Rules
When multiple rules conflict, browsers determine the final style based on specificity:
-
Calculation Rules:
- Inline styles: 1000
- ID selectors: 100
- Class/attribute/pseudo-class: 10
- Element/pseudo-element: 1
- Wildcard: 0
-
Example Comparison:
#content .title { /* 100 + 10 = 110 */
color: red;
}
div#content span.title { /* 1 + 100 + 1 + 10 = 112 */
color: blue;
}
- !important Exception:
.special {
color: green !important; /* Highest priority */
}
Selector Performance Optimization
Efficient selectors improve page rendering performance:
- Avoid Over-Qualification:
/* Not recommended */
div#nav ul li a {}
/* Recommended */
.nav-link {}
- Minimize Wildcard Usage:
/* Not recommended */
* {
box-sizing: border-box;
}
/* Recommended */
html {
box-sizing: border-box;
}
- Prefer Class Selectors:
/* Not recommended */
section:first-child h2 {}
/* Recommended */
.section-title {}
- Avoid Deep Nesting:
/* Not recommended */
body div#wrapper ul.nav li a {}
/* Recommended */
.nav-link {}
BEM Naming Convention
BEM (Block Element Modifier) is a popular CSS naming methodology:
-
Basic Structure:
- Block: Independent functional unit
- Element: Component of a block
- Modifier: State or variation
-
Naming Examples:
/* Block */
.menu {}
/* Element */
.menu__item {}
/* Modifier */
.menu__item--active {}
- Practical Application:
<nav class="nav">
<a class="nav__link nav__link--current">Home</a>
<a class="nav__link">Products</a>
</nav>
Selector Maintainability Practices
Practical tips for maintaining selector readability:
- Semantic Naming:
/* Not recommended */
.red-box {}
/* Recommended */
.alert-warning {}
- Avoid Location Dependency:
/* Not recommended */
.header .logo {}
/* Recommended */
.site-logo {}
- Use Prefixes for Differentiation:
.js-toggle {} /* JavaScript hook */
.is-hidden {} /* State class */
- Modular Organization:
/* button.module.css */
.primary {
/* Button styles */
}
/* Automatically generates unique class names to avoid conflicts */
Selectors in Responsive Design
Key points for using selectors in media queries:
- Mobile-First Principle:
/* Base styles (mobile) */
.card {
width: 100%;
}
/* Large screen adaptation */
@media (min-width: 768px) {
.card {
width: 50%;
}
}
- Device-Specific Selection:
/* Tablet devices only */
@media (hover: hover) and (pointer: fine) {
.tooltip:hover::after {
display: block;
}
}
- Orientation Detection:
@media (orientation: portrait) {
.header {
height: 60px;
}
}
Selector Nesting in Preprocessors
Nesting standards in Sass/Less:
- Reasonable Nesting Depth:
/* No more than 3 levels */
.menu {
&__item {
&--active {
color: red;
}
}
}
- Avoid Excessive Nesting:
/* Not recommended */
body {
div {
ul {
li {
a {
color: blue;
}
}
}
}
}
/* Recommended */
.menu-link {
color: blue;
}
- Parent Selector Reference:
.btn {
&:hover {
opacity: 0.8;
}
&--large {
padding: 12px 24px;
}
}
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:命名规则(BEM/OOCSS等)
下一篇:为什么前端工程师的头发越来越少?