Working with colours Flashcards
What is a color
CSS data type?
The <color>
CSS data type represents a color.
A <color>
may also include an alpha-channel transparency value, indicating how the color should composite with its background.
Source MDN
How can you specify a <color>
CSS data type?
A <color>
can be defined in any of the following ways:
- Using a keyword (such as
blue
ortransparent
). - Using
#-hexadecimal
orrgb()
,hsl()
,hwb()
functional notations to specify a color in the sRGB color space.
Source MDN
What does the currentColor
keyword represent?
The currentcolor
keyword represents the value of an element’s CSS color
property. This lets you use the color
value on properties that do not receive it by default.
Example:
<div style="color: blue; border: 1px dashed currentcolor;"> The color of this text is blue. <div style="background: currentcolor; height:9px;"></div> This block is surrounded by a blue border. </div>
Source MDN
What is the RGB color model?
The RGB color model defines a given color in the sRGB color space according to its red, green, and blue components. An optional alpha component represents the color’s transparency.
Source MDN
How can you specify an RGB color model?
RGB colors can be expressed through both hexadecimal
(prefixed with #
) and functional (rgb()
, rgba()
) notations.
Source MDN
What is the contrast ratio
?
The contrast ratio
explains the difference between the lightest color brightness and the darkest color brightness in a given range.
Source css-trics
Why is the contrast ratio
important?
A good contrast between text or graphics against the background color makes the content of your site readable.
It is particularly beneficial to users with certain types of color blindness and other similar conditions, who experience low contrast, and have trouble differentiating between similar colors.
It is good to have a cool design on your website, but the design is worthless if your users can’t read your content.
Source MDN
How can you check the contrast ratio of 2 colors?
Using online tool such as contrast-ratio.com.
What does CSS color-scheme
property do?
The CSS color-scheme
property lets the browser use (or choose) to display certain elements with its dark or light default styling.
The color-scheme
property is defined in the CSS Color Adjustment Module Level 1 specification, where it is called the “Opting Into a Preferred Color Scheme” property.
Source css-tricks
Syntax of CSS color-scheme
property
Syntax
color-scheme: normal; color-scheme: light; color-scheme: dark; color-scheme: light dark; color-scheme: only light;
The color-scheme
property’s value must be one of the following keywords:
- normal - Indicates that the element isn’t aware of any color schemes, and so should be rendered using the browser’s default color scheme.
- light - Indicates that the element can be rendered using the operating system light color scheme.
- dark - Indicates that the element can be rendered using the operating system dark color scheme.
- only - Forbids the user agent from overriding the color scheme for the element.
Can be used to turn off color overrides caused by Chrome’s Auto Dark Theme, by applying color-scheme: only light;
on a specific element or :root
.
Source MDN
How can we apply a dark theme or a light theme depending on the user’s system or browser preferences?
With the prefers-color-scheme
CSS media feature.
The prefers-color-scheme
CSS media feature is used to detect if a user has requested light
or dark
color themes. A user indicates their preference through an operating system setting (e.g. light
or dark
mode) or a user agent setting.
It takes two values:
-
light
: When a user has selected that they prefer a light theme or has no active preferences -
dark
: When a user has selected a dark display in their settings
Example:
body { --bg-color: white; --text-color: black; background-color: var(--bg-color); color: var(--text-color); } @media screen and (prefers-color-scheme: dark) { body { --bg-color: black; --text-color: white; } }
“A Complete Guide to CSS Media Queries | CSS-Tricks” (CSS-Tricks). Retrieved February 8, 2024.
What are the differences between color-scheme
and prefers-color-scheme
?
color-scheme
is all about default appearances. It tells the browser to update the colors in its stylesheet.
Meanwhile, prefers-color-scheme
is all about applying the styles we write in our own stylesheet, and only when that condition is met. In other words, any style rules we write inside the media query are applied — it has nothing to do with the browser’s default styles.
“color-scheme | CSS-Tricks” (CSS-Tricks). Retrieved February 8, 2024.