DIV CSS optimized CSS compression tips tutorial

Source: Internet
Author: User

DIV CSS Optimization

DIVCSS5 to introduce simple CSS optimizations for you.
Involves optimizing content:
Optimization of CSS code optimization, CSS reuse optimization, shortening characters, deleting line breaks, etc.

Directory

CSS style word shorthand optimization
Punctuation optimization
Remove line breaks
CSS Reuse optimization
CSS Code Optimization Shorthand Summary

CSS style attribute word code shorthand optimization-TOP

1, border (CSS border) shorthand:

1), 4 edge border width is 1px, color is #000

Border-color: #000; Border-style:solid; border-width:1px

can be abbreviated as: Body Sense music relaxation chair

border:1px solid #000;

2), single up, down, left, right box shorthand
Left:

Border-left-color: #000; Border-left-style:solid; border-left-width:1px

Shorthand:

BORDER-LEFT:1PX Solid #000

Right:

Border-right-color: #000; Border-right-style:solid; border-right-width:1px

Shorthand:

BORDER-RIGHT:1PX Solid #000音乐床垫

Top:

Border-top-color: #000; Border-top-style:solid; border-top-width:1px

Shorthand:

BORDER-TOP:1PX Solid #000

Below:

Border-bottom-color: #000; Border-bottom-style:solid; border-bottom-width:1px

Shorthand:

BORDER-BOTTOM:1PX Solid #000

3), technical shorthand border
Sometimes we can reduce the amount of code with the skill of just setting the border of 3 sides.
If we don't set the top border, and the other 3 sides are 1px, the black border is implemented.

Legacy code:

div{border-right:1px solid #000; border-bottom:1px solid #000; border-left:1px Solid #000}

Shorthand: Music Relaxation Chair

div{border:1px solid #000; border-top:0}

This achieves the same effect and greatly reduces the amount of CSS code

Learn more about CSS Border optimization compression tutorials.

2, padding (CSS padding) shorthand:

1), four-sided set padding padding properties
Traditional thinking:

padding-left:2px; padding-right:3px; padding-top:4px; padding-bottom:5px

can be CSS padding shorthand:

padding:4px 3px 5px 2px

2), only 3 side set padding body Acoustic wave treatment system
If we do not set padding on, the other 3 sides set the Padding property

Traditional:

padding-left:2px; padding-right:3px; padding-bottom:5px

Shorthand:

padding:0 3px 5px 2px

3 sides in the same situation:
Traditional:

padding-left:2px; padding-right:2px; padding-bottom:2px

Shorthand:

padding:0 2px 2px 2px;

3, margin shorthand

Margin is similar to padding abbreviation

1), four-sided spacing set abbreviation:
Traditional:

margin-left:2px; margin-right:3px; margin-bottom:5px; margin-top:4px;

Shorthand:

margin:4px 3px 5px 2px

2), four-sided set distance same abbreviation
Traditional:

margin-left:2px; margin-right:2px; margin-bottom:2px; margin-top:2px;

Shorthand:

margin:2px

3), up and down the same, around the same

margin-left:2px; margin-right:2px; margin-bottom:5px; margin-top:5px;

Shorthand:

margin:5px 2px

4, background shorthand

Background-color: #000;

can be shortened to

Background: #000;

Background-image:url (Image address)

can be abbreviated as:

Background:url (Image address)

CSS backgrounds compare commonly used style attributes, including setting a background color individually, setting the background as a picture individually, setting the background picture individually, repeating all repeats or repeating in x horizontal or Y. Next we introduce background background style common shorthand and attention.

1), set the background of a single color background optimization

Background-color: #CCC

Optimized to:

Background: #CCC

2), background for picture, x horizontal Repeat Tile

Background-image:url (http://www.divcss5.com/img201207/divcss5-logo-2012.gif);
background-position:0 0; Background-repeat:repeat-x

Shorthand:

Background:url (http://www.divcss5.com/img201207/divcss5-logo-2012.gif) repeat-x 0 0;

2), background for picture, Y repeat tile vertically

Background-image:url (http://www.divcss5.com/img201207/divcss5-logo-2012.gif);
background-position:0 0; Background-repeat:repeat-y

CSS shorthand optimizations are:

Background:url (http://www.thinkcss5.com/images2012/logo.gif) repeat-y 0 0;

3), background for picture, do not repeat tiled CSS compression

Background-image:url (http://www.divcss5.com/img201207/divcss5-logo-2012.gif);
background-position:0 0; Background-repeat:no-repeat

Shorthand:

Background:url (http://www.divcss5.com/img201207/divcss5-logo-2012.gif) no-repeat 0 0;

4), background for picture, Web page full background x and Y repeat tile

Background-image:url (http://www.divcss5.com/img201207/divcss5-logo-2012.gif);

Simplified Merge:

Background:url (http://www.divcss5.com/img201207/divcss5-logo-2012.gif);

5), the background is black, the picture is repeated horizontally to the X tile

Background-color: #CCC; Background-image:url (http://www.divcss5.com/img201207/divcss5-logo-2012.gif);
background-position:0 0; Background-repeat:repeat-x;

Simplified Merge:

Background: #CCC URL (http://www.divcss5.com/img201207/divcss5-logo-2012.gif) repeat-x 0 0;

Notice the color and the picture before and after the order.

5. Font Shorthand

font-size:12px; line-height:12px; Font-family:arial, Helvetica, Sans-serif;

can be abbreviated as:

font:12px/12px Arial, Helvetica, Sans-serif;

Second, punctuation optimization-TOP

1. Remove Extra space characters
We often write CSS code when CSS style words will be a lot of HTML space, we can in the development of CSS code, we can use the software to quickly remove the extra space characters

CSS code optimization Word punctuation
Punctuation Characters Space character optimization

Example:

div{float:left; width:100px; height:100%;}

After you delete a space:

div{float:left;width:100px;height:100%;}

CSS code that removes punctuation from spaces
Remove space to optimize compressed CSS code

Remove the space, in the development of the CSS process does not have to delete, only after the development of the use of such as DW software batch replacement to remove the extra space.

2. Remove the last semicolon for each selector

<a href= "http://www.tiganyinbo.com/" > Music mattress </a>

Remove semi-colon optimized CSS code
Remove the semicolon before the CSS code

Code:

div{float:left;width:100px;height:100%;}
. divcss5{float:left;width:100px;height:100px;}

Shorthand after removing the semicolon compression:

div{float:left;width:100px;height:100%}
. divcss5{float:left;width:100px;height:100px}

CSS code optimization Compression remove the last attribute semicolon
Remove the last semicolon
Third, remove newline-TOP

Remove spaces and line breaks, so that the code is squeezed together.

Before deleting a space
Remove newline spaces between selectors

Shorthand to delete a selector newline placeholder

CSS code:

div{float:left;width:100px;height:100%}
. divcss5{float:left;width:100px;height:100px}
/* www.divcss5.com */

Abbreviation optimization: <a href= "http://www.tiganyinyue.org/" > Music mattress </a>

div{float:left;width:100px;height:100%}.divcss5{float:left;width:100px;height:100px}
/* www.divcss5.com */

Reduce byte-optimized CSS after deletion
To remove line breaks, you can still use the DW software to delete the operation.
Four, CSS reuse optimization-TOP

Here is the main introduction of CSS code common attribute extraction to achieve saving code, maintenance convenience, CSS examples are as follows:

. yangshi_a{width:100px; height:20px; text-align:left; float:left; font-size:24px;}
. yangshi_b{width:100px; height:20px; text-align:right; float:left; font-size:24px;}

They all have the same height, width, float, text size, and only the content is left right (you may need to know the CSS is centered), we can extract their same attributes
After optimizing compression:

. yangshi_a,. yangshi_b{width:100px; height:20px; text-align:left; float:left; font-size:24px;}
. Yangshi_b{text-align:right;}

Pay attention to observe the above code, you understand, do not understand the CSS forum to ask questions.
Five, CSS code optimization shorthand summary-TOP

Based on the above several optimization shorthand, you can compress your CSS code, while reducing the need to take up byte space, semicolon, by shorthand commonly used CSS code, etc. greatly solve the compression CSS code.

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.