Understand CSS value types and use units and functions to create flexible sizes.
A CSS declaration pairs a property, the characteristic to change, with a value, the setting to apply. In color: blue, color is the property and blue is its value. Each property accepts particular kinds of values, so a color cannot be used wherever a length is expected.
h1 {
color: blue;
font-size: 24px;
}The first declaration sets the heading's text color; the second sets its font size. Knowing what a value represents helps you choose a valid value and predict how the result will change.
CSS values can take several forms:
| Value type | Meaning | Example |
|---|---|---|
| Keywords | Predefined words. | display: block or color: blue |
| Numbers | Whole numbers or decimals. | z-index: 10 or line-height: 1.2 |
| Lengths | A number combined with a unit. | 24px or 1.5em |
| Percentages | A proportion of another value. | width: 50% is half the containing block’s width in an ordinary block layout. |
| Colors | Named colors or values written in a color format. | #FF5733 and rgb(255, 87, 51) describe the same color. |
| Strings | Text enclosed in quotes. | font-family: 'Times New Roman', serif |
| Functions | A name and parentheses that produce or supply a value. | calc(100% - 20px) |
The property determines which forms make sense. For example, display can use the keyword block, while font-size can use a length like 24px.
An integer is a whole number, such as 10 in z-index: 10. The z-index property uses it to control stacking order. A number can also be fractional: line-height: 1.2 uses a decimal without a unit. A length such as 1.5em combines a number with a unit, while a percentage such as 50% expresses a proportion of another value.
An identifier can name a selector or provide a keyword value. For example, content identifies the element selected by #content, and block is the value of display:
#content {
display: block;
font-family: 'Times New Roman', serif;
background-image: url("assets/banner.jpg");
}The quoted font name and image path are strings. The url() function uses the path to identify the background image.
Named colors such as red and blue are easy to read, but their available names are limited. Hexadecimal and RGB values offer other ways to specify colors:
.example {
color: #FF5733;
border: 2px solid rgb(255, 87, 51);
}Both declarations use the same color. The text and its solid border are the same orange. In the six-digit hex value, the pairs FF, 57, and 33 represent red, green, and blue. Each pair ranges from 00 to FF. In rgb(), the three numbers represent the same components on a scale from 0 to 255: FF corresponds to 255, 57 to 87, and 33 to 51.
Two other color functions let you work from a hue:
.button {
background-color: hsl(120, 100%, 50%);
}
.background {
background-color: hwb(240 0% 0%);
}In hsl(), the values are hue, saturation, and lightness. Hue is an angle: 120 represents green. Saturation controls color intensity, from gray at 0% to full color at 100%; lightness ranges from black at 0% to white at 100%. The example gives a bright green.
In hwb(), spaces separate the hue, whiteness, and blackness. The example starts with a blue hue and adds 0% white and 0% black. Increasing whiteness or blackness changes how much white or black is mixed into the hue.
A unit tells CSS how to interpret a length. An absolute unit such as px gives a fixed CSS length. Other absolute units include inches (in) and points (pt), which are useful for print. A CSS pixel does not necessarily correspond directly to a physical screen pixel.
Relative units instead depend on another size. The reference matters when you work out the result:
em: For font-size, it follows the inherited font size. For padding in the example below, it follows the element's own font size.rem: Follows the root element's font size.vw and vh: Follow the viewport's width and height. The viewport is the area in which the browser displays the page. 1vw is 1% of its width, and 1vh is 1% of the large viewport height, measured with retractable browser controls out of the way.button {
font-size: 1.5rem;
padding: 1em 2em;
width: 50vw;
height: 10vh;
}If the root font size is 16px, the button's 1.5rem font size is 24px. Its 1em vertical padding is then 24px, and its 2em horizontal padding is 48px. The width and height follow the viewport, so they change as its dimensions change.
A fixed width such as 960px stays at that CSS size. It gives precise control, but it may be too wide for a smaller screen and cause horizontal scrolling. A fluid width such as 80% changes with its parent container: if the parent is 500px wide, the element's width is 400px; if the parent becomes narrower, the element becomes narrower too.
Fluid and fixed values can work together. A width can follow the available space while also having a limit, so it does not grow indefinitely.
The position property also accepts keywords. With relative, the browser first lays out the box in normal flow. An offset moves its visible box while its siblings keep their places.
.box {
position: relative;
top: 50px;
left: 20px;
}The numeric offsets move the box 50px down and 20px right from its normal position. The effect of an offset depends on the positioning context established by the position value.
CSS functions can combine values or choose between them:
.container {
width: calc(100% - 20px);
}
.card {
width: min(90%, 600px);
padding: clamp(10px, 2vw, 20px);
}calc() performs arithmetic. Here, the container's width is its parent container's width minus 20px, so it changes as the parent changes. Put spaces around the minus sign in this expression.min() chooses the smaller value. The card's width is 90% of its available container width when that is less than 600px; otherwise, it is 600px. For a 500px container, 90% is 450px, so min() selects 450px. For a 1000px container, 90% is 900px, so it selects 600px.max() does the reverse: width: max(300px, 50%) selects whichever is larger. The width is at least 300px, but can grow when 50% of the container is larger.clamp() takes a minimum, a preferred value, and a maximum. In the card example, the preferred padding is 2vw, but it stays between 10px and 20px. As the viewport width changes, the padding can change within those limits.To predict a size, identify its reference first. A card can change width when its container changes while its viewport-based padding stays the same.
Try it yourself
HTML + CSS
top and left.calc() combines values, min(), max(), and clamp() constrain sizes.