HTML & CSS coding specifications

Source: Internet
Author: User

General summary)

General principles

Complies with W3C general web page standards.

The structure is clear, the nesting is correct, and the nested depth is appropriate.

CodeRefined.

The annotations are clear and the code is highly readable.

Unified style.

 

1.1 Basic naming Style

The naming rules in this document only use the following naming styles.

    • CamelStyle

In this style, except the first letter of the first word, all other words should be the first letter. Ex: backcolor

1.2 case sensitive

    • Do not show two classes that are case sensitive.
    • Do not show two IDs that are case sensitive.

Naming Conventions and style (naming conventions and code styles)

1.3 all classes and IDs adopt the Pascal Style

<Ul class = "mainnav"> </ul>

<Div id = "footerlink"> </div>

1.4 Use meaningful classes and IDs.

<! -- Good Example -->

<P class = "sidebar"> </P>

<Div id = "Container"> </div>

 

<! -- Not good example -->

<P class = "boxa"> </P>

<Div id = "boxb"> </P>

 

1.5 Use class whenever possible for style control. ID is used for JavaScript to control the DOM structure.

    • This prevents the CSS code from increasing due to priority issues.
    • The same class can be applied to multiple tags, but the same ID can only be used for one tag.

1.6 All csscodes are individually enclosed in a separate cssfile. The file name can be used to compile CSS code for page layout, for example, layout.css.

    • Try not to write CSS Code directly in HTML tags.
    • In the

1.7 one attribute per line in CSS code

    • The Code style of one attribute per line is more conducive to maintenance and easier to annotate
    • Clearer browser compatibility processing with hack

/* Example */

. Mainnav {

Width: 200px;

Height: 200px;

Background: # f00 url(bg.jpg) No-repeat left top ;/*Background Color and background image*/

}

 

    • We do not recommend using the following code format, even if there is only one attribute.

/* Example */

. Mainnav {

Width: 200px; Height: 200px; Background: # f00 url(bg.jpg) No-repeat left top ;/*Background Color and background image*/

}

 

 

1.8 The CSS code should follow the principle of going from outside to inside, from top to bottom, and from left to right

    • First write the peripheral framework, and then write the internal elements.
    • First, write the element CSS code that appears visually on the page on the webpage, and then write the following.
    • First, write the element CSS code that appears visually on the left of the page on the webpage, and then write the code on the right.

<! -- Example -->

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

<HTML xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title></Title>

<Style>

. Header {

}

. Container {

}

. Container. Sidebar {

}

. Container. Main {

}

. Footer {

}

</Style>

</Head>

<Body>

<Div class = "Header">Header</Header>

<Div class = "Container">

<Div class = "sidebar">Sidebar</Div>

<Div class = "Main">Subject Region</Div>

</Div>

<Div class = "footer">Bottom</Div>

</Body>

</Html>

 

 

1.9 code abbreviations

    • Attributes such as padding, margin, and border should be abbreviated.

/* Good example */

Margin: 10px 5px 15px;

Padding: 1px 2px;

Border-width: 1px;

 

/* Not good example */

Margin: 10px 5px 15px 5px;

Padding: 1px 2px 1px 2px;

Border-width: 1px 1px 1px 1px;

 

 

1.10 all csscodes are individually enclosed in a separate cssfile. The file name can be used to compile CSS code for page layout, for example, layout.css.

    • Give appropriate comments at the beginning of the file

/*-------

File Name:Layout.css

Role: Page subject layout

Creator:

Creation date:

Last Updated:

Last Update Time:

-------*/

 

 

 

Indentation and spacing (indentation and space)

1.11 use tab indentation. Do not use spaces. Define the tab value as 4

1.12 The comment and code should be in the same column and use the same indent format

    • A separate comment on an attribute is written in the same row.

/* Good example */

/* Header */

. Header {

Width: 200px;

Min-Height: 100px ;/*Set the minimum height for IE7 */

}

1.13 there should be only one space between the selector and the start braces ({)

1.14 end braces (}) should be placed in a separate line.

1.15 use one or two blank lines to separate CSS code segments of different page modules

/* Good example */

. Header {

Width: 200px;

Height: 100px;

}

. Header. mainnav {

Margin: 5px 2px;

}

 

 

. Container {

}

. Container. Sidebar {

}

 

. Container. Main {

}

 

 

. Footer {

}

 

/* Not good example */

. Header {

Width: 200px;

Height: 100px;

}

. Header. mainnav {

Margin: 5px 2px;

}

. Container {

}

. Container. Sidebar {

}

. Container. Main {

}

. Footer {

}

 

 

Good programming practices (coding best practices)

1.16 The CSS writing sequence should follow the following principles (from top to bottom)

    • Layout class clear, float, position, display
    • Container Control width, height, padding, margin, border
    • BACKGROUND Background
    • Text control font, color, text-align, vertical-align
    • Other attributes

 

1.17 The selector name can directly reflect the meaning of the element. Do not use meaningless names.

/* Good example */

/*Main navigation bar */

. Mainnav {

}

/*Sidebar menu */

. Sidemenu {

}

 

/* Not good example */

. Menu1 {

}

. Menu2 {

}

1.18 select the correct tag

    • Do not blindly pursue and use Div labels
    • Do not discard table labels completely. Table is the first choice when you need to arrange and display data.
    • Use the <DL> label correctly in the proper place, instead of the redundant Div and P labels.
    • Reasonably arrange
    • Try not to use the <font> label

1.19 The nesting depth of the same tag should not exceed three layers

    • Common Errors: Use Div labels with more than three layers nested, and use other replaceable labels for compiling.

1.20 do not use unnecessary code

    • The less code, the better.

/* Good example */

SPAN {

Float: left;

}

/* Not good example */

SPAN {

Float: left;

Display: block;

}

/*Although span itself is an inline element, it is automatically converted to a block element after float: left, so you do not need to add the display: block */

 

1.21 clear floating in time and pay attention to the Method

    • After the elements are floating, the floating should be cleared in time. If you use the empty tag clearing Floating Method, the empty tag should follow the floating element instead of the parent element of the floating element.

 

1.22 place common classes in a separate file

    • Write the commonly used float, clear, float, Font, and color classfor a single public file. You can use the name common.css.

 

Others (Others)

1.23 CSS troubleshooting

    • When a CSS error occurs, we recommend that you set the background color or border for the label to locate the error and correct it more quickly.
    • The CSS troubleshooting method should first check whether the class attribute in the HTML code is consistent with the selector in the CSS code to avoid errors caused by missing or wrong letters.
    • In case of misplacement, you should first consider whether to fall into the various CSS traps of IE6, such as double margin.

 

 

Resource

1.24 http://www.w3.org/

    • W3C Official Website
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.