SASS
( SCSS
), Less
on the habrahabr are already full, but in my opinion there is not enough one in which there would be some real practice. I will try to fill this gap. About a year ago, one of the SASS
articles “hooked” me, and since then I have loved this technology so much that manual typing of regular CSS
code seems to me a waste of time. This article is dedicated to those layout makers (or web programmers) who have not heard about SCSS yet, or have not tried it in action yet. More experienced comrades, I believe, there is nothing to catch in it.SCSS
is a "dialect" of the SASS language. What is SASS? SASS is a language similar to HAML (a very concise template engine), but intended to simplify the creation of CSS code. Simply put, SASS is a language whose code is translated by a special ruby ​​program into regular CSS code. The syntax of this language is very flexible, it takes into account many details that are so desirable in CSS. Moreover, it even has logic (@if, each ), mathematics (you can add both numbers, strings, and colors). Perhaps some of the SCSS features will seem redundant to you, but, in my opinion, they will not be superfluous, they will remain "in reserve". $blue: #3bbfce $margin: 16px .content-navigation border-color: $blue color: darken($blue, 9%) .border padding: $margin / 2 margin: $margin / 2 border-color: $blue
$blue: #3bbfce; $margin: 16px; .content-navigation { border-color: $blue; color: darken($blue, 9%); } .border { padding: $margin / 2; margin: $margin / 2; border-color: $blue; }
sass-gem
( gem install sass
in the console). If everything went smoothly, now the console program sass
is available to you. You can read about all the nuances of its use here - sass --help
. I will tell only about two basic possibilities:--watch
key, the program will monitor the files you specified. In case of their change, it will automatically rebuild all necessary css-files (not all in general, but only those associated with the modified ones). -- css ---- scss ------ style.scss ---- style.css
css/scss/*
and save the result in css/*.css
. In this case, run sass like this - sass --watch css/scss:css/.
. Those. sass --watch []:[]
.--watch
use --update
. No surveillance will be conducted, as well as checks for the need to update.Less
, SASS
not able to compile its code directly in the browser. In my opinion, this approach (compilation on a server or a PC-coder) is the only correct one.@import
._
. Those. the presence of the style.scss
file will create style.css
, and the presence of the _some.scss
file _some.scss
not._template.scss
or template.scss
we write @import "template"; //
style.css
file, I got more than a hundred small scss
files. At first glance it may seem that such an amount is too large and will lead to terrible agony. However, I find the file I need right away based on a convenient directory structure. In addition, I believe that due to caching such a "scheme" is more productive. #some { border: 1px solid red; .some { background: white; } } /* => */ #some { border: 1px solid red; } #some .some { background: white; }
input { border: 1px solid gray; background: white; &[type=text] { color: black; } &.some_class { display: none; } } /* => */ input { border: 1px solid gray; background: white; } input[type=text] { color: black; } input.some_class { display: none; }
&
symbol is equivalent to the parent selector. Suppose the <body> tag has a class of ie_7
, if we have Internet Explorer 7
as a browser. The following code allows you to get rid of all the "hacks" and special comments: $IE_7: 'body.ie_7'; //... .some { display: inline-block; #{$IE_7} & { zoom: 1; display: inline; } } /* => */ .some { display: inline-block; } body.ie_7 .some { zoom: 1; display: inline; }
$some: red;
_const.scss
file, which contains all the basic colors, font sizes, etc. $link: #15157d; $link_bottom: $link; $input_font_size: 13px; $content_bg: #F1F1F1; $input_color: #4E4D4D; $input_color_placeholder: #959595; $text_color: black; ...
$link
. a { color: $link; } span.link { color: $link; text-decoration: underline; }
contacts
, the color of the links is different. There are at least two solutions. $contacts_link: orange; // $contacts_link $link
$__link: $link; $link: orange; // $link: $__link;
.block { $block_width: 500px; padding: 5px; border: 1px solid black; width: $block_width - ( 5px * 2 ) - ( 1px * 2 ); }
.block { $count: 10; $margin_left: 5px; $all_width: 1000px; width: $all_width; .sub_element { width: ( $all_width / $count ) - $margin_left; margin: 0 0 0 $margin_left; } }
.some { $color: #010203; color: $color; border-color: $color - #010101; &:hover { color: #010203 * 2; } } /* => */ .some { color: #010203; border-color: #000102; } .some:hover { color: #020406; }
opacify
and transparentize
are also available (in more detail ).#{}
$image_dir: 'images/web/'; $page: 12; .some { background-image: url( $image_dir + 'pen.gif' ); &:before { content: " #{ $page }!"; } } /* => */ .some { background-image: url("images/web/pen.gif"); } .some:before { content: " 12!"; }
@mixin
- custom functions@if
- conditions@each
- cyclesSource: https://habr.com/ru/post/140612/
All Articles