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.
- . Weather {
- @ Extends % module;
- ...
- }
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.
- . Weather {
- @ Extends % module;
- Background: LightCyan;
- ..
- }
- @ Include (s)
-
- . Weather {
- @ Extends % module;
- Background: LightCyan;
- @ Include transition (all 0.3 s outgoing-out );
- ...
- }
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.
- . Weather {
- @ Extends % module;
- Background: LightCyan;
- @ Include transition (all 0.3 s finished );
- > H3 {
- Border-bottom: 1px solid white;
- @ Include transform (rotate (90deg ));
- }
- }
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.
- . Weather {
- . "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 {
- Li {
- // No more!
- }
- }
- }
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.
- . Weather
- > H3 {
- @ Extend % line-under;
- }
- }
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.
- /* Vendor Dependencies */
- @ Import "compass ";
-
- /* Authored Dependencies */
- @ 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 ";
- @ Import "global/mixins ";
-
- /* Patterns */
- @ Import "global/tabs ";
- @ Import "global/modals ";
-
- /* Sections */
- @ Import "global/header ";
- @ 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.
- @ 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 /";
- @ Import "global/header/logo /";
- @ Import "global/header/dropdowns /";
- @ Import "global/header/nav /";
- @ 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.
- $ ZHeader: 2000;
- $ ZOverlay: 5000;
- $ ZMessage: 5050;
-
- . Header {
- Z-index: $ zHeader;
- }
- . Overlay {
- Z-index: $ zOverlay;
- }
- . Message {
- Z-index: $ zMessage;
- }
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.
- . Sidebar {
- Float: rightright;
- Width: 33.33%;
- @ Include bp (mama-bear ){
- Width: 25%;
- }
- }
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.
- @ Import "compass"
-
- ...
-
- @ 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.