summaryrefslogtreecommitdiffhomepage
path: root/sass.html.markdown
blob: 509aee9bec9bd2ec15bf1ad67122e17628eba7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
---
language: sass
filename: learnsass.scss
contributors:
  - ["Laura Kyle", "https://github.com/LauraNK"]
---

Sass is a CSS extension language that adds features such as variables, nesting, mixins and more. 
Sass (and other preprocessors, such as [Less](http://lesscss.org/)) help developers to write maintainable and DRY (Don't Repeat Yourself) code.

Sass has two different syntax options to choose from. SCSS, which has the same syntax as CSS but with the added features of Sass. Or Sass (the original syntax), which uses indentation rather than curly braces and semicolons. 
This tutorial is written using SCSS.


```scss

	
//Single line comments are removed when Sass is compiled to CSS.

/*Multi line comments are preserved. */	
	
	
	
/*Variables
==============================*/
	
	

/* You can store a CSS value (such as a color) in a variable.
Use the '$' symbol to create a variable. */
	
$primary-color: #A3A4FF;
$secondary-color: #51527F;
$body-font: 'Roboto', sans-serif;	

/* You can use the variables throughout your stylesheet. 
Now if you want to change a color, you only have to make the change once.*/	
	
body {
	background-color: $primary-color;
	color: $secondary-color;
	font-family: $body-font;
}

/* This would compile to: */
body {
	background-color: #A3A4FF;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}


/* This is much more maintainable than having to change the color
each time it appears throughout your stylesheet. */
	


/*Mixins
==============================*/



/* If you find you are writing the same code for more than one
element, you might want to store that code in a mixin.

Use the '@mixin' directive, plus a name for your mixin.*/

@mixin center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* You can use the mixin with '@include' and the mixin name. */

div {
	@include center;
	background-color: $primary-color;
}

/*Which would compile to: */
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #A3A4FF;
}


/* You can use mixins to create a shorthand property. */

@mixin size($width, $height) {
	width: $width;
	height: $height;
}
	
/*Which you can invoke by passing width and height arguments. */

.rectangle {
	@include size(100px, 60px);
}

.square {
	@include size(40px, 40px);
}

/* This compiles to: */
.rectangle {
  width: 100px;
  height: 60px; 
}

.square {
  width: 40px;
  height: 40px; 
}




/*Extend (Inheritance)
==============================*/



/*Extend is a way to share the properties of one selector with another. */

.display {
	@include size(5em, 5em);
	border: 5px solid $secondary-color;
}

.display-success {
	@extend .display;
	border-color: #22df56;
}

/* Compiles to: */
.display, .display-success {
  width: 5em;
  height: 5em;
  border: 5px solid #51527F; 
}

.display-success {
  border-color: #22df56; 
}


	

/*Nesting
==============================*/



/*Sass allows you to nest selectors within selectors */

ul {
	list-style-type: none;
	margin-top: 2em;
	
	li {
		background-color: #FF0000;		
	}	
}

/* '&' will be replaced by the parent selector. */
/* You can also nest pseudo-classes. */
/* Keep in mind that over-nesting will make your code less maintainable.
For example: */

ul {
	list-style-type: none;
	margin-top: 2em;
	
	li {
		background-color: red;
		
		&:hover {
		  background-color: blue;
		}
		
		a {
		  color: white;
		}
	}	
}

/* Compiles to: */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



	
```	



## SASS or Sass?
Have you ever wondered whether Sass is an acronym or not? You probably haven't, but I'll tell you anyway. The name of the language is a word, "Sass", and not an acronym. 
Because people were constantly writing it as "SASS", the creator of the language jokingly called it "Syntactically Awesome StyleSheets". 


## Practice Sass
If you want to play with Sass in your browser, check out [SassMeister](http://sassmeister.com/).
You can use either syntax, just go into the settings and select either Sass or SCSS.

	
## Further reading
* [Official Documentation](http://sass-lang.com/documentation/file.SASS_REFERENCE.html)
* [The Sass Way](http://thesassway.com/) provides tutorials (beginner-advanced) and articles.