Sub topic 1
Progressive enhancement: start with a working baseline
Prerequisites
This chapter assumes you know the following:
- Basic CSS syntax: selectors, properties and values.
- How the cascade and source order decide between conflicting declarations.
- What an at-rule such as
@medialooks like.
You don't need JavaScript for this chapter.
What progressive enhancement means
Progressive enhancement is a way of building pages in layers. You write a baseline that works in every browser you support. You then add improvements for browsers that can handle them. A browser that lacks the newer feature still gets a usable page, just a plainer one.
The baseline is not a "broken" version. Content is readable, controls work, and the layout makes sense. Enhancements change how nice the page looks or how convenient it is. They do not decide whether the page works.
Progressive enhancement versus graceful degradation
Graceful degradation starts from the full, modern design. You then try to patch it so it doesn't fall apart in older browsers. Progressive enhancement starts from the other end.
| Approach | Starting point | Direction of work | Typical risk |
|---|---|---|---|
| Graceful degradation | The ideal modern design | Patch downward for weaker browsers | Fixes are added late and some browsers are missed |
| Progressive enhancement | A baseline that works everywhere | Add layers upward for stronger browsers | Enhancements can be skipped, leaving a plain page |
The practical difference is where failure ends up. With enhancement, the worst case is a plain but working page. With degradation, the worst case is a broken page.
The layering model
Each layer should be safe to remove. If the enhancement layer is ignored, the baseline is still complete.
A worked example
Suppose you want a responsive gallery. Flexbox is supported almost everywhere, so it makes a good baseline. CSS Grid gives a tidier result, so it becomes the enhancement.
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.gallery > * {
flex: 1 1 12rem;
}
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}Read it in two parts:
- The first two rules are the baseline. Items wrap onto new lines and each one prefers a width of
12rem. - The
@supportsblock is the enhancement. Only a browser that understandsdisplay: gridapplies it. That browser switches the container to a grid and uses the column definition.
You will learn the syntax of @supports in a later chapter. For now, notice the order of work: the baseline comes first and is complete without the block.
One detail to watch: the enhancement overrides display: flex with display: grid. The flex property on the children is then ignored, which is harmless because grid items don't use it.
Rules for writing a good baseline
- Write the baseline first. Test it with the enhancement block removed.
- Keep content and function in the baseline. Text, links, forms and navigation must not depend on the enhancement.
- Make enhancements additive. Prefer adding or overriding a few properties over rewriting the whole rule.
- Avoid enhancements that hide things. If an enhancement is the only way to see a control, then it isn't an enhancement.
- Keep readability in the baseline. Text must have enough contrast without any effect that depends on newer features.
Common mistakes
- Designing for the newest browser first. You then discover the baseline only at the end, and it is usually broken.
- Treating the baseline as an afterthought. If you never view it, you will not notice when it fails.
- Putting essential layout only in the enhancement. When the enhancement is skipped, content overlaps or becomes unreadable.
- Assuming "old browser" is the only case to cover. A baseline also helps when a feature is blocked, partially implemented, or fails to load.
What comes next
The next chapters explain how a browser decides which features it supports. They also show how to write the enhancement layer with @supports and with plain cascade fallbacks. Keep the layering model in mind: baseline first, enhancement second.
Exercise
A stylesheet sets .menu { display: grid; grid-template-columns: 1fr 1fr; } with no other rules for .menu. In a browser without grid support the menu items stack awkwardly and overlap other content. Describe how you would restructure this using progressive enhancement, and name the baseline.
Show answer
Choose a layout that works everywhere as the baseline, for example normal block flow or flexbox with wrapping, and make it complete so items never overlap. Then put the display: grid and grid-template-columns declarations in an @supports (display: grid) block. A browser without grid support keeps the baseline and still has a usable menu. A browser with grid support applies the enhancement on top of it.