Home >Web Front-end >Front-end Q&A >What selectors are there in CSS?
p { color: red; }<p>The above code will change the text color of all
<p>
elements in the HTML document to red.
.blue { color: blue; }<p>The above code will change the text color of all elements with the class attribute
"blue"
to blue.
#header { width: 100%; }<p>The above code will adjust the width of the element with the id attribute
"header"
to 100%.
<p>2. Combination selector
header nav { background-color: blue; }<p>The above code will set the background of all
<nav>
elements under the <header>
element The color changes to blue.
ul > li { font-size: 16px; }<p>The above code will remove the direct child elements of all
<ul>
elements<li>## The font size of # is set to 16 pixels.
3. Attribute selector<p>
a[href="https://www.example.com"] { color: green; }The above code will set the color of all anchor elements linking to <p>https://www.example.com
to green.
input[type="text"] { background-color: #f2f2f2; }The above code will remove all <p>
elements that have a type attribute of
"text" The background color is set to off-white.
4. Pseudo-class selector<p>
a:hover { color: red; }The above code will set the color of all links to red on mouseover. <p>
input:focus { border: 2px solid green; }The above code will set the border color of an <p>
element when the user sets it as focus is green.
5. Pseudo-element selector<p>
h1::before { content: ">> "; }The above code will insert a content with two greater than signs in front of all <p>
elements.
p::first-letter { font-size: 20px; }The above code will set the font size of the first letter of each <p><p>
element to 20 pixels.
Summary: <p>The above are the commonly used selector types in CSS, through which web pages can better display various styles. In actual web design, we can choose different selectors to use according to the actual situation to achieve better results. <p>The above is the detailed content of What selectors are there in CSS?. For more information, please follow other related articles on the PHP Chinese website!