CSS Colors, Backgrounds and Typography: Color Notations, Font Stacks, Units and Inherited Text Styles
Hex, rgb() and hsl() notations, the background shorthand's reset behavior, font-family fallback stacks, em vs rem, and the difference between text styles that inherit and box styles that don't — taught plainly, then tested with six original practice questions written from the CSS specification and MDN.
How color, backgrounds, and typography actually work in CSS
In CSS you control appearance through a small set of properties — color, background, font-family, font-size, and line-height — fed by a handful of value notations (named colors, hex, rgb(), hsl()) and units that are either absolute like px or relative like em, rem, and %. What ties them together is inheritance: text-related properties flow down to descendant elements automatically, while box-related properties such as background-color, border, and padding do not.
Four ways to write a color
A named keyword (navy, rebeccapurple) is the simplest. Hex notation is case-insensitive and comes in four lengths. #ff0099 is red-green-blue; the three-digit #f09 is shorthand where each digit is duplicated (f becomes ff); and the four- and eight-digit forms add an alpha channel, so #f09a expands to #ff0099aa. In the eight-digit form the alpha byte runs from 00 (fully transparent) to ff (fully opaque). The functional forms rgb() and hsl() accept modern space-separated values with alpha after a slash: rgb(255 0 153 / 80%) or hsl(150 30% 60% / 0.8). The older comma forms rgba() and hsla() remain valid — in current CSS they are simply aliases of rgb() and hsl(). Finally, two keywords earn their keep: transparent, and currentcolor, which resolves to the element's own computed color value.
Backgrounds paint the box, not the text
The background shorthand sets eight longhands: background-image, background-position, background-size, background-repeat, background-origin, background-clip, background-attachment, and background-color. Anything you omit is reset to its initial value — background-image: none, background-repeat: repeat, background-color: transparent, and so on. That reset is the single most common source of "my image disappeared." Note also that background-size may only appear immediately after background-position, separated by a slash: background: url("hero.jpg") center/cover no-repeat;
Font stacks and units
font-family takes a prioritized, comma-separated list, and the browser works left to right until it finds a family it can use. Each entry is either a quoted string or a sequence of unquoted identifiers, so "Times New Roman" and Times New Roman are both technically legal — but quoting is the recommended form, and it becomes mandatory once a token starts with a digit or contains punctuation ("Goudy Bookletter 1911" must be quoted; unquoted it is invalid). Generic families are the opposite case: serif, sans-serif, monospace, cursive, fantasy, and system-ui are keywords and must never be quoted. Always end the list with one of them.
For sizing, em means "relative to the parent's computed font size" when used on font-size itself, but "relative to this element's own computed font size" on every other property, including padding, margin, and width. rem is always relative to the root element, which makes it predictable no matter how deeply an element is nested.
The cascade of inherited text styles
color, font-family, font-size, font-weight, line-height, letter-spacing, text-align, and visibility are inherited by default. background-color, border, padding, margin, and display are not. You can override either direction with the global keywords inherit, initial, unset, and revert. Set your typography once on html or body and let it flow down — that is the whole point of an inherited cascade. For the full topic map, see the WGU D276 Web Development Foundations study guide.
Practice: Colors Backgrounds Typography
-
A designer gives you the value #0a2540cc for a hero panel's background. What does the final cc control?
- The alpha channel, making the color roughly 80% opaque
- The blue channel, shifting the color toward a lighter blue
- Nothing — an eight-digit hex value is invalid, so the declaration is dropped
- A brightness multiplier applied after the red, green, and blue channels
Show answer & explanation
Correct answer: The alpha channel, making the color roughly 80% opaque. In an eight-digit hex color the first six digits are red, green, and blue, and the last two are the alpha channel, running from 00 (fully transparent) to ff (fully opaque). Hexadecimal cc is 204, and 204 divided by 255 is about 0.8, so the color is roughly 80% opaque. The tempting wrong answer is "the blue channel" — but blue is already covered by the fifth and sixth digits (40 here); the seventh and eighth digits exist only because CSS added alpha support to hex notation.
-
A rule declares background-image: url("hero.jpg"); and background-repeat: no-repeat; and then, further down in the same rule, background: #0a2540;. What renders?
- The image still shows, unrepeated, sitting on top of the navy color
- Only the navy color — the image is gone and background-repeat is back to repeat
- The image shows and still does not repeat, because longhand declarations beat shorthands
- Nothing changes, because a background shorthand containing only a color is invalid
Show answer & explanation
Correct answer: Only the navy color — the image is gone and background-repeat is back to repeat. A shorthand sets every one of its longhands, and any component you leave out is reset to its initial value — so background: #0a2540 silently sets background-image back to none and background-repeat back to repeat. The plausible trap is thinking longhands outrank shorthands; they do not, since both have equal specificity. Source order is what decides, so put the shorthand first and your longhand overrides after it.
-
Given body { font-size: 16px; line-height: 1.5em; } and h2 { font-size: 32px; }, what line-height does the h2 compute to?
- 48px
- 24px
- 32px
- 16px
Show answer & explanation
Correct answer: 24px. When line-height is given a length or a percentage, it computes to an absolute length on the element where it is declared — 1.5 × 16px = 24px on body — and that frozen 24px is what descendants inherit, regardless of their own font size. The 48px answer is what you would get from the unitless value line-height: 1.5, because a unitless number is inherited as the number itself and each element multiplies it by its own font-size. That is exactly why the documentation recommends unitless line-height values.
-
Given html { font-size: 16px; } and .card { font-size: 1.5em; padding: 0.5em; }, where .card is a direct child of body and body sets no font-size of its own, how much padding does .card get?
- 8px — 0.5 multiplied by the 16px font size .card inherits from its parent
- 12px — 0.5 multiplied by .card's own computed 24px font size
- 24px — em on padding always matches the element's font size exactly
- 16px — em on padding resolves against the root font size, same as rem
Show answer & explanation
Correct answer: 12px — 0.5 multiplied by .card's own computed 24px font size. The em unit resolves against the parent's font size only on the font-size property itself, so .card's font-size is 1.5 × 16px = 24px. On every other property, including padding, em resolves against the element's own computed font size, giving 0.5 × 24px = 12px. Option A is the classic half-right guess: it correctly identifies the parent's 16px but applies the font-size rule to a property that does not use it. Reach for rem instead when you want spacing pinned to the root regardless of nesting.
-
You want a font stack that falls back to the browser's generic sans-serif face if neither named font is available. Which declaration actually achieves that?
- font-family: "Helvetica Neue" Arial sans-serif;
- font-family: "Helvetica Neue", Arial, sans-serif;
- font-family: "Helvetica Neue", Arial, "sans-serif";
- font-family: "Helvetica Neue", Arial;
Show answer & explanation
Correct answer: font-family: "Helvetica Neue", Arial, sans-serif;. Generic family names are keywords, not strings, so quoting sans-serif turns it into a request for a font literally named "sans-serif" and destroys the safety net — that is the near-miss most people pick. The first option omits the commas, mixing a quoted string with bare identifiers inside one name, which is invalid syntax and gets the whole declaration discarded. The last option is perfectly valid CSS but simply has no generic at the end, so if both named fonts are missing you get whatever the browser's default font happens to be rather than a guaranteed sans-serif.
-
A rule sets .panel { color: #b30047; border: 2px solid; } and never mentions border-color anywhere. What color is the border?
- Black, because that is the initial color in the user agent stylesheet
- Transparent, so the border occupies space but is invisible
- #b30047, the element's own text color
- There is no border at all, because a border shorthand without a color is invalid
Show answer & explanation
Correct answer: #b30047, the element's own text color. The initial value of border-color is currentcolor, which resolves to the element's computed color — so the border picks up #b30047 automatically. The black answer is tempting because unstyled text usually is black, but that is only because color happened to compute to black, not because borders default to black independently. Note the careful distinction: border-color is not an inherited property; it simply tracks the color value, and color is what inherits.
A worked example
Here is a small, realistic block. Read it and predict what each element looks like before you read the explanation underneath.
html { font-size: 16px; }
body {
color: #1c2b3a;
font-family: "Segoe UI", Helvetica, Arial, sans-serif;
line-height: 1.6;
background: #f7f9fb url("paper.png") repeat;
}
.card {
font-size: 1.25rem;
padding: 1em;
background-color: rgb(255 255 255 / 90%);
border: 1px solid;
}
.card h3 { font-size: 2em; }
The .card font size is 20px — 1.25 × the root's 16px, because rem ignores the parent entirely. Its padding is 20px, because em on padding uses the element's own computed 20px. Its border is #1c2b3a: the inherited color resolving through currentcolor, which is the initial value of border-color. The h3 is 40px — 2 × the parent's 20px, because em on font-size does use the parent — and its line height is 64px, or 1.6 × 40px, since the unitless number was inherited as a number rather than as a frozen length.
Mistakes people actually make
- Expecting links to inherit
color. Settingcoloronbodywill not recolor an<a href="...">, because the browser's own stylesheet sets a color on linked anchors, and any declaration beats inheritance. Styleaexplicitly. An<a>with nohrefis not matched by those rules and does inherit normally. - Expecting form controls to inherit fonts.
<input>,<select>, and<button>are the same story — the user agent stylesheet sets a font on them. Addfont: inherit;to pull them into your type system. - Compounding
em. Nest three lists that each setfont-size: 0.9emand the third level lands at 0.729 of the original. Useremwhen you want a fixed step instead of a compounding one. - Assuming backgrounds inherit. They do not. A child only looks like it shares its parent's background because the child's own
background-coloristransparent, letting the parent show through. - Slash placement in the shorthand.
background: url("a.jpg") cover;is invalid — a size must follow a position and a slash, as incenter/cover. - Over- or under-quoting font names. Multi-word family names may legally be written unquoted as a sequence of identifiers, but quote them anyway: it is required the moment a token starts with a digit or contains punctuation, and it never hurts. Generic keywords are the exception that must stay bare.
How to reason about the tricky cases
When a computed value surprises you, ask two questions in order. First: is this property inherited? If it is not, the child is using its own initial value, not the parent's. Second: what is the relative unit measured against? Percentages and em resolve against different references depending on the property, and once a relative value has computed to a length, that length is what descendants receive. Verify against the "Formal definition" table on any MDN property page, which states plainly whether the property inherits and what its initial and computed values are. Then put it into practice with the D276 practice questions.
Want someone to walk you through this?
Book 1-on-1 prep for WGU D276 — original coaching, never exam content.
Stuck on this topic? WhatsApp us on +1 646 980 4914.
Working through the whole course? Read the full WGU D276 study guide, or see every WGU D276 practice topic.