Common CSS layout specifications

Source: Internet
Author: User

I. file naming rules global style: global.css; Framework layout: layout.css; font.css; Link style: link.css; Print Style: print.css; II. common class/ID naming Rules Page: header content: Container page: footer version right: Copyright navigation: Menu

I. File naming rules

Global style: global.css;
Frame layout: layout.css;
Font.css;
Link style: link.css;
Print Style: print.css;

Ii. Naming rules for common classes/IDS

Webpage: Header
Content
Container: Container
Footer: footer
Copyright: Copyright
Navigation: Menu
Main navigation: mainmenu
Subnavigation: submenu
Logo: logo
Banner
Title: Title
Sidebar: sidebar
Icon
Note: Note
Search: Search
Button: BTN
Login: Login
Link
Information Box: Manage
......

The names of common classes should be based on common English words as much as possible, so that they are easy to understand and can be annotated in appropriate places. For the second-level class/ID naming, the composite writing mode is used, and the first letter of the last word should be written as follows: for example, the "search box" should be named "searchinput", "Search icon", and "Search button" should be named "searchbtn ".

CSS writing specifications and Methods

I. general writing specifications and Methods

1. Select doctype:

XHTML 1.0 provides three types of DTD declarations:

Transitional: requires a very loose DTD, which allows you to continue using the html4.01 logo (but it must conform to the XHTML syntax ). The complete code is as follows:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

Strict: a strict DTD is required. You cannot use any identifier or attribute of the performance layer, such as <br>. The complete code is as follows:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">

Framework: a dtd designed specifically for the Framework page. If your page contains a framework, you need to use this DTD. The complete code is as follows:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 frameset // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-frameset.dtd">

The ideal situation is of course a strict DTD, but for most of our designers who are new to web standards, the transitional DTD (XHTML 1.0 transitional) is currently an ideal choice (including this site, is also a transitional DTD ). Because this DTD also allows us to use the identifier, element, and attribute of the presentation layer, it is easier to pass W3C code verification.

2. Specify the language and character set:

Specify the language for the document:

<HTML xmlns = "http://www.w3.org/5o/xhtml" lang =" en ">

To be correctly interpreted by the browser and validated by W3C code, all XHTML documents must declare the encoding languages they use. For example:
Common Language definitions:

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
Standard XML document language definition:
<? XML version = "1.0" encoding = "UTF-8"?>
Language definition for browsers of earlier versions:
<Meta http-equiv = "content-language" content = "UTF-8"/>
To improve the character set, we recommend that you use UTF-8 ".

3. Call the style sheet:

External style sheet call:

Page embedding: The style sheet is directly written in the head area of the page code. For example:

<Style type = "text/CSS"> <! -Body {Background: white; color: black;}-> </style>
External call method: Write the sample table in an independent .css file, and then use the code similar to the following in the page head area.

<LINK rel = "stylesheet" REV = "stylesheet" href = "CSS/style.css" type = "text/CSS" Media = "all"/>

In the design that complies with the Web standard, it is recommended to use external call methods. You can change the page style without modifying the page but only modifying the .css file. If all pages call the same style sheet file, you can change the style of all files by changing the style sheet file.

4. Select appropriate elements:

Select HTML elements based on the document structure, rather than the HTML element style. For example, use the P element to contain text paragraphs, rather than for line breaks. If appropriate elements cannot be found during document creation, you can consider using a common Div or span;

Avoid using Div and span in transition. A small amount of appropriate use of Div and span elements can make the structure of the document clearer and more reasonable and easy to use styles;

Use as few labels and structure nesting as possible, which not only makes the document structure clear, but also keeps the file small, while improving the user download speed, it is also easy for the browser to interpret and present the document;

5. Derivative selector:

You can use the derived selector to define a style for the child element in an element, which simplifies the name and makes the structure clearer, for example:

. Mainmenu ul Li {Background: URL (images/bg.gif ;)}

6. Secondary Image Processing with a back image:

"Secondary images" here refer to images that are not part of the content to be expressed on the page, but only used for modification, interval, and reminder. You can use the CSS style to modify the back image without modifying the page, for example:

# Logo {Background: URL (images/logo.jpg) # fefefefe no-repeat right bottom ;}

7. structure and style separation:

Only the structure of the document is written on the page, and the style is written in the CSS file. The CSS style sheet is called externally to separate the structure from the style.

8. Structured Writing of documents:

CSS documents on the page should be written in a structured manner, with clear logic and easy to read. For example:

<Div id = "mainmenu">
<Ul>
<Li> <a href = "#"> homepage </a> </LI>
<Li> <a href = "#"> introduction </a> </LI>
<Li> <a href = "#"> service </a> </LI>
</Ul>
</Div>

/* = Main navigation === */
# Mainmenu {
Width: 100%;
Height: 30px;
Background: URL (images/mainmenu_bg.jpg) Repeat-X;

} # Mainmenu ul Li {float: Left; line-Height: 30px; margin-Right: 1px; cursor: pointer ;} /* ===== main navigation end ===== */9. mouse gesture: in XHTML standard, hand is only recognized by IE, when you need to convert the mouse gesture to the hand shape, replace hand with P

}
# Mainmenu ul Li {
Float: left;
Line-Height: 30px;
Margin-Right: 1px;
Cursor: pointer;
}
/* ===== Main navigation end ===== */

9. Mouse gestures:

In the XHTML standard, hand is only recognized by IE. When you need to convert the mouse gesture to a "hand", replace "hand" with "Pointer", that is, "cursor: pointer ;"

Ii. annotation writing specifications

1. Line comment:

Directly after the property value, such:

. Search {
Border: 1px solid # FFF;/* define the border of the search input box */
Background: URL (../images/icon.gif) No-Report #333;/* define the background of the search box */
}

2. comments for the entire segment:

Add comments at the beginning and end, for example:

/* = Search Results = */
. Search {
Border: 1px solid # FFF;
Background: URL (../images/icon.gif) No-repeat #333;
}
/* ===== Search end ===== */

Iii. Abbreviation of style attribute code

1. Abbreviations of different classes with the same attributes and attribute values:

For two different classes, but some of them are the same or even all of them are the same attributes and attribute values, the abbreviations should be merged, especially when there are multiple different classes with the same attributes and attribute values, merging abbreviations can reduce the amount of code and make it easy to control. For example:

# Mainmenu {
Background: URL (../images/bg.gif );
Border: 1px solid #333;
Width: 100%;
Height: 30px;
Overflow: hidden;
}
# Submenu {
Background: URL (../images/bg.gif );
Border: 1px solid #333;
Width: 100%;
Height: 20px;
Overflow: hidden;
}

The attribute values of two different classes have duplicates, which can be abbreviated:

# Mainmenu, # submenu {
Background: URL (../images/bg.gif );
Border: 1px solid #333;
Width: 100%;
Overflow: hidden;
}
# Mainmenu {Height: 30px ;}
# Submenu {Height: 20px ;}

2. abbreviations of the same attribute:

The same attribute can also be abbreviated based on its attribute value, for example:

. Search {
Background-color: #333;
Background-image: URL (../images/icon.gif );
Background-repeat: No-Repeat;
Background-position: 50% 50%;
}
. Search {
Background: #333 URL (../images/icon.gif) No-repeat 50% 50%;
}

3. abbreviation of the outer and outer borders:

In CSS, the distance between the inside and outside borders is arranged in the order of top, right, bottom, and left. When these four attribute values are different, they can be abbreviated directly, for example:

. BTN {
Margin-top: 10px;
Margin-Right: 8px;
Margin-bottom: 12px;
Margin-left: 5px;
Padding-top: 10px;
Padding-Right: 8px;
Padding-bottom: 12px;
Padding-left: 8px;
}

It can be abbreviated:

. BTN {
Margin: 10px 8px 12px 5px;
Padding: 10px 8px 12px 5px;
}

If the top, bottom, left, and right border attribute values are the same, the attribute values can be abbreviated as two, for example:

. BTN {
Margin-top: 10px;
Margin-Right: 5px;
Margin-bottom: 10px;
Margin-left: 5px;
}

Abbreviation:

. BTN {margin: 10px 5px ;}

When the attribute values of the top, bottom, and left borders are the same, you can simply abbreviated them as one, for example:

. BTN {
Margin-top: 10px;
Margin-Right: 10px;
Margin-bottom: 10px;
Margin-left: 10px;
}

Abbreviation:

. BTN {margin: 10px ;}

4. Abbreviation of color value:

The color value code can be abbreviated to the color value code when the three color values of RGB are the same. For example:

. Menu {color: # ff3333 ;}

Abbreviations:

. Menu {color: # f33 ;}

Iv. hack writing specifications

Because different browsers have different W3C standards, different browsers have different interpretations of the page. For example, ie has a 3px gap with FF in many cases. For these differences, it is necessary to adjust the CSS hack. Of course, it is best not to write the hack to adjust it when there is no need to avoid page problems caused by the hack.

1. Compatibility Between IE6, IE7, and Firefox:

Statement 1:

Ie can recognize *; standard browsers (such as ff) cannot recognize *;
IE6 can recognize *, but cannot recognize it! Important,
IE7 can recognize * and can also recognize! Important;
FF cannot recognize *, but can recognize it! Important;
According to the above expression, the CSS hack of the same class/ID can be written:
. Searchinput {
Background-color: #333;/* all three are allowed */
* Background-color: #666! Important;/* Only IE7 */
* Background-color: #999;/* Only IE6 and IE6 */
}
Generally, the writing sequence is FF, IE7, and ie6.

Statement 2:

IE6 can recognize "_", while IE7 and FF cannot be identified. Therefore, when only the difference between IE6 and IE7 and FF can be written as follows:
. Searchinput {
Background-color: #333;/* General */
_ Background-color: #666;/* Only IE6 can be recognized */
}

Statement 3:

* + HTML and * HTML are unique tags of IE, which are not currently supported by Firefox.
. Searchinput {background-color: #333 ;}
* Html. searchinput {background-color: #666;}/* Only IE6 */
* + Html. searchinput {background-color: #555;}/* Only IE7 */

Shield Internet Explorer:

Select is a selection character, which is replaced according to the situation. The second sentence is unique to the Safari browser on Mac.

*: Lang (zh) Select {Font: 12px! Important;}/* FF dedicated */
Select: Empty {Font: 12px! Important;}/* safari visible */

IE6 recognizable:

Here we mainly use CSS annotations to separate an attribute and a value, and comment it before the colon.

Select {display/* IE6 not recognized */: none ;}

Statement of the IF condition hack of IE:

All ie identifiable:

<! -[If IE]> only ie <! [End if]->
Only ie5.0 can recognize:
<! -[If IE 5.0]> only IE 5.0 <! [End if]->
Ie5.0 for ie5.5 can both be identified:
<! -[If gt ie 5.0]> only IE 5.0 + <! [End if]-> (

 

Only IE6 can recognize :! [If lt IE 6] Only IE 6 -! [End if] ie5.x of IE6 and IE6 can be recognized :! [If gte ie 6] Only IE 6/+! [End if] Only IE7 can recognize :! [If lte ie 7] Only IE 7 /-! [End if] 2. Clear floating: In Firefox, when the sub-level

Only IE6 can recognize:
<! -[If lt IE 6]> only IE 6-<! [End if]->
Ie5.x under IE6 and IE6 can be identified:
<! -[If gte ie 6]> only IE 6/+ <! [End if]->
Only IE7 can recognize:
<! -[If lte ie 7]> only IE 7/-<! [End if]->

2. Clear floating:

In Firefox, when the child level is floating, the height of the parent level cannot fully cover the entire child level, in this case, the floating hack can be used to define the parent level.

Select: After {
Content :".";
Display: block;
Height: 0;
Clear: both;
Visibility: hidden;
}

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.