Sass – Quick Guide

Sass – Quick Guide ”; Previous Next Sass – Overview What is SASS? SASS (Syntactically Awesome Stylesheet) is a CSS pre-processor, which helps to reduce repetition with CSS and saves time. It is more stable and powerful CSS extension language that describes the style of a document cleanly and structurally. It was initially designed by Hampton Catlin and developed by Natalie Weizenbaum in 2006. Later, Weizenbaum and Chris Eppstein used its initial version to extend the Sass with SassScript. Why to Use SASS? It is a pre-processing language which provides indented syntax (its own syntax) for CSS. It provides some features, which are used for creating stylesheets that allows writing code more efficiently and is easy to maintain. It is a super set of CSS, which means it contains all the features of CSS and is an open source pre-processor, coded in Ruby. It provides the document style in a good, structured format than flat CSS. It uses re-usable methods, logic statements and some of the built-in functions such as color manipulation, mathematics and parameter lists. Features of SASS It is more stable, powerful, and compatible with versions of CSS. It is a super set of CSS and is based on JavaScript. It is known as syntactic sugar for CSS, which means it makes easier way for user to read or express the things more clearly. It uses its own syntax and compiles to readable CSS. You can easily write CSS in less code within less time. It is an open source pre-processor, which is interpreted into CSS. Advantages of SASS It allows writing clean CSS in a programming construct. It helps in writing CSS quickly. It is a superset of CSS, which helps designers and developers work more efficiently and quickly. As Sass is compatible with all versions of CSS, we can use any available CSS libraries. It is possible to use nested syntax and useful functions such as color manipulation, mathematics and other values. Disadvantages of SASS It takes time for a developer to learn new features present in this pre-processor. If many people are working on the same site, then should use the same preprocessor. Some people use Sass and some people use CSS to edit the files directly. Therefore, it becomes difficult to work on the site. There are chances of losing benefits of browser”s built-in element inspector. Sass – Installation In this chapter, we will learn the step-by-step procedure to install Ruby, which is used for executing the SASS files. System Requirements for SASS Operating System − Cross-platform Browser Support − IE (Internet Explorer 8+), Firefox, Google Chrome, Safari, Opera Programming Language − Ruby Installation of Ruby Step 1 − Open the link https://www.ruby-lang.org/en/downloads/, you will see a screen as shown below − Download the Current stable version of the zip file. Step 2 − Next, run the setup to install Ruby on the System. Step 3 − Next, add Ruby bin folder to your PATH User Variable and System Variable to work with gem command. Path User Variable − Right Click the My Computer icon. Select Properties. Next, click the Advanced tab and click Environment Variables. In the Environment Variables window, double click the PATH as shown in the screenshot given below − You will get an Edit User Variable box as shown. Add ruby bin folder path in the Variable value field as C:Rubybin. If path is already set for other files, then put semicolon after that and add the Ruby folder path as shown below. Click the OK button. System Variable − Click the New button. Next, the New System Variable block is displayed as shown below. Enter RubyOpt in the Variable name field and rubygems in the Variable value field. After writing the Variable name and value, click the OK button. Step 4 − Open the command prompt in your system and enter the following line − gem install sass Step 5 − Next, you will see the following screen after installing SASS successfully. Example The following is a simple example of SASS. <html> <head> <title> Import example of sass</title> <link rel = “stylesheet” type = “text/css” href = “style.css”/> </head> <body> <h1>Simple Example</h1> <h3>Welcome to TutorialsPoint</h3> </body> </html> Now, we will create file as style.scss, which is quite similar to CSS and the only one difference is that it will be saved with .scss extension. Both, .htm and .scss files should be created inside the folder ruby. You can save your .scss file in the folder rubylibsass (before this process, create a folder as sass in lib directory). h1{ color: #AF80ED; } h3{ color: #DE5E85; } You can tell SASS to watch the file and update the CSS whenever SASS file changes, by using the following command − sass –watch C:rubylibsassstyle.scss:style.css When you run the above command, it will create the style.css file automatically. Whenever you change the SCSS file, the style.css file will be updated automatically. The style.css file will have the following code when you run the above given command − style.css h1 { color: #AF80ED; } h3 { color: #DE5E85; } Let us carry out the following steps to see how the above given code works − Save the above given code in hello.html file. Open this HTML file in a browser. Sass – Syntax In this chapter, we will study about SASS Syntax. SASS supports two syntaxes namely SCSS and Indented syntax. The SCSS (Sassy CSS) is an extension of CSS syntax. This means every valid CSS is a valid SCSS as well. SCSS makes much easier to maintain large stylesheets and can recognize vendor specific syntax, Many CSS and SCSS files use the extension .scss. Indented − This is older syntax and sometimes just called as SASS. Using this form of syntax, CSS can be written concisely. SASS files use the extension .sass. SASS Indented Syntax SASS Indented syntax or just SASS is an alternative to CSS based SCSS syntax. It uses indentation rather than { and } to delimit blocks.

Sass – Useful Resources

Sass – Useful Resources ”; Previous Next The following resources contain additional information on Sass. Please use them to get more in-depth knowledge on this topic. Useful Video Courses Full Stack Web Development Course Best Seller 208 Lectures 33 hours Eduonix Learning Solutions More Detail Bootstrap By Building Projects – Includes Bootstrap 4 Featured 162 Lectures 14.5 hours Eduonix Learning Solutions More Detail SASS – The Complete SASS Course (CSS Preprocessor) 51 Lectures 5.5 hours Code And Create More Detail Build Amazing Websites – HTML, CSS, Sass, JavaScript and More 124 Lectures 30 hours Juan Galvan More Detail Bootstrap 5 From scratch with 13 Projects | 4 Sass Projects 162 Lectures 31.5 hours Yossef Ayman Zedan More Detail Fundamentals of Full Stack Web Development 170 Lectures 30.5 hours Stone River ELearning More Detail Print Page Previous Next Advertisements ”;

Sass – Script

Sass – Script ”; Previous Next SASS uses a small set of extensions known as SassScript which can be included in the SASS documents to compute variables from property values and uses properties of variables, arithmetic, and other functions. SassScript can also be used with selectors and property names while using mixins (Mixins allows to re-use CSS styles throughout the stylesheet). The following table lists some of the CSS extensions used in SASS − S. No. CSS Extension & Description 1 Interactive Shell It evaluates SassScript expression using command line. 2 Variables It represents the data such as numeric values, characters or memory addresses. 3 DataTypes It declares data type for every data object. 4 Operations It provides operations such as number, color, string, boolean and list operations. 5 Parentheses It is pair of signs which are usually marked off by round brackets ( ) or square brackets []. 6 Functions It supports for the use of functions by providing some keyword arguments. 7 Interpolation It provides SassScript variables and property names using #{ } syntax. 8 & in SassScript It allows nesting of properties into another properties which leads to group of another related code. 9 Variable Defaults It allows nesting of properties into another properties which leads to group of another related code. Print Page Previous Next Advertisements ”;

Sass – @-Rules and Directives

Sass – @-Rules and Directives ”; Previous Next The following table lists all the rules and directives which you can use in SASS. S. No. Directives & Description 1 @import It imports the SASS or SCSS files, it directly takes the filename to import. 2 @media It sets the style rule to different media types. 3 @extend @extend directive is used to share rules and relationships between selectors. 4 @at-root @at-root directive is a collection of nested rules, which is able to make style block at root of the document. 5 @debug @debug directive detects the errors and displays the SassScript expression values to the standard error output stream. 6 @warn @warn directive is used to give cautionary advice about the problem; it displays the SassScript expression values to the standard error output stream. 7 @error @error directive displays the SassScript expression value as fatal error. Print Page Previous Next Advertisements ”;

Control Directives & Expressions

Sass – Control Directives & Expressions ”; Previous Next In this chapter, we will study about Control Directives & Expressions. Styling based on some conditions or applying the same style many times with variations can be accomplished by using control directives and expressions, which are supported by SassScript. These control directives are advanced options used mainly in mixins. They require considerable flexibility, as they are a part of Compass libraries. The following table lists the control directives and expressions used in SASS − S. No. Control Directive & Expression with Description 1 if() Based on the condition, if() function returns only one result from two possible outcomes. 2 @if The @if directive accepts SassScript expressions and uses the nested styles whenever the result of the expression is anything other than false or null. 3 @for The @for directive allows you to generate styles in a loop. 4 @each In @each directive, a variable is defined which contains the value of each item in a list. 5 @while It takes SassScript expressions and untill the statement evaluates to false it iteratively outputs nested styles. Print Page Previous Next Advertisements ”;

Extending Sass

Extending Sass ”; Previous Next You can extend the functionality of SASS to provide different types of features and customizations for users. To make use of these features, user should have knowledge of Ruby. Defining Custom SASS Functions You can define your own SASS functions while using Ruby API. You can add your custom functions by adding them to Ruby methods as shown in the following code − module Sass::Script::Functions def reverse(string) assert_type string, :String Sass::Script::Value::String.new(string.value.reverse) end declare :reverse, [:string] end In the code you could see, the Function, declare, specifies the argument names for the function. If it fails then it will not accept any arguments even if the function is working and it also takes arbitrary keyword arguments. You can get Ruby values by using value accessor and access the color objects by using rgb, red, green, or blue. Cache Stores SASS stores cache of parsed documents, which can be reused without parsing again. SASS uses :cache_location to write cache files on the file system. It makes compilation of SASS files faster and if you delete cached files, they will be generated again when you compile next time. You can define your own cache store by setting the :cache_store option. This will write cache files on the file system or share cache files to ruby processes or machines. SASS uses instance of subclass of Sass::CacheStores::Base to store and retrieve cache results. Custom Importers SASS uses @import to import SCSS and SASS files and passes paths to @import rule to find an appropriate path code for specified paths. SASS importers use file system for loading the code and added to the load using database or different file naming scheme. Single importer can take single file loading and can be placed in :load_paths array along with the paths of file system. While using @import, SASS looks for loaded paths, which import the path for the importer. When the path is found, the imported file is used. A user can inherit the importers from Sass::Importers::Base. Print Page Previous Next Advertisements ”;

Sass – Syntax

Sass – Syntax ”; Previous Next In this chapter, we will study about SASS Syntax. SASS supports two syntaxes namely SCSS and Indented syntax. The SCSS (Sassy CSS) is an extension of CSS syntax. This means every valid CSS is a valid SCSS as well. SCSS makes much easier to maintain large stylesheets and can recognize vendor specific syntax, Many CSS and SCSS files use the extension .scss. Indented − This is older syntax and sometimes just called as SASS. Using this form of syntax, CSS can be written concisely. SASS files use the extension .sass. SASS Indented Syntax SASS Indented syntax or just SASS is an alternative to CSS based SCSS syntax. It uses indentation rather than { and } to delimit blocks. To separate statements, it uses newlines instead of semicolons(;). Property declaration and selectors must be placed on its own line and statements within { and } must be placed on new line and indented. For instance, consider the following SCSS code − .myclass { color = red; font-size = 0.2em; } The indented syntax is an older syntax, which is not recommended for use in new Sass files. If you use this file, it will display error in the CSS file as we have used = instead of for setting properties and variables. Compile the above given code using the following command − sass –watch C:rubylibsassstyle.scss:style.css Next, run the above command; it will display an error in style.css file as shown below − Error: Invalid CSS after ” color = red”: expected “{“, was “;” on line 2 of C:rubylibsassstyle17.scss 1:.myclass { 2: color = red; 3: font-size = 0.2em; 4:} Syntax Differences of SASS Most CSS and SCSS syntaxes work perfectly in SASS. However, there are some differences, which are explained in the following sections − Property Syntax CSS properties can be declared in two ways − Properties can be declared similar to CSS but without semicolon(;). colon(:) will be prefixed to every property name. For instance, you can write as − .myclass :color red :font-size 0.2em Both the above ways (properties declaration without semicolon and colon prefixed to property name) can be used, by default. However, only one property syntax is allowed to specify when you use the :property_syntax option. Multiline Selectors In Indented syntax, selectors can be placed on a newline whenever they appear after commas. Example The following example describes the use of multiline selectors in the SCSS file − <html> <head> <title>Multiline Selectors</title> <link rel = “stylesheet” type = “text/css” href = “style.css” /> <link rel = “stylesheet” href = “https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css”> <script src = “https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”></script> <script src = “https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js”></script> </head> <body> <h2>Example using Multiline Selectors</h2 > <p class = “class1”>Welcome to Tutorialspoint!!!</p> <p class = “class2”>SASS stands for Syntactically Awesome Stylesheet…</p> </body> </html> Next, create file style.scss. Note the .scss extension. style.scss .class1, .class2{ color:red; } You can tell SASS to watch the file and update the CSS whenever SASS file changes, by using the following command − sass –watch C:rubylibsassstyle.scss:style.css Next, execute the above given command, it will create the style.css file automatically with the following code − The generated style.css is as shown below − style.css .class1, .class2 { color: red; } Output Let us carry out the following steps to see how the above given code works − Save the above given html code in multiline_selectors.html file. Open this HTML file in a browser, an output is displayed as shown below. Comments Comments take up an entire line and enclose all the text nested under them. They are line-based in indented syntax. For more information about comments, refer this link. @import In SASS the @import directive can be written with/without quotes. Unlike in SCSS, they must be used with quotes. For instance, in SCSS the @import directive can be used as − @import “themes/blackforest”; @import “style.sass”; This can be written in SASS as − @import themes/blackforest @import fontstyle.sass Mixin Directives SASS supports shorthand for directives like @mixin and @include. Instead of @mixin and @include you can use = and + characters, which require less typing and makes your code simpler, and easier to read. For instance, you can write the mixin directives as − =myclass font-size: 12px; p +myclass The above given code is the same as − @mixin myclass font-size: 12px; p @include myclass Deprecated Syntax SASS supports the use of some old syntax. However, using this syntax in SASS is not recommended. Warning will be displayed if you use this syntax and it is removed in later versions. Some of the old syntaxes are shown in the following table. S. No. Operator & Description 1 = It was used instead of : when setting variables and properties to values of SassScript. 2 ||= It was used instead of : whenever you are assigning default value of a variable. 3 ! Instead of $, ! was used as variable prefix. Functionality will not be changed when it is used instead of $. Print Page Previous Next Advertisements ”;

Sass – Overview

Sass – Overview ”; Previous Next What is SASS? SASS (Syntactically Awesome Stylesheet) is a CSS pre-processor, which helps to reduce repetition with CSS and saves time. It is more stable and powerful CSS extension language that describes the style of a document cleanly and structurally. It was initially designed by Hampton Catlin and developed by Natalie Weizenbaum in 2006. Later, Weizenbaum and Chris Eppstein used its initial version to extend the Sass with SassScript. Why to Use SASS? It is a pre-processing language which provides indented syntax (its own syntax) for CSS. It provides some features, which are used for creating stylesheets that allows writing code more efficiently and is easy to maintain. It is a super set of CSS, which means it contains all the features of CSS and is an open source pre-processor, coded in Ruby. It provides the document style in a good, structured format than flat CSS. It uses re-usable methods, logic statements and some of the built-in functions such as color manipulation, mathematics and parameter lists. Features of SASS It is more stable, powerful, and compatible with versions of CSS. It is a super set of CSS and is based on JavaScript. It is known as syntactic sugar for CSS, which means it makes easier way for user to read or express the things more clearly. It uses its own syntax and compiles to readable CSS. You can easily write CSS in less code within less time. It is an open source pre-processor, which is interpreted into CSS. Advantages of SASS It allows writing clean CSS in a programming construct. It helps in writing CSS quickly. It is a superset of CSS, which helps designers and developers work more efficiently and quickly. As Sass is compatible with all versions of CSS, we can use any available CSS libraries. It is possible to use nested syntax and useful functions such as color manipulation, mathematics and other values. Disadvantages of SASS It takes time for a developer to learn new features present in this pre-processor. If many people are working on the same site, then should use the same preprocessor. Some people use Sass and some people use CSS to edit the files directly. Therefore, it becomes difficult to work on the site. There are chances of losing benefits of browser”s built-in element inspector. Print Page Previous Next Advertisements ”;

Sass – Function Directives

Sass – Function Directives ”; Previous Next In this chapter, we will study about Function Directives. In SASS, you can create your own function and use them in your script context or can be used with any value. Functions are called by using the function name and with any parameters. Example The following example demonstrates the use of function directive in the SCSS file − function_directive.htm <html> <head> <title>Nested Rules</title> <link rel = “stylesheet” type = “text/css” href = “style.css” /> <link rel = “stylesheet” href = “https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css”> <script src = “https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”></script> <script src = “https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js”></script> </head> <body> <div class = “container” id = “set_width”> <h2>Example for Function directives</h2> <p>SASS stands for Syntactically Awesome Stylesheet. </p> </div> </body> </html> Next, create file style.scss. style.scss $first-width: 5px; $second-width: 5px; @function adjust_width($n) { @return $n * $first-width + ($n – 1) * $second-width; } #set_width { padding-left: adjust_width(10); } You can tell SASS to watch the file and update the CSS whenever SASS file changes, by using the following command − sass –watch C:rubylibsassstyle.scss:style.css Next, execute the above command; it will create the style.css file automatically with the following code − style.css #set_width { padding-left: 95px; } Output Let us carry out the following steps to see how the above given code works − Save the above given html code in function_directive.html file. Open this HTML file in a browser, an output is displayed as shown below. In the output, you can see that the left-padding is being applied. Just like mixin, function can also access globally defined variables and can also accept parameters. You should call the return value for the function by using @return. We can call the SASS-defined functions by using keyword parameters. Call the above function as shown below. #set_width { padding-left: adjust_width($n: 10); } Naming Conventions To avoid naming conflicts function names can be prefixed so that they can be easily differentiated. Like mixins, variable arguments are also supported by user-defined functions. Functions and other SASS identifiers can use underscores(_) and hyphens(-) interchangeably. For example, if a function is defined as adjust_width, it can be used as adjust-width, and vice versa. Print Page Previous Next Advertisements ”;

Sass – Output Style

Sass – Output Style ”; Previous Next In this chapter, we will study about SASS Output Style. The CSS file that the SASS generates consists of default CSS style, which reflects the structure of document. The default CSS styling is good but might not be suitable for all situations; on other hand, SASS supports many other styles. It supports the following different output styles − :nested Nested style is default styling of SASS. This way of styling is very useful when you are dealing with large CSS files. It makes the structure of the file more readable and can be easily understood. Every property takes its own line and indentation of each rule is based on how deeply it is nested. For instance, we can nest the code in SASS file as shown below − #first { background-color: #00FFFF; color: #C0C0C0; } #first p { width: 10em; } .highlight { text-decoration: underline; font-size: 5em; background-color: #FFFF00; } :expanded In expanded type of CSS styling each property and rule has its own line. It takes more space compared to the Nested CSS style. The Rules section consists of properties, which are all intended within the rules, whereas rules does not follow any indentation. For instance, we can expand the code in the SASS file as shown below − #first { background-color: #00FFFF; color: #C0C0C0; } #first p { width: 10em; } .highlight { text-decoration: underline; font-size: 5em; background-color: #FFFF00; } :compact Compact CSS style competitively takes less space than Expanded and Nested. It focuses mainly on selectors rather than its properties. Each selector takes up one line and its properties are also placed in the same line. Nested rules are positioned next to each other without a newline and the separate groups of rules will have new lines between them. For instance, we can compact the code in the SASS file as shown below − #first { background-color: #00FFFF; color: #C0C0C0; } #first p { width: 10em; } .highlight { text-decoration: underline; font-size: 5em; background-color: #FFFF00; } :compressed Compressed CSS style takes the least amount of space compared to all other styles discussed above. It provides whitespaces only to separate selectors and newline at the end of the file. This way of styling is confusing and is not easily readable. For instance, we can compress the code in SASS file as shown below − #first { background-color:#00FFFF;color:#C0C0C0 } #first p { width:10em } .highlight { text-decoration:underline;font-size:5em;background-color:#FFFF00 } Print Page Previous Next Advertisements ”;