Designers often create different sizes and decorations of the site buttons. Some of them can be repeating, some are not. It would be awesome to create system for quickly adding and editing buttons, sass @extend’s is the right choice. It’s illustration of buttons on typical project:
HTML for buttons would be constant throughout the entire article:
Bad way
The first idea that may come to mind of developer is to write styles for buttons into classes.
SCSS:
We have created styles for our main class, now let’s create color modifiers(variables used for colors):
Now it’s quietly easy and clear. But we met new button on another page.
This button has the same dimensions as the previous one, but another decoration. Let’s break DRY-concept and copy styles from previous buttons:
Then we should add decor styles:
Then we should add block styles:
Now we have this in the stylesheet:
Then we met new button on another page:
It has the same decor, but another dimensions and font-size. We have two ways now:
We can continue to copy styles for different buttons and add unique classes
We can create special classes for buttons styles and add it to our tags
All of these ways have disadvantages. In first case we copy too much code, and when we need change something we must change it in different places(it’s very boring). In another case we add to tags presentation classes, it’s bad too(Bootstrap lovers will be against).
But we have third way(right way) - using sass @extends(or it’s analogs in another preprocessors).
The right way
Put your button styles to the new file - buttons.scss, It’s very flexible. We will separate it on dimensions(+typography) and color schemes. Let’s create classes for dimensions:
Then we create color schemes:
Now we have flexible system for buttons organization. Our styles would be like this:
If we need new color scheme - we just create @extend for it and use. If we change current scheme - edit in one place.
Key points
Use @extends instead of @include(less css result size)
If you have 1-2 buttons on your site, this system would be redundant.
Use display:block in your extends, you can overwrite it in selector rules