Sub topic 16
Support, fallbacks and migrating a stylesheet
This chapter assumes you know the box model, the lang and dir attributes, and the logical properties from earlier chapters: margin-inline, padding-block, inline-size, inset-inline-start and the logical keyword values. Here you decide how to ship them and how to convert an existing stylesheet.
Browser support
The core logical properties are supported in all current evergreen browsers. That covers sizing, margin, padding, border and inset-*. Support for the start and end values of text-align is also broad. Older browsers differ in two ways. Some lack the inset-* shorthands. Some lack the logical values for float, clear and resize.
Check your project's browser list before you decide whether fallbacks are needed. If every browser you support has the properties, skip fallbacks and use logical properties directly.
Writing a fallback
CSS ignores declarations it does not understand. So you can put a physical declaration first and a logical one after it. A browser that knows the logical property applies it. An older browser keeps the physical value.
When you need an explicit check, use @supports:
.box {
margin-left: 1rem;
}
@supports (margin-inline-start: 1rem) {
.box {
margin-left: 0;
margin-inline-start: 1rem;
}
}Note the margin-left: 0 reset inside the block. Without it, the physical margin and the logical margin would both apply, and in right-to-left text you would get margin on both sides.
Migrating a stylesheet
Migrate in small steps. Each step should leave the page looking the same in left-to-right English.
- Audit. Search for
left,right,width,height,margin-top,padding-leftand similar names. - Convert the easy cases first. Mechanical swaps are
margin-lefttomargin-inline-start,padding-righttopadding-inline-end, andwidthtoinline-size. - Collapse pairs into two-value shorthands.
margin-left: auto; margin-right: autobecomesmargin-inline: auto. - Handle values. Change
text-align: lefttostart, andfloat: lefttoinline-start. - Review what remains. Keep physical values where they are correct, such as a shadow that must match a fixed light source.
- Test with
dir="rtl"and a verticalwriting-mode.
| Physical | Logical |
|---|---|
| `width` | `inline-size` |
| `height` | `block-size` |
| `margin-left` | `margin-inline-start` |
| `padding-right` | `padding-inline-end` |
| `border-top` | `border-block-start` |
| `top: 0` | `inset-block-start: 0` |
| `text-align: left` | `text-align: start` |
Common mistakes
- Converting
widthtoinline-sizeeverywhere without thinking about vertical text. In a vertical mode the sizes swap, so check each case. - Converting properties that must stay physical, such as a decoration tied to the screen edge.
- Leaving a physical rule in place beside its logical replacement, so both apply.
- Relying on
diron the element only. Setdironhtmlor the content root so inheritance works.
Exercise
A stylesheet has .panel { margin-left: 2rem; text-align: left; float: left; }. Rewrite it with logical properties, and explain why a fallback may be needed.
Show answer
.panel { margin-inline-start: 2rem; text-align: start; float: inline-start; }. A browser that does not support float: inline-start or margin-inline-start ignores those declarations. If you must support such browsers, keep the physical declarations first, then reset them to 0 or the default inside @supports (margin-inline-start: 1rem) and add the logical ones.
Exercise
Why does the @supports example set margin-left: 0 before margin-inline-start: 1rem?
Show answer
Both properties would otherwise apply. In a right-to-left layout the inline start is the right side, so the box would get a left margin from the fallback and a right margin from the logical property. The reset removes the physical margin.