Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Sass lightness() Function

Posted in Sass Tutorial
Updated on Sep 29, 2024
By Mari Selvan
👁ī¸ 32 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
Sass lightness() Function

Photo Credit to CodeToFun

🙋 Introduction

The lightness() function in Sass is a valuable tool for working with colors in the HSL (Hue, Saturation, Lightness) color model. It allows you to retrieve the lightness component of a color, which represents how light or dark a color is.

This function is particularly useful when you need to analyze or adjust the lightness of colors in your stylesheets.

💡 Syntax

The syntax for the lightness() function is simple. It takes a single argument:

Syntax
Copied
Copy To Clipboard
lightness(color)

đŸ”ĸ Parameters

  • color: This is the input color value. It can be specified using various formats such as HEX, RGB, or HSL.

↩ī¸ Return Value

The function returns a percentage representing the lightness component of the specified color. This value ranges from 0% (completely dark) to 100% (completely light).

📝 Example Usage

Let's explore some examples to understand how lightness() can be used in practical scenarios.

📜 Example 1: Basic Usage

example.scss
Copied
Copy To Clipboard
$color: #3498db; // A shade of blue
$lightness-value: lightness($color);

body {
  background-color: $color;
  border: 2px solid hsl(0, 0%, $lightness-value);
}

In this example, the lightness value of the blue color #3498db is extracted and used to create a border color in grayscale with the same lightness level.

📜 Example 2: Working with HSL Colors

example.scss
Copied
Copy To Clipboard
$color: hsl(120, 50%, 40%); // A shade of green
$lightness-value: lightness($color);

p {
  color: hsl(0, 0%, $lightness-value); // Grayscale with same lightness
}

Here, a green color specified in HSL is analyzed for its lightness, and the value is used to create a grayscale text color that matches the lightness of the original color.

📜 Example 3: Conditional Styling Based on Lightness

example.scss
Copied
Copy To Clipboard
$color: #ffcc00; // A shade of yellow

@if lightness($color) > 50% {
  background-color: black;
  color: white;
} @else {
  background-color: white;
  color: black;
}

This example demonstrates how you can use the lightness() function to conditionally style elements. If the lightness of the color is greater than 50%, a dark background with light text is applied, and vice versa.

🎉 Conclusion

The lightness() function in Sass is an essential tool for any developer looking to manage color lightness dynamically within their stylesheets. By extracting the lightness component of a color, you can make informed decisions about how to adjust colors or apply conditional styling based on lightness levels.

Whether you are working on creating adaptive themes, analyzing color contrasts, or simply ensuring consistency in your designs, lightness() offers a straightforward approach to manipulating the lightness of colors. Integrating this function into your Sass toolkit can significantly enhance your ability to work with colors in a more flexible and responsive manner.

Experiment with different colors and see how lightness() can help you achieve the perfect balance in your designs.

👨‍đŸ’ģ 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