# symbol plus the value of the id attribute to select elements.
<p>For example, add the id attribute to a <div>
element and use the id selector to style it: <div id="myDiv">这是一个示例</div>
#myDiv { color: red; font-size: 16px; }
#myDiv
selects the <div>
element with an id attribute value of "myDiv" and sets its text color to red and font size to 16 pixels. By specifying a unique id attribute value, we can select and style specific elements. .
symbol followed by the class name to select elements. <p>For example, add the same class name to two <p>
elements and use the class selector to style them: <p class="myClass">这是第一个段落</p> <p class="myClass">这是第二个段落</p>
.myClass { background-color: yellow; padding: 10px; }
.myClass
will select all elements with the class name "myClass" and set their background color to yellow, adding 10 pixels of padding. By specifying the same class name, we can select a group of elements and style them uniformly. []
followed by the attribute name (optional: you can also add the attribute value) to select elements. <p>For example, to select an <img>
element with a title attribute: <img src="image.jpg" alt="图片" title="这是一个图片">
img[title] { border: 1px solid black; }
img[title]
selects all <img>
elements that have a title attribute and adds a black 1-pixel border to them. We can also further specify specific attribute values, such as img[title="This is a picture"]
, so that only those with the title attribute value of "This is a picture" will be selected< img>
element.
<p>To sum up, id, class and attribute selector are three commonly used CSS selector properties. By using them appropriately, we can select and style specific elements in web pages. Hopefully the specific code examples provided in this article will help you better understand and use these selector properties. If you have more questions about CSS selectors, you can check out the relevant documentation or tutorials for further in-depth learning and mastery. The above is the detailed content of Detailed explanation of CSS selector properties: id, class and attribute selectors. For more information, please follow other related articles on the PHP Chinese website!