In the previous chapters, we learned that you can use inline styles, ID selectors, class selectors, and tag selectors to apply styles to an element.
If we want to apply styles to elements that all define a certain attribute, what should we do at this time?
In this section, I will use Double Color Ball Case and Document Type Tip Icon Case to introduce and apply attribute selectors.
Double-color ball case:
As we all know, there are 7 balls in double-color ball, 6 red balls and 1 blue ball. First, we define 7 span tags on the page:
< ;head>
0102< /span>03050607 |
.balls span{ margin-left:0.8em; } |
.balls span{ margin-left:0.8em; /*Set the distance between the digital balls to 0.8em*/ display:block; /*Set the span of the digital balls to block for easy width adjustment and height*/ float:left; /*Make the number ball float to the left and display it in one line*/ width:1.4em; /*Set the width and height of the number ball span* / height:1.4em; border:1px solid red;/*Set the digital ball border to red. This is to make it easier to see whether the set style has taken effect*/ -webkit-border-radius:0.7em;/*Set the span fillet radius. If the radius is half the total length, a circle will be formed*/ -moz-border-radius:0.7em; border-radius:0.7em; text-align:center; /*Make the words in the number ball appear horizontally centered*/ line-height:1.4em ; /*Make the words in the number ball appear vertically centered*/ } |
Run the Demo at this time, and the prototype of the sphere has come out:
Some people will ask, why are your styles written in the same span tag selector Okay, why haven’t we mentioned the attribute selector yet?
Don’t worry, the above styles are attribute styles common to all spheres, so they are all written in the same tag. We know that the first six balls are red balls and the last one is blue balls. So how to deal with this?
Let’s take a look at the knowledge points first. Attribute selectors were introduced from CSS2. The following are the attribute selectors defined in CSS2:
| Selects elements of type E that match the attr attribute. Note that the E type can be omitted and directly write [attr], which means matching all elements with the attr attribute defined. | ||||||||||||||
E[attr="value"] | Select matching E-type elements whose attr attribute value is set to value. Note that the E type can be omitted and directly written [attr="value"], which means matching all elements with attr="value". | ||||||||||||||
E[attr~="value"] | Select matching attr attribute values in list form, and each value All elements of type E that are separated by spaces and have a value of value. Note that E can be omitted. | ||||||||||||||
E[attr|="value"]
| Select to match those attr attribute values that are value or start with value- E type element. Note that the E type can be omitted. | ||||||||||||||
E[attr^="value"] | Select to match those E-type elements whose attr attribute value starts with value . Note that the E tag selector can be omitted. | ||||||||||||||
E[attr$="value"] | Select to match those E types whose attr attribute values are suffixed by value element. Note that the E tag selector can be omitted. | ||||||||||||||
E[attr*="value"] | Select matching E-type elements whose attr attribute value contains value. Note that the E tag selector can be omitted. |
.balls span{ margin-left:0.8em; /*Set the distance between the digital balls to 0.8em*/ display:block; /*Set the span of the digital balls to block for easy adjustment of width and Height*/ float:left; /*Make the number ball float to the left and display it in one line*/ width:1.4em; /*Set the width and height of the number ball span*/ height:1.4em; border:1px solid red;/*Set the digital ball border to red. This is to make it easier to see whether the set style has taken effect*/ -webkit-border-radius:0.7em;/*Set the span fillet radius. If the radius is half the total length, a circle is formed*/ -moz-border-radius:0.7em; border-radius:0.7em; text-align:center; /*Make the words in the number ball appear horizontally centered*/ line-height:1.4em; /*Make the words in the number ball appear vertically centered*/ color:#FFFFFF; /*Set the font color in span*/ box-shadow:0.15em 0.15em #999; /*Apply shadow effects to balls to increase the three-dimensional effect*/ } /*Apply styles to all balls*/ .balls span[title]{ /*Apply style to span with title attribute set*/ background-color:#FF0000; } /*Give class blueball Apply styles to the ending span element*/ .balls span[class$="blueball"]{ background-color:#0033CC; } |
Run our Demo at this time, and the double color ball renderings will come out.
Careful netizens may have noticed that in the first ball, we also set the blueball style, but it still has a red background, which proves that E[attr $="value"] does add styles to those E elements whose attr attribute values end with value.
Next, we continue to explain the document type prompt icon case:
First create the page:
|
.prefer_docs dd{ /*Clear the distance from dd to dl border*/ margin-left:0px; } .prefer_docs ul{/*Reset ul style*/ list-style-type:none; padding-left:0px; margin-left:0px; } .prefer_docs li{/*Adjust the top and bottom distance of recommended documents*/ margin-bottom:2px; } .prefer_docs a{/* Remove underline from document hyperlink*/ text-decoration:none; } .prefer_docs a:hover{/*Apply style to hyperlink when mouse rolls over* / color:red; text-decoration:underline; } .prefer_docs a span{ /*Define document type icon display area size */ background: no-repeat; display:block; height:16px; width:16px; float:left; margin-right:2px; } .prefer_docs a span{ /*Import document type background image*/ background -image:url(pkg_icon.png); } .prefer_docs a[href$="ppt"] span{/*All ending with ppt, apply the ppt icon*/ background-position:-856px -36px; } .prefer_docs a[href*="pdf"] span{/*For links containing pdf text Link, apply pdf icon*/ background-position:-625px -36px; } .prefer_docs a[class|="doc"] span{/* For links starting with doc or doc- for class, use the doc icon */ background-position:-877px -36px; } .prefer_docs a[class~ ="net"] span{/* In the class attribute, the link containing the net value applies the Internet icon*/ background-position:-520px -36px; } |
Finally, let’s take a look at the running effect:
The above is a code demonstration that uses the attribute selector to apply the corresponding icon to the document download link.
Of course, there are many selectors, such as pseudo-class selectors and pseudo-element selectors, which will not be described in detail here. In future examples, if used, they will be further detailed.
Welcome to join the Internet technology exchange group: 62329335
Personal statement: The blog posts shared are absolutely original, and we strive to make every one of them original. Knowledge points are verified through practical demonstrations.