Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Sass Color

Sass darken() Function

Posted in Sass Tutorial
Updated on Sep 30, 2024
By Mari Selvan
πŸ‘οΈ 54 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
Sass darken() Function

Photo Credit to CodeToFun

πŸ™‹ Introduction

The darken() function in Sass is a useful tool for color manipulation, allowing developers to reduce the lightness of a color by a specified percentage.

This function is commonly used to create darker shades of a base color, which can be particularly helpful in designing themes, creating hover effects, and ensuring sufficient contrast between UI elements.

πŸ’‘ Syntax

The syntax of the darken() function is simple and intuitive. It requires two arguments:

Syntax
Copied
Copy To Clipboard
darken(color, amount)

πŸ”’ Parameters

  • color: The input color you want to darken (e.g., #3498db, rgb(52, 152, 219), hsl(204, 70%, 53%)).
  • amount: A percentage value that indicates how much to darken the color (e.g., 20%).

↩️ Return Value

The function returns a color with its lightness reduced by the specified amount, making it darker.

πŸ“ Example Usage

Here are some practical examples to demonstrate how the darken() function can be used in your Sass projects.

πŸ“œ Example 1: Basic Usage

example.scss
Copied
Copy To Clipboard
$base-color: #3498db; // Light Blue
$darker-color: darken($base-color, 15%);

.header {
  background-color: $base-color;
  border-bottom: 2px solid $darker-color;
}

In this example, the light blue color #3498db is darkened by 15%. The darker shade is then used for the border of a header element, providing a subtle contrast.

πŸ“œ Example 2: Darkening a Hover State

example.scss
Copied
Copy To Clipboard
$button-color: #e74c3c; // Red

.button {
  background-color: $button-color;
  
  &:hover {
    background-color: darken($button-color, 10%);
  }
}

Here, the base red color is darkened by 10% when a button is hovered over. This creates a visual effect that highlights interactivity.

πŸ“œ Example 3: Generating Multiple Shades

example.scss
Copied
Copy To Clipboard
$primary-color: #2ecc71; // Green

@for $i from 1 through 5 {
  .darken-#{$i} {
    background-color: darken($primary-color, $i * 5%);
  }
}

This example uses a loop to generate multiple classes, each with a progressively darker shade of the base green color. This approach is useful for creating a range of shades for different UI components.

πŸŽ‰ Conclusion

The darken() function in Sass is an essential tool for any developer looking to fine-tune color schemes. By adjusting the lightness of colors, you can create darker shades that enhance your design’s depth and visual hierarchy. Whether you’re styling buttons, borders, backgrounds, or text, darken() offers a straightforward way to ensure your colors are exactly how you need them.

Understanding how to use darken() effectively can lead to more dynamic and visually appealing designs. Experiment with different percentages to see how darkening affects your chosen colors and find the perfect balance for your projects.

❌

Deprecated

The darken() function in Sass has been deprecated and is no longer recommended for use. It is advised to switch to newer color manipulation functions to ensure compatibility with future versions of Sass.

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy