Sub topic 14
When to keep physical values
This chapter assumes you know logical margin, padding, inset and text-align: start, and that you know dir and writing-mode. Logical properties are the default for layout. A few things are tied to the screen or to the physical world, and for those a physical value is the correct choice.
The test: does it follow the text or the screen?
Ask what the value is attached to.
- If it describes where content flows or which side is "before" or "after" the text, use a logical property.
- If it describes a fixed direction on the display, use a physical one.
Cases where physical is correct
Light and shadow come from the real world. A shadow cast down and to the right looks right in every language, so flipping it in Arabic would be a visual bug.
.panel {
box-shadow: 2px 4px 8px rgb(0 0 0 / 0.3);
}box-shadow offsets, transform: translateX(), gradient directions such as to right, and background-position have no widely supported logical forms. They are physical by design.
Media queries also measure the viewport physically. width and height in @media describe the screen, not the writing mode.
@media (min-width: 48rem) {
.layout {
display: grid;
grid-template-columns: 1fr 2fr;
}
}Some interface elements are anchored to a screen edge, not to a reading direction. A "back to top" button that must stay in the bottom-right corner can use right and bottom. If you want it to follow the reading side, use inset-inline-end instead. Decide from the design requirement, not from habit.
.to-top {
position: fixed;
right: 1rem;
bottom: 1rem;
}Other physical cases:
- Maps, charts with a time axis, and music notation. Many of these always run left to right.
- Scroll axes.
overflow-xandoverflow-ystay physical in most code bases. - Images that contain text or a logo. These should not mirror.
Directional icons need an explicit flip
Logical properties move boxes. They do not mirror images. An arrow icon that points "forward" must be flipped in right-to-left text, and you do that with a transform.
.arrow {
transform: none;
}
[dir="rtl"] .arrow {
transform: scaleX(-1);
}Only flip icons that mean "next" or "back". A clock, a checkmark or a brand logo should not be mirrored.
Mixing both in one rule
Physical and logical properties can sit in the same rule. They work together as long as you avoid setting both for the same side, because the later declaration in the cascade wins.
.tooltip {
padding-inline: 0.75rem;
box-shadow: 0 2px 6px rgb(0 0 0 / 0.25);
transform: translateY(-4px);
}Here the padding follows the text direction, while the shadow and the lift stay fixed.
Common errors
- Mirroring a shadow or gradient by using a logical value that does not exist, which makes the declaration invalid and silently ignored.
- Setting
margin-leftandmargin-inline-starton the same element and getting an order-dependent result. - Flipping every icon in RTL, including ones that have no direction.
Exercise
A page uses box-shadow: -4px 2px 6px gray on a card, margin-left: 2rem on the card, and float: left on its thumbnail. Which of these should become logical, and which should stay physical?
Show answer
The shadow stays physical, because light comes from a fixed direction and should not flip with text direction. The margin-left becomes margin-inline-start: 2rem, and the float: left becomes float: inline-start, because both describe the start side of the text and should flip in right-to-left layouts.