Definition
- Logical properties and values, from the W3C's CSS Logical Properties module, name sides and sizes by the flow of text, not the screen: the inline axis runs along a line, the block axis across lines, and each has a start and an end.
- On a horizontal page,
inline-startis where lines start: left in English, right in Arabic. Somargin-inline-startis the left margin of an English page and the right margin of an Arabic one. - Physical properties have logical counterparts, such as
padding-inline-endforpadding-rightin English,inset-inline-startforleftandinline-sizeforwidth, and there are logical values:text-align: start,float: inline-startandclear: inline-end.
Why it matters
- A theme written with
margin-leftandfloat: leftkeeps those sides when the page turns right to left, so spacing, alignment and floated elements stay where the English design put them. - A common case:
ul { padding-left: 0 }does not remove a list's indent on an Arabic page, because the browser puts the indent at the start of the line, on the right. - With logical properties, one stylesheet serves both directions, instead of a second one for Arabic that goes out of date.
Example
One rule instead of two:
css
/* Before */
.card {
margin-left: 1rem;
text-align: left;
}
[dir='rtl'] .card {
margin-left: 0;
margin-right: 1rem;
text-align: right;
}
/* After */
.card {
margin-inline-start: 1rem;
text-align: start;
}Common mistakes
- Converting margins and paddings only:
text-align,float, the positionsleftandright, and the corners ofborder-radiushave logical forms too. - Four-value shorthands are physical:
padding: 0 1rem 0 0sets the top, right, bottom and left. For start and end, usepadding-inline: 0 1rem. - In Tailwind CSS, writing
ml-4,pl-4andtext-leftinstead of the logical classesms-4,ps-4andtext-start.