CSS style reuse, sub selectors, and group selectors

Source: Internet
Author: User
Tags define
Css

Work and web production related, recently sorted out some CSS things, sent out, hope useful.

Reuse:
Often use some basic pattern overlays, such as font color and bold. There may be three situations in the Web page: 1. The font is Red 2. Font Bold 3. Font red Bold
At this point we just need to define the first two CSS:
. red{color:red;}
. B{font-weight:bold;}
In the third case, use <div class= "Red B" ></div>


Sub selector:
In contrast, code that simplifies HTML files is more important, so using a child selector in CSS is useful and makes CSS code easier to understand. For example, the following code:
<div id= "Sub_nav" >
<ul>
<li> <a href= "#" >item 1</a></li>>
<li> <a href= "#" >item 2</a></li>
<li> <a href= "#" >item 3</a></li>
</ul>
</div>
If Div Li A has its own style, with a sub selector, you can omit the class attribute of Li and a in the code to simplify the code:
#sub_nav {/* Some styling */}
#sub_nav Li {/* Some styling */}
#sub_nav a {/* Some styling */}
Group selector:
When some element types, classes, or IDs have common attributes, you can use the group selector to avoid multiple duplicate definitions. This can save a lot of bytes.
For example, to define the font, color, and margin of all headings, you can write this:
H1,h2,h3,h4,h5,h6 {
font-family: "Lucida Grande", Lucida,arial,helvetica,sans-serif;
Color: #333;
Margin:1em 0;
}
If there are individual elements that need to be defined as separate styles when used, you can add new definitions to cover old definitions, such as:
h1 {font-size:2em;}
h2 {Font-size:1.6em;}
Reuse, child selector and group selector flexible use can be very effective to reduce the code, but also very advantageous to increase the readability of the code, the specific application needs in the specific process of writing experience.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.