It is also for the same reason to specify a class.
If you want to change the title to orange, use the serif font, and add a light gray edge to the bottom. In the unordered list of "sidelinks", remove the dot symbol and change the configuration item to bold.
The CSS content required by method A looks like this:
. Sideheading {
Font-family: Georgia, serif;
Color: # c63;
Border-bottom: 1px solid # ccc;
}
. Sidelinks {
List-style-type: none;
}
. Link {
Font-weight: bold;
}
We can specify a special style for these labels by referring to the class name specified in the tag. You can even imagine that other parts of the page are organized in this way: navigation bar, at the end of the page and in the content area, each tag is added with a messy classification to gain full control over them.
Yes, it does work, but there is a simple way to save these class attributes, at the same time make your CSS easier to read, more organized, then look at method B. method B: natural choice
About This Site
This is my site.
My Links
Method B is short and concise! But wait, where are all categories? Well... you will soon find that we don't really need them, mainly because we put these labels into a unique name (in this example, sidebar ).
.
This is where the inheritance selector works. You only need to directly specify the tag in the sidebar with the tag name to remove these redundant Classification attributes. Specify CSS based on the relationship between the front and back of the content.
Let's take a look at the same style as method A, but this time we use the inheritance selector to specify the tag located in sidebar.
# Sidebar h3{
Font-family: Georgia, serif;
Color: # c63;
Border-bottom: 1px solid # ccc;
}
# Sidebar ul{
List-style-type: none;
}
# Sidebar li{
Font-weight: bold;
}
By referring to # sidebar ID, you can specify a special style for the tags contained in it. For example, only
The above CSS rules will be set for the labels in.
This method of specifying a style based on the relationship between the content and the content is the key to shortening the content of the tag. Generally, after a semantic structure is designed for the content, Classification attributes are not required for the tag.
We only show one section of the page (that is, the sidebar), but this method can be applied to the entire page structure, as long as the marked content is logically divided into several paragraphs (maybe # nav, # content, # sidebar, # footer), and then use the inheritance selector to define special styles for the labels in this section.
For example, if tags are used in the # content and # sidebar sections on the page and you want them to all use the serif font, however, you want a section to be displayed in purple, the other is orange.
This does not need to modify any tags, and adds the classification attribute. We can use a global style to specify the rules shared by all tags, and then use the inheritance selector to set the color based on the tag location.
H3 {
Font-family: Georgia, serif;/* All h3s to be serif */
}
# Content h3 {
Color: purple;
}
# Sidebar h3 {
Color: orange;
}
Specify that all labels use the senif font, and the color must be purple or orange based on the context of the content. In this case, we do not need to share the rules again (font-family in this example ), therefore, it can shorten the content of CSS and prevent repeated rules in multiple declarations.
We can not only reduce the additional tag space required for the class attribute, but also make the CSS structure more meaningful. This makes it easier for us to read its content and organize it according to page segments, modifying specific rules is also very easy, especially for large and complex typographical la s, because you may have hundreds of CSS rules at the same time.
For example, in this example, if you want to add the sharing rule to each declaration, and later you want to replace all the rules with the sans serif font, You have to modify them in three places. fewer categories, better maintenance
In addition to reducing the source code space that needs to be used, replacing redundant classes with the inheritance selector also indicates that the marked content will be easy to expand in the future.
For example, let's assume that you want to change the link in the sidebar to red instead of using blue as the rest of the page, so you have created a red class, add it to the anchor tag as follows:
About This Site
This is my site.
My Links
- Class = "red"> Archives
- Class = "red"> About Me
To turn these links into red (assuming the preset link color is not red), CSS like this is required:
A: link. red {
Color: red;
}
These actions are fine and can work properly. But what if you change your mind and want to change these links to green in the future? Or, more practically, your boss occasionally says, "This year's red is outdated. Change these sidebar links to green "! No problem. You only need to modify the red class in CSS, but the class attribute in the TAG content is still red, which obviously does not fully conform to the semantics, just like using other colors as classification names.
This is not a good place to use the display effect as the category name, but if no category is specified at all, we can save a lot of effort (and Code) to process the classification and make the content semantics more reasonable, instead, use the inheritance selector to select links in these sidebar and specify the style as needed.
The content of the tag is exactly the same as Method B, and the CSS required to set the link on the sidebar will be as follows:
# Sidebar li a: link{
Color: red;
}
Basically, this indicates that "only
Within