CSS Basics Tutorial: Streamlining your CSS code

Source: Internet
Author: User

In the WebFront-end project development (http://www.maiziedu.com/course/web/), you willCSScode to streamline, reduce code redundancy, improve page loading speed, but also more convenient for later maintenance, look at the followingCSSHow to streamline your code:Use the shorthand attribute and the default value. header {margin-top:10px;  margin-right:20px;  margin-bottom:30px; margin-left:40px;} /*You can use the Shorthand propertymargin * /. header {margin:10px 20px 30px 40px;/* 4the values correspond to the directions of eachtop Right Bottom left * /}/* ==========I'm a split line .========== */. nav-list {font-style:italic;  Font-variant:small-caps;  Font-weight:bold;  font-size:14px;  Font-family:georgia, Serif; line-height:24px;} /*You can use the Shorthand propertyfont * /. nav-list {font:italic small-caps bold 14px/24px Georgia, Serif;} Common shorthand properties includeAnimation,background,Border,Font,margin,padding,transition ...Use the abbreviated Fontproperty, if you do not use theiconYou must specify at least one of the keywordsfont-sizeand thefont-familyproperty, andfont-sizemust be located infont-familybefore, the other properties(asfont-weight,font-style,font-variant)default values are automatically used if not specified and must be located in thefont-sizebefore theLine-heightmust be located infont-sizeafter, and andfont-sizewith"/"DelimitedWhen a partial shorthand attribute value is omitted, the missing part uses the property's default value, div {font:14px Serif;} /*equivalent*/div {font:normal normal normal 14px/normal Serif;/* 1 */}/*** 1.missing from hereFont-style/font-variant/font-weight/line-height*will be resolved to the default valueNormal,The default value even overrides the inherited value*There's a difference between this and the following separate statements.*of the following codeFont-style/font-variant/font-weight/line-heightdoes not affirm that*is inherited from the parent element, which is theInherit*/div {font-family:serif; font-size:14px;} If the shorthand attribute is used!important, all of the child properties under the shorthand attribute are set to be important. You can specify a border with no color to achieve the change effect a {border:1px solid; /*border color defaults to the same font color*/color:red;}  a:hover {color:green; /*The font color changes and the border color changes as well.*/The transition effect can be reduced to transition:0.4s only by specifying a duration; /* Transition-durationif not specified, the default is0s, it will be ineffective.*//* equivalent*/
Transition:all 0.4s Ease 0s;background-positionThe default value is0% 0%,background-position:0% 0%; In the use of shorthandbackgroundattribute can be omitted, if only one is omitted, then the omitted one will become50%/center,background-position:0%;/*equivalent*/background-position:0% 50%; The omitted part of a few exceptions does not use the default value, but is copied from the existing value (so to be understood), such as includingBorder-color, Border-style, Border-width, Border-radius, margin, padding ...These attributes are characterized by a property that usually has4values correspond to each other4in a different direction,border-width:1px; /* =/border-width:1px 1px 1px 1px; /*Copy3Times*/margin:10px 20px; /* =/margin:10px 20px 10px 20px; /*Copy1Times*/padding:10px 20px 30px; /* =/padding:10px 20px 30px 20px; /*Copy the middle one and put it in the last*/There are some user-agent (browser) style sheets already defined styles, and in all modern browsers are consistent, there is no need to redefine, div {display:block;/*It doesn't make any sense .*/Conclusion: Some properties if you expect the value to be the same as the default value or the value specified by the user-agent style sheet, then you can omit the write-off and avoid duplication. Reasonable use of inheritance typically, descendants of an element that specifies a style automatically inherit certain style attributes, such asColor. Content h1,.content div,.content p,.content ul,.content li {color: #666;} /*it's easier .*/. Content {color: #666;} Note that some elements come with a browser default style, such asH1of thefont-size, the default style overrides the inherited valueCommon inheritable properties includecolor, font, text-align, text-indent ...You can also enforce inheritance effects by forcing an attribute that cannot be inherited, div {padding:inherit;/*it inherits itself, but its descendants are still not inherited .*/}, you can also enforce that a property that should inherit is not inherited, div {font-size:initial;/*revert to default value*/Because of compatibility issues, these two values are not very common. Conclusion: If a value can be obtained by inheritance, it is omitted. Group selector h1 {Color:blue;} h2 {Color:blue;} h3 {Color:blue;} h4 {Color:blue;} h5 {Color:blue;} h6 {Color:blue;} /*it's easier .*/H1, H2, H3, H4, H5, h6 {color:blue;} Groups of selectors with many of the same styles can be used to streamline the code effectively.  For example, there are two unrelated things defined here. Badge {background-color:orange;  border-raidus:5px;  Color: #fff; font-size:13px;}  There are many other styles in the middle. Label {background-color:orange;  border-raidus:5px;  Color: #fff; font-size:12px;}  The contrast found that there are many identical styles, putting the same style group after the. Badge,.label {background-color:orange;  border-raidus:5px; Color: #fff;} There are many other styles in the middle. Badge {/*If you look at it here, it doesn't look like there are other styles .*/font-size:13px;} There are many other styles in the middle. Label {/*If you look at it here, it doesn't look like there are other styles .*/font-size:12px;} This makes the style of the same component scattered everywhere, and maintenance can become difficult to useCSSthe preprocessor can effectively solve this problem, such asSassof the%placeholder syntax, almost for the group to choose the Quantum of the body built,%label {background-color:orange;  border-raidus:5px; Color: #fff;} There are many other styles in the middle. Badge {/*Here you can see clearly that there are other styles in this class,*/@extend%label; font-size:13px;} There are many other styles in the middle. Label {/*It is also clear from here that there are other styles in this class,*/@extend%label; font-size:12px;} The group selector needs to be careful not to group browser-compatible selectors together, resulting in unrecognized and ignoring the entire rule set, the following code cannot be grouped together, Input::-webkit-input-placeholder {color: #999;}  Input::-moz-placeholder {color: #999; Opacity:1;} Input:-ms-input-placeholder {color: #999;} HexadecimalRGBColor ValuesIf every two bits are equal, you can abbreviate the color: #ff3300;/*can be simplified to, it represents the same color*/
Color: #f30;
Use shorter specific values instead of longer keywords H2 {font-weight:700;} p {font-weight:400;} /*equivalent*/h2 {Font-weight:bold;} p {font-weight:normal;} Digital -equivalent toNormal, while theequivalent toBold. Simplified background image path background picture path may be like this, Background-image:url (". /.. /images/bg.gif "); background images andCSSWhat role does the file play? Both are inherently closely related, so you should put the background picture folder andCSSfile in the same directory, the path will becomeBackground-image:url ("Images/bg.gif"); even if the picture folder is not necessarily named plural, if there are no spaces or some special symbols in the path, the quotation marks can be removed, Background-image:url ( Img/bg.gif); Further, name the picture folder with a letter, Background-image:url (i/bg.gif); Although the name of the folder has no semantics, but usually in this directory is commonly used in two folders only, A picture folder, a font folder, and possibly a folder where other files are placed, this approach can reduce the amount of code bytes in a relatively large number of background images.
Can be inSassSave the path as a variable to facilitate arbitrary switching,$BG-path: ". /images "!default;...background-image:url (#{bg-path}/bg.gif);0units of Valuesmargin:0px;/*to be0the values with no units are0 * /margin:0; Firefoxtemporarily does not support removing the0The unit of time value, which meansTransition:color 0.5s linear 0; /*CurrentFirefox (28.0)This property is ignored*/Rather than specifying such a value at all, the default value is typically0secondsTransition:color 0.5s linear; /*finished*/Remove both ends of floating-point numbers0div {Background-color:rgba (0,0,0,0.3); opacity:0.9} for opacity, remove the preceding decimal point0can also be well understood, as it will not be greater than1div {Background-color:rgba (0,0,0,.3); Opacity:. 9;} But for others that may be greater than1floating-point value, it might make other people think you're forgetting the number in front of the decimal point,Transition:all. 5s; removeIDQualifier in front of selectorIdIt would have been unique, and it would have been meaningless to add element-and ancestor-elements in front of it.. container Div#box {}/*after streamlining*/#box {} The content below is somewhat hate meaning to remove the old browser compatible code body {text-align:center;}.  container {margin:0 auto;text-align:left; width:960px;} /*The code above is designed to implement the weird modeIEas wellie5.xthe center effect of The block element*/. container {margin:0 auto; width:960px;} Always use standard mode, nowIE6/7/8Have to face the elimination. Remove the extra browser prefixes that are still in yourCSSDo you write a lot of browser vendor prefixes in your code? Then you'll outout! . header {-webkit-border-radius:5px; -moz-border-radius:5px; /* 1 */-ms-border-radius:5px; /* 2 */-o-border-radius:5px; /* 3 */border-radius:5px;} /*** 1.new version ofFirefoxis no longer supported-moz-border-radiusProperties,*at the same time, fromFirefox 4.0began to support the standardBorder-radiusthe wording. * 2.IE +support for StandardBorder-radiusnotation,IE 8and lower versions are not supported in any notation. * 3.Opera 10.50+support for StandardBorder-radiusnotation*after changing the coreOperaIt also supports-webkit-border-radiusnotation*/.header {-webkit-border-radius:5px; border-radius:5px;} /*further*/. header {border-radius:5px;/* 4 */}/*** 4.alsoAndroid 2.1+, IOS 4.0+, Safari 5+all support StandardBorder-radiusnotation*/Can be usedSassDefine aMixinto finish this thing,@mixin Border-radius ($radius) {-webkit-border-radius: $radius; Border-radius: $radius;}. Header {@include Border-radius (5px);} Use LessSimilarly,. Border-radius (@radius) {-webkit-border-radius: @radius; Border-radius: @radius;}. Header {. Border-radius (5px);} Use the usualCSS3properties are all encapsulated intoMixin, you can reduce the amount of code, but also very well control which browser prefixes should be removed, and which should be retained, when a prefix is no longer needed can be easily deleted, more information please refer tohttp://css3please.com/, where you can see more clearly what browser vendor prefixes a property requires, or you can refer to the following items, all with a very maturemixinsfor use,

CSS Basics Tutorial: Streamlining your 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.