h1 {
color: red;
font-size: 5em;
}
In the above example, the CSS rule opens with a selector . This selects the HTML element that we are going to style. In this case, we are styling level one headings h1.
In your HTML document, add a class attribute to the second list item. Your list will now look like this:
<ul>
<li>Item oneli>
<li class="special">Item twoli>
<li>Item <em>threeem>li>
ul>
Copy to Clipboard
In your CSS, you can target the class of special
by creating a selector that starts with a full stop character. Add the following to your CSS file:
.special {
color: orange;
font-weight: bold;
}
// 儿子
li em {
color: rebeccapurple;
}
This selector will select any element that is inside (a descendant of) an
.
Something else you might like to try is styling a paragraph when it comes directly after a heading at the same hierarchy level in the HTML. To do so, place a +
(an adjacent sibling combinator) between the selectors.
// 兄弟
h1 + p {
font-size: 200%;
}
the CSS below styles unvisited links pink and visited links green.
a:link {
color: pink;
}
a:visited {
color: green;
}
/* when the user hovers over it */
a:hover {
text-decoration: none;
}
You can target multiple selectors at the same time by separating the selectors with a comma. If you want all paragraphs and all list items to be green, your rule would look like this:
p, li {
color: green;
}
li.special {
color: orange;
font-weight: bold;
}
This syntax means “target any li
element that has a class of special”.
/* selects any that is inside a , which is inside an */
article p span { }
/* selects any that comes directly after a
, which comes directly after an */
h1 + ul + p { }
body h1 + p .special {
color: yellow;
background-color: black;
padding: 5px;
}
This will style any element with a class of special
, which is inside a , which comes just after an
, which is inside a
.
参考 CSS选择器