Css sass style programming guide _ HTML/Xhtml _ webpage Creation

Source: Internet
Author: User
This article mainly introduces the SASS style programming guide for CSS, including the use of nesting tools and comments. For more and more developers, see SASS, we need to pay attention to the number of SASS codes. We can start with the CSS (stacked style sheet) syntax to explain some of the SASS syntax's special points. After all, CSS style guide is very common.

This article mainly introduces some of the features that I personally are interested in, and may help you use them to form a set of SASS user guides.
Continue to maintain your usual CSS format rules and styles Guide

This article focuses on some SASS content, but on this basis, developers should keep their existing and good format rules. If you have not developed a set of your own format rules, there are some style guidelines to review, which should help you form your own CSS writing habits. Here, only some of the content is listed:

1. Keep the line indent consistent
2. Keep the numbers of spaces before and after colons and braces consistent
3. Keep one row with one selector and one row with one rule
4. Write relevant attributes together as much as possible
5. For the class name naming rules
6. Avoid using the CSS id Selector
7. etc.

Next, let's take a look at how to write beautiful SASS code, and write a. weather class attribute as an example:
First List @ extend (s)

Copy the content to the clipboard using CSS Code.

  1. . Weather {
  2. @ Extends % module;
  3. ...
  4. }

In this way, developers can keep a clear idea, and immediately know the relationship between this class and its attributes and other classes and their attributes, so as to maintain the consistency of attributes and clear ideas of attribute reuse.
Normal style

Copy the content to the clipboard using CSS Code.

  1. . Weather {
  2. @ Extends % module;
  3. Background: LightCyan;
  4. ..
  5. }
  6. @ Include (s)
  7. . Weather {
  8. @ Extends % module;
  9. Background: LightCyan;
  10. @ Include transition (all 0.3 s outgoing-out );
  11. ...
  12. }

In this way, developers can see at a glance the deployment of @ extend (s) and @ include (s), so that they and other developers can easily interpret the code. You may also decide whether to distinguish between custom @ shortdes and public-source @ shortdes (especially considering the reusability and timeliness of code)
Selector nesting

Copy the content to the clipboard using CSS Code.

  1. . Weather {
  2. @ Extends % module;
  3. Background: LightCyan;
  4. @ Include transition (all 0.3 s finished );
  5. > H3 {
  6. Border-bottom: 1px solid white;
  7. @ Include transform (rotate (90deg ));
  8. }
  9. }

In the nested section, continue to use the above style rules. The nested part should always be placed at the end.
 All vendor prefixes use @ mixins

The vendor prefix (CSS prefix) has a very strong timeliness. Because of the update of modern browsers, these prefixes will be used less and less. You can update the content in mixins (or some libraries used in your mixin will be automatically updated) to adapt to these changes. Even if mixin only has one row, it does not matter.
However, when some manufacturers' prefixes are privatized, these prefixes will be very difficult to standardize and other prefix or non-Prefix versions will not be worth the candle. I will choose to give up the prefixes of the vendors such as @ mixin. Such as-webkit-line-clamp,-mscontent-zoom-chaining, or similar situations.
The number of nested layers cannot exceed three.

Copy the content to the clipboard using CSS Code.

  1. . Weather {
  2. . "Width: auto; height: auto; float: none;" id = "14_nwp"> "text-decoration: none; "mpid =" 14 "target =" _ blank "href =" http://cpro.baidu.com/cpro/ui/uijs.php? Adclass = 0 & app_id = 0 & c = news & cf = 1001 & ch = 0 & di = 128 & fv = 0 & is_app = 0 & jk = be9ebff1476c47c4 & k = cities & k0 = cities & kdi0 = 0 & luki = 6 & n = 10 & p = baidu & q = 06011078_cpr & rb = 0 & rs = 1 & seller_id = 1 & sid = c4476c47f1bf9ebe & ssp2 = 1 & stid = 0 & t = tpclicked3_hc & tu = u1922429 & u = http % 3A % 2F % 2 Fwww % 2Eadmin10000% 2 Ecom % 2 Fdocument % 2f2%2 Ehtml & urlid = 0 "id =" 14_nwl ">" color: # 0000ff; font-size: 14px; width: auto; height: auto; float: none; "> cities {
  3. Li {
  4. // No more!
  5. }
  6. }
  7. }

If your nesting is redundant three times, it is very likely that you write a bad guy (poor ?) Selector. The reason is that this selector is too dependent on the HTML architecture (unstable), too detailed (too powerful, not flexible), or too reusable (not available ). At the same time, too many nested layers may cause the code to be obscure.

If there are too many class-related code sometimes, you have to use the tag selector. You may need to write a specific class to avoid unnecessary cascade. It is even possible to use extend to use some reusable features in CSS.

Copy the content to the clipboard using CSS Code.

  1. . Weather
  2. > H3 {
  3. @ Extend % line-under;
  4. }
  5. }

No more than 50 lines of nested code

If SASS contains more than 50 nested rows, it may not be completely displayed on a compiler page, which will make the code hard to read and understand. Nesting is intended to facilitate and simplify the organization of thinking and code. If it is against readability, do not nest it.
Global and regionalized SASS file sequences are equivalent to table content

In other words, they do not have any fixed style. Developers should remind themselves to keep all styles consistent and orderly.

First, list the vendor/global database, then the custom database, then the mode, and finally the database used by each branch.

In this way, the 'directory' looks as clear as in the following example:

Copy the content to the clipboard using CSS Code.

  1. /* Vendor Dependencies */
  2. @ Import "compass ";
  3. /* Authored Dependencies */
  4. @ Import "width: auto; height: auto; float: none;" id = "10_nwp"> text-decoration: none; "mpid =" 10 "target =" _ blank "href =" http://cpro.baidu.com/cpro/ui/uijs.php? Adclass = 0 & app_id = 0 & c = news & cf = 1001 & ch = 0 & di = 128 & fv = 0 & is_app = 0 & jk = be9ebff1476c47c4 & k = global & k0 = global & kdi0 = 0 & luki = 9 & n = 10 & p = baidu & q = 06011078_cpr & rb = 0 & rs = 1 & seller_id = 1 & sid = c4476c47f1bf9ebe & ssp2 = 1 & stid = 0 & t = tpclicked3_hc & tu = u1922429 & u = http % 3A % 2F % 2 Fwww % 2Eadmin10000% 2 Ecom % 2 Fdocument % 2f2%2 Ehtml & urlid = 0 "id =" 10_nwl "> color: # 0000ff; font-size: 14px; width: auto; height: auto; float: none; "> global/colors ";
  5. @ Import "global/mixins ";
  6. /* Patterns */
  7. @ Import "global/tabs ";
  8. @ Import "global/modals ";
  9. /* Sections */
  10. @ Import "global/header ";
  11. @ Import "global/footer ";

These files are like a compass. colors and mixins do not generate compiled CSS code. They are purely independent libraries. After that, the mode is introduced to make the rewrite more secure and there will be no conflict of specificity.
Divide SASS into multiple small files

This is nothing bad. When conditions permit, try to use multiple small and refined files as much as possible, so that developers can find some specific files, rather than making a haystack in a few large files with lengthy code.

...

Copy the content to the clipboard using CSS Code.

  1. @ Import "width: auto; height: auto; float: none;" id = "9_nwp"> text-decoration: none; "mpid =" 9 "target =" _ blank "href =" http://cpro.baidu.com/cpro/ui/uijs.php? Adclass = 0 & app_id = 0 & c = news & cf = 1001 & ch = 0 & di = 128 & fv = 0 & is_app = 0 & jk = be9ebff1476c47c4 & k = global & k0 = global & kdi0 = 0 & luki = 9 & n = 10 & p = baidu & q = 06011078_cpr & rb = 0 & rs = 1 & seller_id = 1 & sid = c4476c47f1bf9ebe & ssp2 = 1 & stid = 0 & t = tpclicked3_hc & tu = u1922429 & u = http % 3A % 2F % 2 Fwww % 2Eadmin10000% 2 Ecom % 2 Fdocument % 2f2%2 Ehtml & urlid = 0 "id =" 9_nwl "> color: # 0000ff; font-size: 14px; width: auto; height: auto; float: none; "> global/header /";
  2. @ Import "global/header/logo /";
  3. @ Import "global/header/dropdowns /";
  4. @ Import "global/header/nav /";
  5. @ Import "global/header/really-specific-thingy /";

I often reference these files one by one in a global scss file, instead of referencing a _ header. scss file, and then referencing them one by one in the _ header. scss file. This can reduce the indexing time and improve reading efficiency.

When too many of these files lead to too long import sequence, you may use Globbing.
Remember to name Partials AS _ partial. scss

This is a common name for files that cannot be compiled by themselves. Such files depend on other files, so that they cannot be compiled independently. I personally like to add an underline before the file name, such as _ dropdown-menu.scss
Add row ing during local Compilation

Here, this means that the development tool can tell you the source of each rule, even if it is an imported partial file.
Compile a streamlined file during deployment.

Running web pages always only require simplified CSS.
No need to hand over the. CSS file

This may take some time, but it is a wonderful thing to put the. CSS file in the inventory. The file compilation is completed during deployment. So the only thing you can see is the sass files in simplified format. This makes the description of the file very useful. The file description is used to compare some changes made by the version publisher. For the simplified .css file, the file description is basically unnecessary.
Usage notes

Few people regret leaving comments in the code. Whether it's useful or inconspicuous comments, they will eventually be erased when compiled into simplified CSS files, and there will be no additional cost for developers.

. Overlay {
/* Modals are 6000, saving messages are 5500, header is 2000 */
Z-index: 5000;
}

When it comes to annotations, you may also need to make some standard adjustments to them. In SASS, '//' is very suitable for adding comments. '//' makes comments and canceling comments very convenient.
  Convert common values and values with special meanings into variables.

If you find that you reuse a value (which is common in front-end design), You 'd better convert it into a variable. In this way, you can name the value to remind yourself of its meaning and maintain consistency during code writing. Yes, you do not need to adjust the value line by line when you change the value.

If a number has an obvious meaning, it is essential to convert it into a variable.

Copy the content to the clipboard using CSS Code.

  1. $ ZHeader: 2000;
  2. $ ZOverlay: 5000;
  3. $ ZMessage: 5050;
  4. . Header {
  5. Z-index: $ zHeader;
  6. }
  7. . Overlay {
  8. Z-index: $ zOverlay;
  9. }
  10. . Message {
  11. Z-index: $ zMessage;
  12. }

These numbers may be stored in a single file and imported as @ imported. In this way, you can perform a unified management of all z-indexes or other variables.
Convert colors into Variables

In addition to black and white. Many colors won't be used only once, even if you think you won't use it any more. But if you convert it into a variable, you may find that it is used elsewhere. For color variables, there are color functions in sass to process them, such as lighten () and darkern (). This makes it easy for you to control the overall color (one change, once and for all)
 Nesting and naming your media Repository

The function of embedding a media library in sass means 1. you do not need to override the selector elsewhere to cause unnecessary errors; 2. the rules you rewrite become clear and confusing when the code is at the end of your css code or other files.

Copy the content to the clipboard using CSS Code.

  1. . Sidebar {
  2. Float: rightright;
  3. Width: 33.33%;
  4. @ Include bp (mama-bear ){
  5. Width: 25%;
  6. }
  7. }

Here is a more detailed explanation: http://css-tricks.com/naming-media-queries/
Put the Shame at the end

Introduce a _ shame. scss file at the end of your global style sheet.

Copy the content to the clipboard using CSS Code.

  1. @ Import "compass"
  2. ...
  3. @ Import "shame"

If you need to make some quick changes, you can modify them here. If you have the proper time and energy in the future, you can
_ Shame. scss makes a better arrangement and architecture for the overall architecture modification. See http://csswizardry.com/2013/04/shame-css/ for details.
You are the dominant

Sass won't do what you didn't tell it, so the final output content of the sass file is only for people. Using sass to write css files is just like using sass. You are the dominant component of the 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.