Most CSS you have seen talks about left, right, top and bottom. That works until your site is shown in a right-to-left language such as Arabic or Urdu, or in vertical text. Then “left” is suddenly the wrong side. Logical properties describe sides by their role in the text instead: the start and end of a line, and the start and end of the block. One stylesheet then works in every direction, and many logical properties are also shorter to write.
In this lesson you will learn
- The two directions: inline and block
- Logical margin, padding and border properties
inline-sizeandblock-sizeinstead of width and height- How
dir="rtl"andwriting-modechange things - When physical properties are still the right choice
You should know the box model and Flexbox.
Inline and Block Directions
Every piece of text has two directions:
- The inline direction is the way a line of text runs. In English it goes left to right; in Arabic right to left.
- The block direction is the way new lines and paragraphs stack. In English it goes top to bottom.
Each direction has a start and an end. In English, inline-start is the left side and block-start is the top. In Arabic, inline-start is the right side.
Physical vs Logical
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Logical properties</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 14px; }
.note {
/* Physical: always left, whatever the language */
border-left: 4px solid #e53935;
padding-left: 12px;
margin-bottom: 10px;
background: #ffebee;
}
.note-logical {
/* Logical: at the START of the line, so it flips for right-to-left text */
border-inline-start: 4px solid #43a047;
padding-inline-start: 12px;
margin-block-end: 10px;
background: #e8f5e9;
}
</style>
</head>
<body>
<div class="note">Physical left border (English)</div>
<div class="note" dir="rtl">حد يسار ثابت (Arabic, right-to-left)</div>
<div class="note-logical">Logical start border (English)</div>
<div class="note-logical" dir="rtl">حد في بداية السطر (Arabic, right-to-left)</div>
</body>
</html>
Result
- The red notes use
border-leftandpadding-left. In the Arabic line, the border stays on the left, at the end of the text, which looks wrong. - The green notes use
border-inline-startandpadding-inline-start. The border moves to the right side for Arabic, where the text begins. dir="rtl"on an element (or on<html>for a whole page) sets right-to-left direction.margin-block-endis the space after the block: the bottom in English.
Sizes, Shorthands and Writing Modes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Logical sizes and writing modes</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 14px; display: flex; gap: 16px; align-items: flex-start; }
.box {
inline-size: 160px; /* width in horizontal text */
padding-block: 8px; /* top and bottom */
padding-inline: 12px; /* left and right */
border: 2px solid #1e88e5;
border-start-start-radius: 14px; /* top-left corner in English */
background: #e3f2fd;
}
.vertical { writing-mode: vertical-rl; } /* text runs top to bottom */
.centered { margin-inline: auto; max-inline-size: 200px; text-align: center; }
</style>
</head>
<body>
<div class="box">Horizontal: inline-size is the width.</div>
<div class="box vertical">Vertical: inline-size is the height.</div>
<div style="flex:1"><div class="box centered">margin-inline: auto centers me</div></div>
</body>
</html>
Result
inline-sizeis the size along the line, the width in horizontal text.block-sizeis the height. In the vertical box, the sameinline-size: 160pxbecame its height.writing-mode: vertical-rlmakes text run top to bottom, with lines stacking right to left, as in traditional Japanese.padding-blockandpadding-inlineare handy shorthands: one sets top and bottom, the other left and right.margin-inline: autocentres a block horizontally, a shorter way of writingmargin-left: auto; margin-right: auto.- Corners have logical names too:
border-start-start-radiusis the corner where block-start and inline-start meet (top-left in English).
| Physical (English) | Logical |
|---|---|
width / height | inline-size / block-size |
max-width | max-inline-size |
margin-top / margin-bottom | margin-block-start / margin-block-end |
padding-left / padding-right | padding-inline-start / padding-inline-end |
left: 0; right: 0 | inset-inline: 0 |
text-align: left | text-align: start |
When to Use Which
Use logical properties for anything tied to the text: spacing around paragraphs, indents, borders that mark the start of a quote, and the width of reading columns. Keep physical properties for things tied to the screen, such as a shadow that should always fall to the bottom-right, or an element pinned to the top of the window.
Common Mistakes
- Mixing
margin-leftandmargin-inline-starton the same element. In English they set the same side, and whichever comes later wins, which is confusing. - Thinking
inline-sizeis always the width. In vertical writing modes it is the height. - Using
float: leftin a site that may be translated. Usefloat: inline-start, or better, Flexbox or Grid, which follow the text direction automatically. - Forgetting
dirand only settingtext-align: rightfor right-to-left text. Alignment alone does not flip punctuation and layout.
Practice Exercises
Exercise 1. Rewrite margin-top: 20px; margin-bottom: 20px; padding-left: 10px; padding-right: 10px; using two logical shorthands.
Show answer
margin-block: 20px;
padding-inline: 10px;
Exercise 2. A quote has border-inline-start: 3px solid gray. On which side is the border in English, and on which side in Urdu?
Show answer
Left in English, right in Urdu, because Urdu is written right to left and the line starts on the right.
Frequently Asked Questions
Are logical properties supported in browsers?
Yes. All modern browsers support the common logical properties, such as margin-inline, padding-block, inline-size and inset. They are safe to use in new projects.
My site is only in English and Bangla. Do I still need them?
Both languages run left to right, so nothing breaks without them. Many developers still use logical properties because the shorthands are convenient and the site is ready if a right-to-left language is ever added.
Summary
- Inline is the direction of a line of text; block is the direction lines stack.
- Logical properties use start and end, so they flip correctly for right-to-left and vertical text.
inline-size,margin-block,padding-inlineandinset-inlinereplace their physical versions.- Keep physical properties for effects tied to the screen, not the text.
Next: CSS aspect-ratio.
