Utilizing HSL Colours In CSS

No Comments

From my expertise, many of the colours I see individuals utilizing in CSS are hex, and RGB. Not too long ago, I began seeing extra utilization for HSL colours. Nevertheless, I nonetheless suppose that HSL is missed, and when it’s getting used to its full potential, it might assist us in working higher with colours in CSS.

Introduction

Often, we use hexadecimal shade codes (hex colours) that are tremendous, however they’ve a few points:

They’re limiting;
They’re laborious to know from studying them.

By “restricted”, I imply that it’s not straightforward to change the colour with out opening a shade wheel and selecting a shade your self. Including on that, it’s not straightforward to guess what shade is from wanting on the hex code.

Take into account the next determine:

I picked a hex shade for a sky blue, and a darker one. Discover that the hex colours are usually not associated to one another. It’s laborious to inform that they’re each blue however with completely different shades.

In a real-life situation, you would possibly have to create a lighter or darker shade of a shade to rapidly take a look at or validate one thing. With hex colours, this isn’t attainable till you open the colour picker.

Fortunately, HSL colours may help us in fixing this particular drawback, and it opens plenty of potentialities for us.

What Is HSL?

HSL stands for hue, saturation, and lightness. It’s primarily based on the RGB shade wheel. Every shade has an angle and a proportion worth for the saturation and lightness values.

Let’s take an instance of the sky blue shade that we mentioned beforehand. First, we decide the colour like we often do from a shade picker, and we ensure that to get the HSL worth for it.

Observe: I’m utilizing Sketch app, however you employ no matter design device you need.

Take into account the next determine:

Discover that the HSL values in there. The primary one is the angle, which represents the angle of the colour we now have. On this case, it’s sky blue. As soon as we now have the angle, we are able to begin tweaking the saturation and brightness as per our wants.

Saturation

Saturation controls how saturated the colour needs to be. 0% is totally unsaturated, whereas 100% is absolutely saturated.

Lightness

As for lightness, it controls how mild or darkish the colour is. 0% is is black, and 100% is white.

Take into account the next determine:

With that, we now have three values which might be representing shade, angle, saturation, and brightness. Right here is how we are able to use the colour in CSS:

.aspect {
background-color: hsl(196, 73%, 62%);
}

By modifying the colour angle, we are able to get colours which might be related in saturation and lightness to the bottom one. That is very helpful when engaged on new model colours as it might create a constant set of secondary model colours.

Take into account the next determine:

Do you’re feeling that the three colours are associated to one another when it comes to how the colour is saturated, and the way it’s darkish or mild it’s? That has been achieved by solely altering the colour angle. That is what nice about HSL colours. It’s extra human-friendly to learn and edit than every other shade sort.

Use Circumstances For HSL Colours

Altering Colours On Hover

When a shade in a particular part wants to seem darker on hover, HSL colours might be good for this. It may be useful for parts like buttons and playing cards.

:root {
–primary-h: 221;
–primary-s: 72%;
–primary-l: 62%;
}

.button {
background-color: hsl(var(–primary-h), var(–primary-s), var(–primary-l));
}

.button:hover {
–primary-l: 54%;
}

Discover how I mixed CSS variables with HSL colours. On hover, I solely want to change the lightness worth. Bear in mind, the upper the worth, the lighter. For a darker shade, we have to scale back the worth.

A Mixture Of Tinted Colours

HSL might be helpful when we now have a design that makes use of the identical shade however with completely different shades. Take into account the next design:

The primary header navigation has the first shade, whereas the secondary navigation has a lighter shade. With HSL, we are able to get the lighter shade simply by altering the lightness worth.

This may be extraordinarily helpful whereas having a UI with a number of themes. I created two themes and switching from one to a different solely requires me to edit the hue diploma.

First theme:

Second theme:

Shade Palettes

By altering the lightness, we are able to create a set of shades for a shade that can be utilized all through the UI the place attainable.

That is helpful for design methods the place designers present builders with the shades for every shade of the model.

Right here is an interactive demo that reveals that. The enter slider solely adjustments the hue worth, and the remainder of the shades change primarily based on that.

Discover how the white on the best is an excessive amount of. We will change this with a customized white that’s derived from a really mild shade of the colour we now have. In my view, it’s a lot better.

Variations Of A Button

One other helpful use case for HSL colours is when we now have major and secondary choices which might be from the identical shade however with completely different shades. On this instance, the secondary button has a really mild tint of the principle shade. HSL colours are good for that.

:root {
–primary-h: 221;
–primary-s: 72%;
–primary-l: 62%;
}

.button {
background-color: hsl(var(–primary-h), var(–primary-s), var(–primary-l));
}

.button–secondary {
–primary-l: 90%;
shade: #222;
}

.button–ghost {
–primary-l: 90%;
background-color: clear;
border: 3px stable hsl(var(–primary-h), var(–primary-s), var(–primary-l));
}

Tweaking the first button variations the place quick and might be prolonged extra for broader utilization. Altering the hue worth will change all of the buttons’ themes.

Dynamic Washed-Out Results

In some circumstances, we’d want a gradient to have a really mild shade of the opposite shade cease. With HSL, we are able to use the identical shade however with a special lightness worth for the second.

.part {
background: linear-gradient(to left, hsl(var(–primary-h), var(–primary-s), var(–primary-l)), hsl(var(–primary-h), var(–primary-s), 95%));
}

.section-2 {
–primary-h: 167;
}

The gradient begins from the best with a stable shade after which fades out to the lighter shade. This can be utilized for an ornamental hero part, for instance.

That’s all with the use circumstances. I hope that you just realized one thing new and helpful.

Conclusion

HSL colours are very highly effective once we use them the best method. They will save us effort and time and even assist us to discover choices for tips on how to apply shade to design.

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment