Couleurs CSS3
Valeurs de couleur CSS
Section titled “Valeurs de couleur CSS”Définition des couleurs en CSS
Section titled “Définition des couleurs en CSS”CSS offre plusieurs façons de spécifier les couleurs, y compris les noms de couleurs prédéfinis, les codes hexadécimaux, et les valeurs RGB, RGBA, HSL et HSLA.
Les ajouts modernes comme RGBA et HSLA permettent de spécifier l’opacité directement dans la valeur de couleur.
- Noms de couleurs :
red,blue,transparent(plus de 140 noms pris en charge) - Hexadécimal :
#RRGGBBou#RGB(ex. :#ff0000,#f00) - RGB :
rgb(rouge, vert, bleu)(valeurs de 0 à 255) - RGBA :
rgba(rouge, vert, bleu, alpha)(alpha de 0.0 à 1.0) - HSL :
hsl(teinte, saturation, luminosité)(teinte 0-360, sat/lum 0%-100%) - HSLA :
hsla(teinte, saturation, luminosité, alpha)(alpha de 0.0 à 1.0)
Prise en charge par les navigateurs
Section titled “Prise en charge par les navigateurs”Tous les navigateurs modernes prennent entièrement en charge ces formats de valeurs de couleur CSS, y compris RGBA, HSL et HSLA.
| Valeur de couleur/Propriété | Internet Explorer | Chrome | Firefox | Safari | Opera |
|---|---|---|---|---|---|
| RGBA, HSL, HSLA | IE 9+ | Chrome | Firefox | Safari | Opera |
| opacity Property | IE 9+ | Chrome | Firefox | Safari | Opera |
Couleurs RGBA
Section titled “Couleurs RGBA”RGBA étend RGB en ajoutant un canal alpha pour la transparence. rgba(rouge, vert, bleu, alpha). La valeur alpha varie de 0.0 (entièrement transparent) à 1.0 (entièrement opaque).
C’est utile pour créer des arrière-plans ou des couleurs de texte semi-transparents sans affecter l’opacité de l’élément entier.
Exemple : Arrière-plans rouges semi-transparents
[Div with background: rgba(255, 0, 0, 0.2);][Div with background: rgba(255, 0, 0, 0.5);][Div with background: rgba(255, 0, 0, 0.8);]Exemple CSS :
.element1 { background-color: rgba(255, 0, 0, 0.3); /* Rouge avec 30% d'opacité */}.element2 { background-color: rgba(0, 255, 0, 0.3); /* Vert avec 30% d'opacité */}.element3 { background-color: rgba(0, 0, 255, 0.3); /* Bleu avec 30% d'opacité */}Couleurs HSL
Section titled “Couleurs HSL”HSL (Teinte, Saturation, Luminosité) offre une manière plus intuitive de définir les couleurs basée sur la roue chromatique.
- Teinte (Hue) : Un angle sur la roue chromatique (0-360 degrés). 0 ou 360 est rouge, 120 est vert, 240 est bleu.
- Saturation : L’intensité de la couleur (0% est en niveaux de gris, 100% est en pleine couleur).
- Luminosité (Lightness) : La luminosité (0% est noir, 50% est la couleur ‘normale’, 100% est blanc).
Syntaxe : hsl(teinte, saturation%, luminosité%)
Exemple : Variations de vert
[Div with background: hsl(120, 100%, 50%); /* Vert normal */][Div with background: hsl(120, 100%, 75%); /* Vert clair */][Div with background: hsl(120, 100%, 25%); /* Vert foncé */][Div with background: hsl(120, 50%, 70%); /* Vert pastel */]Exemple CSS :
.element1 { background-color: hsl(120, 100%, 50%); /* Vert */}.element2 { background-color: hsl(120, 100%, 75%); /* Vert clair */}.element3 { background-color: hsl(120, 100%, 25%); /* Vert foncé */}.element4 { background-color: hsl(120, 60%, 70%); /* Vert pastel */}Couleurs HSLA
Section titled “Couleurs HSLA”HSLA ajoute un canal alpha à HSL pour la transparence. hsla(teinte, saturation%, luminosité%, alpha). La valeur alpha fonctionne de la même manière que dans RGBA (de 0.0 à 1.0).
Exemple : Variations de vert semi-transparentes
[Div with background: hsla(120, 100%, 50%, 0.3);][Div with background: hsla(120, 100%, 75%, 0.3);][Div with background: hsla(120, 100%, 25%, 0.3);][Div with background: hsla(120, 60%, 70%, 0.3);]Exemple CSS :
.element1 { background-color: hsla(120, 100%, 50%, 0.3); /* Vert avec opacité */}.element2 { background-color: hsla(120, 100%, 75%, 0.3); /* Vert clair avec opacité */}.element3 { background-color: hsla(120, 100%, 25%, 0.3); /* Vert foncé avec opacité */}.element4 { background-color: hsla(120, 60%, 70%, 0.3); /* Vert pastel avec opacité */}La propriété opacity
Section titled “La propriété opacity”La propriété opacity définit le niveau de transparence pour l’élément entier, y compris son contenu, son arrière-plan et sa bordure.
La valeur varie de 0.0 (entièrement transparent) à 1.0 (entièrement opaque).
Exemple : Application de l’opacité à des éléments avec des arrière-plans unis
[Div style="background-color: red; opacity: 0.2;"] Texte également transparent [/Div][Div style="background-color: red; opacity: 0.5;"] Texte également transparent [/Div][Div style="background-color: red; opacity: 0.8;"] Texte également transparent [/Div]Important : Contrairement à RGBA/HSLA qui n’affectent qu’une propriété de couleur spécifique, opacity affecte l’élément et tous ses enfants.
Exemple CSS :
.element1 { background-color: rgb(255, 0, 0); /* Rouge uni */ opacity: 0.6; /* L'élément entier est opaque à 60% */}.element2 { background-color: rgb(0, 255, 0); /* Vert uni */ opacity: 0.6;}.element3 { background-color: rgb(0, 0, 255); /* Bleu uni */ opacity: 0.6;}