Reading history

CSS Logical Properties

6 min read 2 views

Learn CSS logical properties: inline and block directions, margin-inline, padding-block, inline-size and how layouts flip for right-to-left text.

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-size and block-size instead of width and height
  • How dir="rtl" and writing-mode change 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-left and padding-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-start and padding-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-end is 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-size is the size along the line, the width in horizontal text. block-size is the height. In the vertical box, the same inline-size: 160px became its height.
  • writing-mode: vertical-rl makes text run top to bottom, with lines stacking right to left, as in traditional Japanese.
  • padding-block and padding-inline are handy shorthands: one sets top and bottom, the other left and right.
  • margin-inline: auto centres a block horizontally, a shorter way of writing margin-left: auto; margin-right: auto.
  • Corners have logical names too: border-start-start-radius is the corner where block-start and inline-start meet (top-left in English).
Physical (English)Logical
width / heightinline-size / block-size
max-widthmax-inline-size
margin-top / margin-bottommargin-block-start / margin-block-end
padding-left / padding-rightpadding-inline-start / padding-inline-end
left: 0; right: 0inset-inline: 0
text-align: lefttext-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

  1. Mixing margin-left and margin-inline-start on the same element. In English they set the same side, and whichever comes later wins, which is confusing.
  2. Thinking inline-size is always the width. In vertical writing modes it is the height.
  3. Using float: left in a site that may be translated. Use float: inline-start, or better, Flexbox or Grid, which follow the text direction automatically.
  4. Forgetting dir and only setting text-align: right for 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-inline and inset-inline replace their physical versions.
  • Keep physical properties for effects tied to the screen, not the text.

Next: CSS aspect-ratio.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *