CSS logical properties
What are CSS Logical Properties?
CSS Logical Properties replace physical direction-based properties (left, right, top, bottom) with flow-relative alternatives that adapt to different writing modes and text directions (LTR, RTL, vertical text).
Why Logical Properties?
In left-to-right (LTR) languages like English, margin-left adds space at the start. But in right-to-left (RTL) languages like Arabic, the start is on the right side. Logical properties handle this automatically.
Physical vs Logical Mapping
| Physical | Logical (Horizontal writing) | Concept |
|---|---|---|
margin-left | margin-inline-start | Start of text direction |
margin-right | margin-inline-end | End of text direction |
margin-top | margin-block-start | Start of block flow |
margin-bottom | margin-block-end | End of block flow |
padding-left | padding-inline-start | β |
padding-right | padding-inline-end | β |
width | inline-size | Size in inline direction |
height | block-size | Size in block direction |
border-left | border-inline-start | β |
border-right | border-inline-end | β |
left | inset-inline-start | β |
right | inset-inline-end | β |
top | inset-block-start | β |
bottom | inset-block-end | β |
Shorthand Properties
.element {
/* Block (top + bottom) and Inline (left + right) */
margin-block: 1rem 2rem; /* block-start block-end */
margin-inline: 1rem 2rem; /* inline-start inline-end */
padding-block: 1rem; /* Same for both */
padding-inline: 2rem; /* Same for both */
/* Size */
inline-size: 300px; /* width in LTR */
block-size: 200px; /* height in LTR */
/* Inset (positioning) */
inset-block: 0; /* top: 0; bottom: 0; */
inset-inline: 0; /* left: 0; right: 0; */
inset: 0; /* All four directions */
}Practical Example: RTL Support
/* β Physical β breaks in RTL */
.icon {
margin-right: 8px;
}
/* β
Logical β works in LTR and RTL */
.icon {
margin-inline-end: 8px;
}/* β Physical β needs duplicate rule for RTL */
.sidebar {
border-left: 2px solid #ccc;
}
[dir="rtl"] .sidebar {
border-left: none;
border-right: 2px solid #ccc;
}
/* β
Logical β one rule for all directions */
.sidebar {
border-inline-start: 2px solid #ccc;
}Logical Values for text-align and float
.text {
text-align: start; /* Instead of 'left' */
text-align: end; /* Instead of 'right' */
}
.image {
float: inline-start; /* Instead of 'left' */
}border-radius Logical Properties
.card {
border-start-start-radius: 8px; /* top-left in LTR */
border-start-end-radius: 8px; /* top-right in LTR */
border-end-start-radius: 8px; /* bottom-left in LTR */
border-end-end-radius: 8px; /* bottom-right in LTR */
}Important:
CSS Logical Properties are the future of CSS layout and essential for internationalization. They're supported in all modern browsers. Start using them in new projects, especially if your application needs to support RTL languages or vertical writing modes.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.