Sub topic 16
Support, fallbacks and migrating a stylesheet
This chapter assumes you know the box model, the lang and dir attributes, and the basic logical properties from earlier chapters: inline-size, margin-inline, padding-block, inset-inline-end and the start and end keywords. It covers three practical questions. Which browsers can you rely on? How do you write a fallback when you can't? How do you move a real stylesheet over without breaking it?
Browser support in practice
Logical properties arrived in browsers over several years, so support depends on which property you use.
- Core logical properties (
inline-size,block-size,margin-inline-*,padding-block-*,border-inline-*) work in all current evergreen browsers. - The
inset-*family, the two-value shorthands such asmargin-inline, and logical border-radius corners such asborder-start-start-radiusarrived later than the longhands. - The
float: inline-startandclear: inline-endkeyword values, andresize: blockorresize: inline, arrived later still. - Container query units such as
cqiandcqbare newer than viewport units such asviandvb.
Before you make a decision, check a current compatibility table such as MDN's browser compatibility data or caniuse for each property you plan to use. Support changes, and this chapter can't tell you today's numbers. Compare the table against your project's browser support policy, which is the list of browsers and versions your team promises to support.
How browsers treat unknown declarations
Fallbacks work because of how CSS handles errors. A browser that doesn't recognise a property name or value drops that one declaration and continues with the rest of the rule. It doesn't report an error or discard the rule.
That behaviour gives you the simplest fallback pattern: write the physical declaration first, then the logical one.
.box {
margin-left: 1rem;
margin-inline-start: 1rem;
}An old browser ignores the second line and uses the first. A modern browser reads both. Because the two declarations have equal specificity, the later one wins. In a left-to-right page both lines give the same result. In a right-to-left page the logical line overrides the physical one and moves the margin to the right.
This pattern has a catch. The physical line still applies in browsers that support both, but the logical line replaces it only where the logical property maps to the same side. For margin-left and margin-inline-start, the sides differ in right-to-left text. The logical declaration sets the right margin, while the physical margin-left: 1rem stays on the left. You then get a margin on both sides. That is why the next section resets the physical value.
Using @supports for clean fallbacks
The @supports at-rule applies its block only when the browser understands the stated declaration. You can use it to reset the physical value, so the two never overlap.
.box {
margin-left: 1rem;
}
@supports (margin-inline-start: 1rem) {
.box {
margin-left: 0;
margin-inline-start: 1rem;
}
}An old browser applies only the first rule. A modern browser applies both. It sets margin-left back to 0 and uses margin-inline-start for the real spacing. In a right-to-left page the margin ends up on the right side only.
Test one representative property per feature group. The support for margin-inline-start says little about inset-inline-end or float: inline-start, so test each group you use:
@supports (inset-inline-start: 0) {
/* inset-based rules */
}
@supports (float: inline-start) {
/* float keyword rules */
}Deciding whether you need fallbacks at all
Fallbacks add code, and code adds maintenance. Use three questions to decide.
- Does your browser support policy include browsers that lack the property? If not, skip the fallback.
- Does a missing declaration cause a visible break, or only a small spacing difference? A lost decorative margin may not matter. A lost
inset-inline-endon a close button might. - Will the page ever render in right-to-left or vertical text? If the site is left-to-right only, a physical fallback is already correct, and a failure falls back to a sensible layout.
If all three answers say a fallback is cheap and useful, add it. Otherwise write the logical property alone.
Migrating an existing stylesheet
A full rewrite is risky. Migrate in small steps that you can review and revert.
Step 1: Take an inventory
Search the stylesheet for physical properties and values. A text search covers most of them.
grep -nE "(margin|padding|border)-(left|right|top|bottom)|(^|[^-])(left|right|top|bottom):|text-align: *(left|right)|float: *(left|right)|clear: *(left|right)" styles.cssGroup the results by component. Each hit falls into one of three categories:
- Logical in intent, such as a sidebar gap on the leading side. Convert it.
- Physical by nature, such as a shadow that matches a light source on the top left. Keep it (see the earlier chapter on when to keep physical values).
- Unclear. Mark it and ask the designer.
Step 2: Convert by mapping
For a left-to-right, horizontal-tb page, the mapping is mechanical.
| Physical | Logical |
|---|---|
| `width` / `height` | `inline-size` / `block-size` |
| `margin-left` / `margin-right` | `margin-inline-start` / `margin-inline-end` |
| `margin-top` / `margin-bottom` | `margin-block-start` / `margin-block-end` |
| `padding-left` / `padding-right` | `padding-inline-start` / `padding-inline-end` |
| `border-left` | `border-inline-start` |
| `left` / `right` | `inset-inline-start` / `inset-inline-end` |
| `top` / `bottom` | `inset-block-start` / `inset-block-end` |
| `text-align: left` / `right` | `text-align: start` / `end` |
| `float: left` / `right` | `float: inline-start` / `inline-end` |
Then collapse matching pairs into two-value shorthands. This rule
.card {
padding-top: 1rem;
padding-bottom: 1rem;
padding-left: 1.5rem;
padding-right: 1rem;
}becomes
.card {
padding-block: 1rem;
padding-inline: 1.5rem 1rem;
}The first value of padding-inline is the start side and the second is the end side. In a left-to-right page the result matches the original exactly.
Step 3: Convert one component at a time
Pick a self-contained component, convert it, and compare it with the original in a left-to-right page. The result should look identical. A visual difference means a wrong mapping. Commit each component separately so you can revert one without losing the others.
Step 4: Verify in other directions
After each component, test it with dir="rtl" and with a vertical writing-mode, as described in the previous chapter. In the right-to-left check, the leading side should flip to the right. In the vertical check, nothing should overflow or collapse, because inline-size and block-size follow the rotated axes.
<html dir="rtl" lang="ar">Step 5: Guard against regression
Add a lint rule so physical properties don't creep back. Stylelint has a plugin, stylelint-use-logical, that reports physical properties and can rewrite them. Allow exceptions with a comment where a physical value is intended.
.shadow {
/* stylelint-disable-next-line liberty/use-logical-spec */
box-shadow: 4px 4px 0 gray;
}Plugin and rule names change between versions, so confirm them against the plugin's current documentation before you copy the configuration.
Common errors during migration
- Mixing physical and logical on one side.
margin-left: 1remandmargin-inline-end: 1remin the same rule can both target the left side in right-to-left text. Convert a rule fully. - Assuming
widthequalsinline-size. They match only in horizontal writing modes. Invertical-rl,inline-sizesets the height. Only convert sizing that is meant to follow the text flow. - Forgetting transforms and shadows.
translateX(10px)andbox-shadowoffsets are physical and don't flip. Decide for each whether the direction is part of the design or part of the text flow. - Forgetting background position.
background-position: leftis physical. Logical keywords exist for some contexts, but check support before you rely on them. - Icons that point in a direction. An arrow that means "next" must flip in right-to-left text. Logical properties don't change images, so handle it with
:dir(rtl)or a separate class. - Relying on the cascade order of a fallback. If a later rule sets
margin-lefton the same element, it can undo your logical value. Search the whole cascade for overrides.
A complete example
This version of a notification banner has a fallback for older browsers. It uses the physical property first, then resets it where logical properties are supported.
.banner {
width: 100%;
max-width: 40ch;
margin-left: auto;
margin-right: auto;
padding: 1rem 1rem 1rem 1.5rem;
border-left: 4px solid steelblue;
}
@supports (margin-inline: auto) and (border-inline-start: 1px solid red) {
.banner {
width: auto;
max-width: none;
margin-left: 0;
margin-right: 0;
padding: 0;
border-left: 0;
inline-size: min(100%, 40ch);
margin-inline: auto;
padding-block: 1rem;
padding-inline: 1.5rem 1rem;
border-inline-start: 4px solid steelblue;
}
}The reset lines undo every physical declaration, so no side is styled twice. If you can drop old browsers, delete the first rule and the resets, and keep only the logical lines.
Exercise
A rule sets margin-left: 2rem; followed by margin-inline-start: 2rem;. A page using it switches to dir="rtl" in a browser that supports both properties. What margins appear, and how do you fix the rule?
Show answer
The element gets a 2rem margin on the left from margin-left and a 2rem margin on the right from margin-inline-start, so both sides have margin. Fix it by wrapping the logical declaration in @supports (margin-inline-start: 2rem) and setting margin-left: 0 there, or by dropping the physical line if old browsers aren't supported.
Exercise
List the first three actions you would take to migrate a large left-to-right stylesheet, and name one check to run after each converted component.
Show answer
First, search the stylesheet for physical properties and values and group them by component. Second, classify each hit as logical in intent, physical by nature, or unclear. Third, convert one component with the mapping table and collapse pairs into two-value shorthands. After each component, compare it with the original in a left-to-right page, then test it with dir="rtl" and a vertical writing-mode.