Write a better CSS

Source: Internet
Author: User

Well-written CSS code can improve the rendering speed of the page. Essentially, a rule does not have the fastest resolution of the engine. The MDN splits the CSS selector into four main categories, as shown below, and the performance is reduced in turn.

    1. ID rule
    2. Class rules
    3. Label rules
    4. General rules

The general understanding of efficiency begins with the advanced Guide to high-performance website construction, published by Steve Souders in 2009, although Souders's book is detailed in its entirety, you can see the full list of references here. You can also view more details in the best practices of Google's efficient CSS selector.

In this article I would like to share some simple examples and guidelines that I have used to write high-performance CSS. Be inspired by the authoring of MDN efficient CSS guides and follow a similar format.

Avoid over-restraint

As a general rule, no unnecessary constraints are added.

Bad Ul#someid {..}. menu#otherid{..} Good #someid {...} #otherid {..}
The descendant selector sucks.

Not only is the performance low and the code very fragile, the HTML code and CSS code are badly coupled, the HTML code structure changes, the CSS has to be modified, which is how bad, especially in large companies, writing HTML and CSS is often not the same person.

It sucks. html DIV tr td {...}
Avoid chaining (intersection) selectors

This is similar to overly restrictive situations, and it is wiser to simply create a new CSS class selector.

Oops. Menu.left.icon {..} Okay, Menu-left-icon. {..}
Stick to the KISS principle

Imagine that we have the following DOM:

<ul id= "Navigator" >    <li><a href= "#" class= "Twitter" >Twitter</a></li>    <li ><a href= "#" class= "Facebook" >Facebook</a></li>    <li><a href= "#" class= "Dribble" >Dribbble</a></li></ul>

Here are the corresponding rules ...

Bad #navigator li a {...} Good #navigator {...}
Using compound syntax

Use conforming syntax whenever possible.

Oops. SomeClass {padding-top:20px; padding-bottom:20px; padding-left:10px; padding-right:10px; background: #000; back Ground-image:url (.. /imgs/carrot.png); Background-position:bottom; Background-repeat:repeat-x;} Okay. SomeClass {padding:20px 10px 20px 10px; background: #000 URL (.. /imgs/carrot.png) Repeat-x Bottom;}
Avoid unnecessary namespaces
Oops. SomeClass table Tr.otherclass Td.somerule {..} Okay. SomeClass. otherclass td.somerule {..}
Avoid unnecessary duplication

Combine the repeating rules as much as possible.

Bad. SomeClass {color:red; background:blue; font-size:15px;}. otherclass {color:red; background:blue; font-size:15px;} Ok. SomeClass,. otherclass {color:red; background:blue; font-size:15px;}
Streamline rules as much as possible

Based on the above rules, you can further merge the repeating rules in different classes.

Bad. SomeClass {color:red; background:blue; height:150px; width:150px; font-size:16px;}. otherclass {color:red; background:blue; height:150px; width:150px; font-size:8px;} Ok. SomeClass,. otherclass {color:red; background:blue; height:150px; width:150px;}. SomeClass {font-size:16px;}. Otherclass {font-size:8px;}
Avoid ambiguous naming conventions

It is best to use a name that represents semantics. A good CSS class name should describe what it is and not what it looks like.

Avoid!importants

In fact, you should also be able to use other high-quality selectors.

Follow a standard declaration order

While there are some common ways of arranging CSS property order, here's a popular way I follow.

. SomeClass {/* positioning */* Display & Box Model *//* Background and typography styles */* transitions */* O ther */}
Well-organized code formats

The readability of the code is proportional to the maintainability and ease of maintenance. Here are the formatting methods I followed.

Oops. Someclass-a,. Someclass-b,. Someclass-c,. someclass-d {...} Ok. Someclass-a,. Someclass-b,. Someclass-c,. someclass-d {...} Good practice. SomeClass {    background-image:        linear-gradient (#000, #ccc),        linear-gradient (#ccc, #ddd);    Box-shadow:        2px 2px 2px #000,        1px 4px 1px 1px #ddd inset;}

Obviously, these are just a handful of rules that I try to follow in my own CSS, in a more efficient and maintainable nature. If you want to read more knowledge, I recommend reading MDN on the authoring efficient CSS and optimizing browser rendering on Google Guide.

Write a better CSS

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.