You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Sass, a <dfn>mixin</dfn> is a group of CSS declarations that can be reused throughout the style sheet.
12
12
13
-
Newer CSS features take time before they are fully adopted and ready to use in all browsers. As features are added to browsers, CSS rules using them may need vendor prefixes. Consider `box-shadow`:
13
+
Here is an example: Say you want to define individual styles for some headings.
14
14
15
-
```scss
16
-
div {
17
-
-webkit-box-shadow: 0px0px4px#fff;
18
-
-moz-box-shadow: 0px0px4px#fff;
19
-
-ms-box-shadow: 0px0px4px#fff;
20
-
box-shadow: 0px0px4px#fff;
15
+
```html
16
+
<styletype='text/scss'>
17
+
h1 {
18
+
margin: 10px;
19
+
padding: 20px;
20
+
color: red;
21
21
}
22
+
23
+
#another-heading {
24
+
margin: 20px;
25
+
padding: 40px;
26
+
color: blue;
27
+
}
28
+
29
+
.yet-another-heading {
30
+
margin: 10px;
31
+
padding: 30px;
32
+
color: orange;
33
+
}
34
+
</style>
35
+
36
+
<h1>Title</h1>
37
+
<h2id="another-heading">
38
+
Another heading
39
+
</h2>
40
+
<h3class="yet-another-heading">
41
+
Yet Another Heading
42
+
</h3>
22
43
```
23
44
24
-
It's a lot of typing to re-write this rule for all the elements that have a `box-shadow`, or to change each value to test different effects. Mixins are like functions for CSS. Here is how to write one:
45
+
It's a lot of typing to re-write the rules for all the elements, or to change each value to test different effects. Mixins are like functions for CSS. Here is how to write one:
The definition starts with `@mixin` followed by a custom name. The parameters (the `$x`, `$y`, `$blur`, and `$c` in the example above) are optional. Now any time a `box-shadow` rule is needed, only a single line calling the mixin replaces having to type all the vendor prefixes. A mixin is called with the `@include` directive:
55
+
We can now re-write the HTML code above like this:
The definition starts with `@mixin` followed by a custom name. A mixin’s name can be any custom name as long as it doesn’t begin with --. The parameters (the `$h-margin`, `$h-padding`, and `$h-color` in the example above) are optional. Now any time you want to define individual styles for some headings, only a single line calling the mixin replaces having to type all the individual style codes. A mixin is called with the `@include` directive:
36
88
37
89
```scss
38
-
div {
39
-
@includebox-shadow(0px, 0px, 4px, #fff);
90
+
h1 {
91
+
@includeheading-style(10px, 20px, red);
40
92
}
41
93
```
42
94
43
95
# --instructions--
44
96
45
-
Write a mixin for `border-radius` and give it a `$radius` parameter. It should use all the vendor prefixes from the example. Then use the `border-radius` mixin to give the `#awesome` element a border radius of `15px`.
97
+
Write a mixin named `shape` and give it 3 parameters: `$w`, `$h`, and `$b-color`. Then usethe `shape` mixin to give the `#square`elementa width and height of `50px`, and the color `red`. For the `#rect-a`elementa width of `100px`, a height of `50px`, and the color `blue`. Finally, for the `#rect-b`elementawidth of `50px`, a height of `100px`, and the color `orange`.
46
98
47
99
# --hints--
48
100
49
-
Your code should declare a mixin named `border-radius` which has a parameter named`$radius`.
101
+
Your code should declare a mixin named `shape` which has 3 parameters: `$w`, `$h`, and `$b-color`.
Your code should include the general `border-radius` rule that uses the `$radius` parameter.
129
+
Your code should call the `shape` mixin using the `@include` keyword for the `#square`element, setting it to a width and height of `50px`, and the color `red`.
Your code should call the `shape` mixin using the `@include` keyword for the `#rect-a`element, setting it to a width of `100px`, a height of `50px`, and the color `blue`.
Your code should call the `border-radius mixin` using the `@include` keyword, setting it to `15px`.
141
+
Your code should call the `shape` mixin using the `@include` keyword for the `#rect-b`element, setting it to a width of `50px`, a height of `100px`, and the color `orange`.
0 commit comments