Skip to content

Commit 56b713c

Browse files
committed
Add Define HSL Colors With Alpha Values as a CSS til
1 parent 0c41e46 commit 56b713c

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1212

13-
_1091 TILs and counting..._
13+
_1092 TILs and counting..._
1414

1515
---
1616

@@ -125,6 +125,7 @@ _1091 TILs and counting..._
125125
- [Conditional Styling For Unsupported CSS Features](css/conditional-styling-for-unsupported-css-features.md)
126126
- [Create A Pulsing Background With CSS Animation](css/create-a-pulsing-background-with-css-animation.md)
127127
- [Define CSS Custom Properties With CSS Variables](css/define-css-custom-properties-with-scss-variables.md)
128+
- [Define HSL Colors With Alpha Values](css/define-hsl-colors-with-alpha-values.md)
128129
- [Display Responsive iframe Maintaining Aspect Ratio](css/display-responsive-iframe-maintaining-aspect-ratio.md)
129130
- [Dry Up SCSS With Mixins](css/dry-up-scss-with-mixins.md)
130131
- [Give Elements The Same Width With Flexbox](css/give-elements-the-same-width-with-flexbox.md)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Define HSL Colors With Alpha Values
2+
3+
HSL is a more intuitive option for defining colors in CSS.
4+
5+
It is an acronym which stands for Hue, Saturation, and Lightness which are the
6+
three components of an HSL value. It is also possible to include a fourth
7+
value: Alpha for the amount of transparency of the color.
8+
9+
The modern syntax allows you to use `hsl` with or without an alpha value, and
10+
no need to use comma separators. If the alpha value is included, it must be
11+
separated from the other three values with a forward slash.
12+
13+
```css
14+
.modern-hsl {
15+
background-color: hsl(340deg 100% 50%);
16+
}
17+
.modern-hsla {
18+
background-color: hsl(340deg 100% 50% / 0.75);
19+
}
20+
```
21+
22+
If you need IE-support, then you'll have to use the older syntax. This version
23+
of `hsl` requires comma separators, and to apply an alpha value you have to
24+
switch to `hsla`.
25+
26+
```css
27+
.old-hsl {
28+
background-color: hsl(340deg, 100%, 50%);
29+
}
30+
.old-hsla {
31+
background-color: hsla(340deg, 100%, 50%, 0.75);
32+
}
33+
```
34+
35+
You can see it in action and play around with the value in this [live
36+
example](https://codepen.io/jbranchaud/pen/gOLVzjx?editors=1100).
37+
38+
39+
See [Can I Use? on HSL](https://caniuse.com/?search=hsl) for more details on
40+
browser support.
41+
42+
h/t to [Josh Comeau](https://twitter.com/JoshWComeau) and his excellent [CSS
43+
for JS Developers course](https://css-for-js.dev/)

0 commit comments

Comments
 (0)