In this tutorial, we aim to help you understand the power of using SASS/SCSS's advanced built-in functions for enhancing the styling of your web pages. These functions can help you manage and manipulate your styles more effectively.
By the end of this tutorial, you should be able to:
A basic understanding of CSS and familiarity with SASS/SCSS is recommended.
SASS/SCSS provides various built-in functions that can be used for color manipulation, string manipulation, list manipulation, etc. These functions can be very useful for adding dynamicity to your styles.
Here are some examples of commonly used functions:
darken($color, $amount)
: This function takes a color and a percentage, and returns a color that is a darker version.lighten($color, $amount)
: Similar to darken
, but returns a lighter version of the color.mix($color1, $color2, [$weight])
: This function takes two colors and an optional weight, and returns a color that is a mix of the two input colors.darken
and lighten
$primary-color: #3498db;
.button {
background-color: $primary-color;
&:hover {
// Darken the button color on hover
background-color: darken($primary-color, 10%);
}
&:active {
// Lighten the button color on active
background-color: lighten($primary-color, 10%);
}
}
In the above example, we're using the darken
and lighten
functions to change the background color of a button on hover and active states.
mix
$color1: #3498db;
$color2: #2ecc71;
.button {
// Create a new color by mixing two colors
background-color: mix($color1, $color2);
}
In this example, we're using the mix
function to create a new color for the button background by combining two colors.
We have covered:
- The importance of SASS/SCSS's built-in functions and their usage.
- How to use darken
, lighten
, and mix
functions.
- Best practices when using these functions.
Now, you can try using other functions provided by SASS/SCSS like contrast
, adjust-hue
, etc.
Create a SASS/SCSS code snippet that uses the adjust-hue
function to change the hue of a color.
Create a SASS/SCSS code snippet that uses the contrast
function to set the color of text based on the background color.
// Solution 1
$color: #3498db;
.button {
// Change the hue of a color
background-color: adjust-hue($color, 60deg);
}
In this solution, we're using the adjust-hue
function to change the hue of the $color
by 60 degrees.
// Solution 2
$bg-color: #3498db;
.text {
// Set the color of the text based on the background color
color: contrast($bg-color, #000, #fff);
}
In this solution, we're using the contrast
function to set the color of the text based on the background color. If the background color is light, the text color will be black (#000
). If it's dark, the text color will be white (#fff
).