CSS's eighteen skills

xiaoxiao2021-03-05  24

Original author: Roger Johansson Author: Lives in Gothenburg, Sweden, in 1994, came into contact with and involved in web design, 456 Berea Street was his home address, so the use of this name as his personal home domain ORIGINAL: www.456bereastreet.com originally published Time: March 15, 2005 Abie Description: This article "CSS TIPS AND TRICKS", 2 articles, I merged them into this article.

Recently, there are often friends asking me some CSS issues encountered. They can't control CSS very well, affecting the efficiency of CSS. Let me analyze the error, help everyone easier to use CSS.

This article summed up all the skills and compatibility programs I have started using the CSS layout method. I am willing to share these with you, I will focus on the mistakes of some novices (including myself also committed), if you are already CSS Master, these experience skills may already know, if you have more, I hope I can help me.

1. Use CSS abbreviation

Using abbreviations can help reduce the size of your CSS file, easier to read. The main rules of CSS abbreviations are given to "Common CSS Abblast Summary Summary", which is not expanded here.

II. Clearly defined units unless the value is 0

Forgetting the unit of defined size is a universal error in CSS. In HTML, you can write only width = "100", but in CSS, you must give an accurate unit, such as: Width: 100px Width: 100em. Only two exceptions may not be defined: rows and 0 values. In addition, other values ​​must keep up with the unit, not to add binding between the values ​​and units.

Size case

When using CSS in XHTML, the element name defined in CSS is case sensitive. In order to avoid this error, I recommend that all definition names use lowercase.

The values ​​of Class and ID are also case sensitive in HTML and XHTML. If you must write a mixed write, please make a closer to confirm that you are in the definition of CSS and the labels in XHTML are consistent.

4. Cancel the element defined before the Class and ID

When you write to an element definition class or id, you can omit the previous elements define because the ID is unique in one page, while Clas S can be used multiple times in the page. You define a certain element. E.g:

Div # content {/ * Declarations * /} fieldset.details {/ * declarations * /}

Can write

#content {/ * declarations * /} .details {/ * declarations * /}

This saves some bytes.

V. Default

Usually the default value of Padding is 0, and the default value for Background-Color is Transparent. But the default value of different browsers may be different. If you are afraid of conflict, you can define all the size of the margin and padding values ​​of all elements in the style table:

* {margin: 0; Padding: 0;

6. No need to repeat the definition of the inheritable value

In CSS, the child element automatically inherits the attribute value of the parent element, the color, font, etc., which have been defined in the parent element, and can be inherited directly in the child element, and no repeat definition is required. But note that the browser may override your definition with some default values.

7. Recent priority principles

If there is a variety of definitions of the same element, the definition of the closest (minimum level) is best, for example, such a code

Update: Lorem Ipsum Dolor Set

In the CSS file, you have defined element P and define a class "update" p {margin: 1em 0; font-size: 1em; color: # 333;} .update {font-weight: bold; color: # 600;}

In these two definitions, class = "update" will be used because the Class is closer than p. You can check out the W3C's "Calculating A Selector's Specificity" to learn more.

Eight. Multiple Class definitions

A tag can define multiple Class at the same time. For example: Let's define two styles, the first style background is # 666; the second style has a border of 10 px.

.one {width: 200px; Background: # 666;}. Two {border: 10px solid # f00;}

In the page code, we can call this way

Such a final display is the background of this DIV existing # 666, and there is a 10px border. Yes, this is ok, you can try it.

Nine. User selectors (Descendant Selectors)

CSS beginners don't know if the use of sub-selectors is one of the reasons that affect their efficiency. Subsets can help you save a lot of Class definitions. Let's look at the following code:

转载请注明原文地址:https://www.9cbs.com/read-36520.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.040, SQL: 9