Mixins

Mixins are like named templates. They are Sass's way to allowing you of group CSS or Sass declarations (that is, CSS styles) together and give it a name. This way, you can include these declarations in other CSS classes as needed, without copying and pastingwhich causes a bit of a mess should anything need to change later on. In a sense, they are also like variables because you only need to change something in one place (that is, in the mixin itself), but they are more powerful than variables, which is why I mentioned that they are like templates. In fact, mixins can even be parameterized using variables. Let's look at an example, and the preceding description should become clear as to what I mean when I say that mixins are like templates.

Here is an example of styling a dropdown that I like to use in my websites. We'll parameterize the width so that we can create different sizes of dropdowns. Note the use of the @mixin directive:

@mixin custom-dropdown($dropdown-width) {
-webkit-appearance: button;
-webkit-border-radius: 2px;
-webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
-webkit-padding-end: 20px;
-webkit-padding-start: 2px;
-webkit-user-select: none;
background-image: url(https://www.maxfusioncloud.com/static/img/15xvbd5.png),
-webkit-linear-gradient(#FAFAFA, #F4F4F4 40%, #E5E5E5);
background-position: 97% center;
background-repeat: no-repeat;
border: 1px solid #AAA;
color: #555;
font-size: 10pt;
margin: 0px;
overflow: hidden;
padding: 5px 12px 6px 6px;
text-overflow: ellipsis;
white-space: nowrap;
width: $dropdown-width;
}

And here is how we can use the mixin (note the use of the @include directive):

.small-dropdown { @include custom-dropdown(75px); }
.medium-dropdown { @include custom-dropdown(115px); }
.large-dropdown { @include custom-dropdown(155px); }

This will compile to the following CSS:

.small-dropdown {
-webkit-appearance: button;
-webkit-border-radius: 2px;
-webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
-webkit-padding-end: 20px;
-webkit-padding-start: 2px;
-webkit-user-select: none;
background-image: url(https://www.maxfusioncloud.com/static/img/15xvbd5.png),
-webkit-linear-gradient(#FAFAFA, #F4F4F4 40%, #E5E5E5);
background-position: 97% center;
background-repeat: no-repeat;
border: 1px solid #AAA;
color: #555;
font-size: 10pt;
margin: 0px;
overflow: hidden;
padding: 5px 12px 6px 6px;
text-overflow: ellipsis;
white-space: nowrap;
width: 75px;
}

.medium-dropdown {
-webkit-appearance: button;
-webkit-border-radius: 2px;
-webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
-webkit-padding-end: 20px;
-webkit-padding-start: 2px;
-webkit-user-select: none;
background-image:
url(https://www.maxfusioncloud.com/static/img/15xvbd5.png),
-webkit-linear-gradient(#FAFAFA, #F4F4F4 40%, #E5E5E5);
background-position: 97% center;
background-repeat: no-repeat;
border: 1px solid #AAA;
color: #555;
font-size: 10pt;
margin: 0px;
overflow: hidden;
padding: 5px 12px 6px 6px;
text-overflow: ellipsis;
white-space: nowrap;
width: 115px;
}

.large-dropdown {
-webkit-appearance: button;
-webkit-border-radius: 2px;
-webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
-webkit-padding-end: 20px;
-webkit-padding-start: 2px;
-webkit-user-select: none;
background-image:
url(https://www.maxfusioncloud.com/static/img/15xvbd5.png),
-webkit-linear-gradient(#FAFAFA, #F4F4F4 40%, #E5E5E5);
background-position: 97% center;
background-repeat: no-repeat;
border: 1px solid #AAA;
color: #555;
font-size: 10pt;
margin: 0px;
overflow: hidden;
padding: 5px 12px 6px 6px;
text-overflow: ellipsis;
white-space: nowrap;
width: 155px;
}

By now, you can see how cool Sass is and how much time it can save you, as well as how you can use it to avoid code duplication and avoid making the silly cut and paste mistakes.

And as if all this wasn't cool enough, Sass gives you a lot of power with its built-in functions. There are a ton of them, which is what gives Sass so much power and utility. You can take a look at them here: http://sass-lang.com/documentation/Sass/Script/Functions.html. We'll only be covering one, just to show you an example of how to use a function in the Built-in Functions section that follows.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.1.239