Optimization tips for website refactoring CSS style sheets
Author:Eve Cole
Update Time:2009-06-04 19:45:48
1. Use css abbreviations
Using abbreviations can help reduce the size of your CSS files and make them easier to read. For the main rules of CSS abbreviation, please refer to "Common CSS Abbreviation Syntax Summary", which will not be described here.
2. Define the unit clearly unless the value is 0
Forgetting to define the units of a dimension is a common mistake among newbies to CSS. In HTML you can just write width=100, but in CSS you have to give an exact unit, such as: width:100px width:100em. There are only two exceptions where units cannot be defined: row height and 0 value. In addition, other values must follow the unit. Be careful not to add spaces between the value and the unit.
3. Case sensitive
When using CSS in XHTML, element names defined in CSS are case-sensitive. To avoid this error, I recommend using lowercase for all definition names.
The values of class and id are also case-sensitive in HTML and XHTML. If you must write mixed case, please carefully confirm that your definition in CSS is consistent with the tags in XHTML.
4. Cancel the element restrictions before class and id
When you write to define a class or id for an element, you can omit the previous element qualification, because the ID is unique in a page and can be used multiple times in the page. It makes no sense for you to qualify an element. For example:
div#content { }
fieldset.details { }
can be written as
#content { }
.details { }
This saves some bytes.
5.Default value
Usually the default value of padding is 0, and the default value of background-color is transparent. But the default value may be different in different browsers. If you are afraid of conflicts, you can define the margin and padding values of all elements to be 0 at the beginning of the style sheet, like this:
* {
margin:0;
padding:0;
}
6. No need to repeatedly define inheritable values
In CSS, child elements automatically inherit the attribute values of the parent element, such as color, font, etc., which have been defined in the parent element, can be directly inherited in the child element without repeated definition. But be aware that the browser may override your definition with some default values.
7. Recent first principle
If there are multiple definitions of the same element, the closest (minimum level) definition will be given priority. For example, there is this piece of code
Update: Lorem ipsum dolor set
In the CSS file, you have defined the element p and a classupdate
p {
margin:1em 0;
font-size:1em;
color:#333;
}
.update {
font-weight:bold;
color:#600;
}
Of these two definitions, class=update will be used because class is closer than p. You can check out W3C's "Calculating a selector's specificity" to learn more.
8. Multiple class definitions
A tag can define multiple classes at the same time. For example: We first define two styles, the first style has a background of #666; the second style has a 10 px border.
.one{width:200px;background:#666;}
.two{border:10px solid #F00;}
In the page code, we can call like this
The final display effect is that this div has both a #666 background and a 10px border. Yes, it is possible to do this, you can try it.
9. Use descendant selectors
CSS beginners don't know that using sub-selectors is one of the reasons that affects their efficiency. Subselectors can help you save a lot of class definitions. Let's take a look at the following code:
<div id=subnav>
<ul>
<li class=subnavitem> <a href=# class=subnavitem>Item 1</a></li>>
<li class=subnavitemselected> <a href=# class=subnavitemselected> Item 1</a> </li>
<li class=subnavitem> <a href=# class=subnavitem> Item 1</a> </li>
</ul>
</div>
The CSS definition for this code is:
div#subnavul { }
div#subnavulli.subnavitem { }
div#subnavulli.subnavitem a.subnavitem { }
div#subnavulli.subnavitemselected { }
div#subnavulli.subnavitemselected a.subnavitemselected { }
You can replace the above code with the following method
<ul id=subnav>
<li> <a href=#> Item 1</a> </li>
<li class=sel> <a href=#> Item 1</a> </li>
<li> <a href=#> Item 1</a> </li>
</ul>
The style definition is:
#subnav { }
#subnavli { }
#subnav a { }
#subnav .sel { }
#subnav .sel a { }
Use subselectors to make your code and CSS more concise and easier to read.
10. No need to add quotes to the background image path
To save bytes, I recommend not quoting the background image path, as the quotes are not necessary. For example:
background:url(images
margin:0 auto;
}
But IE5/Win cannot display this definition correctly. We use a very useful trick to solve it: use the text-align attribute. Like this:
body {
text-align:center;
}
#wrap {
width:760px;
margin:0 auto;
text-align:left;
}
The first body's text-align:center; rule defines that all elements of the body in IE5/Win are centered (other browsers just center the text), and the second text-align:left; is to center the text in #warp to the left.
15. Import and hide CSS
Because older browsers do not support CSS, a common approach is to use the @import technique to hide CSS. For example:
@import url(main.css);
However, this method didn't work for IE4, which gave me a headache for a while. Later I wrote it like this:
@import main.css;
In this way, CSS can be hidden in IE4. Haha, it also saves 5 bytes. If you want to know the detailed explanation of @import syntax, you can see here "centricle's css filter chart"
16. Optimization for IE
Sometimes, you need to define some special rules for IE browser bugs. There are too many CSS hacks here. I only use two of them, regardless of whether Microsoft is better in the upcoming IE7 beta version. With support for CSS, both methods are the safest.
1. Annotation method
(a) To hide a CSS definition in IE, you can use a child selector:
html>body p {
}
(b) The following writing method can only be understood by IE browser (hidden from other browsers)
* html p {
}
(c) Sometimes, you want IE/Win to be active but IE/Mac to be hidden, you can use the backslash trick:
* html p {
declarations
}
2. Method of conditional comments
Another method, which I think is more time-tested than CSS Hacks, is to use Microsoft's private attribute conditional comments (conditional comments). Using this method you can define some styles separately for IE without affecting the definition of the main style sheet.
17. Debugging Tips: How big are the layers?
When debugging CSS errors, you have to be like a typewriter and analyze the CSS code line by line. I usually define a background color on the layer in question so it's obvious how much space the layer takes up. Some people suggest using border, which is generally possible, but the problem is that sometimes border will increase the size of elements, and border-top and boeder-bottom will destroy the vertical margin value, so it is safer to use background.
Another property that often causes problems is outline. Outline looks like boeder, but does not affect the size or position of the element. Only a few browsers support the outline attribute, the only ones I know of are Safari, OmniWeb, and Opera.
18. CSS code writing style
When writing CSS code, everyone has their own writing habits regarding indentation, line breaks, and spaces. After constant practice, I decided to adopt the following writing style:
selector1,
selector2 {
property:value;
}
When using union definitions, I usually write each selector on its own line so they are easier to find in the CSS file. Add a space between the last selector and the curly braces {. Also write each definition on its own line. The semicolon should be placed directly after the attribute value. Do not add spaces.
I am used to adding a semicolon after each attribute value. Although the rules allow that the semicolon does not need to be written after the last attribute value, if you want to add a new style, it is easy to forget to add the semicolon and cause an error, so you still add it. Better.
Finally, the closing brace } is written on a line by itself.
Spaces and line breaks aid reading.