# CSS Logical Properties by [kirupa](https://www.kirupa.com/me/index.htm) | filed under [HTML and CSS](https://www.kirupa.com/html5/learn_html_css.htm) **TL;DR:** CSS can describe the sides of a box by where text starts and ends, using logical properties. margin-inline-start is the left side in English, the right side in Arabic, and the top in vertical Japanese, so one set of styles lays out correctly for every writing direction without any extra rules. - The inline direction runs along a line of text, and the block direction runs from one line to the next. - The dir attribute and writing-mode decide where inline-start and block-start land, so a card's border and icon follow the text. - Shorthands like margin-inline: auto and padding-block: 24px 48px set both ends of a direction at once. - Shadows, transforms, and the plain inset shorthand stay physical, so styles tied to the screen still use left, right, top, and bottom. When we write `margin-left: 20px` to put some space before a line of text, we're quietly assuming something. We're assuming text starts on the left. For English, that's true. For Arabic and Hebrew, text starts on the right. For Japanese written vertically, text starts at the top and new lines get added to the left. Our `margin-left` doesn't know any of that, so on those pages, the space ends up on the wrong side. In this article, we'll learn a way to write our CSS in terms of where text starts and ends instead of left and right, so the same styles work for every language and writing direction, and we'll see how a component flips itself around without any extra code. Onwards! ## What Are Logical Properties? Most of the CSS properties we learn first are tied to the sides of the screen: `left`, `right`, `top`, and `bottom`. These are called **physical properties**, because they always mean the same side of the box, no matter what's inside it. What if, instead of saying "put this space on the left," we could say "put this space where the line of text starts"? On an English page, that would be the left. On an Arabic page, it would be the right. We'd write the rule once, and the browser would figure out which side we meant based on the text. CSS has a full set of properties that work exactly this way, and they're called **logical properties** (the specification also calls them flow-relative properties). Here's our margin, written both ways: ```css /* Physical: always the left side */ .note { margin-left: 20px; } /* Logical: wherever lines of text start */ .note { margin-inline-start: 20px; } ``` To make sense of names like `margin-inline-start`, we need two new directions. ## What Are the Inline and Block Directions? Every piece of text on a page flows in two directions. Letters and words follow each other along a line, and that's the **inline direction**. When a line fills up, the next line goes after it, and paragraphs stack up the same way. That's the **block direction**. Here's how those two directions look in three different writing systems: ![Three panels of text. In English, lines run left to right, so the inline direction points right, and new lines stack downward, so the block direction points down. In Arabic, lines run right to left, so the inline direction points left while the block direction still points down. In vertical Japanese, lines run top to bottom, so the inline direction points down, and new lines are added to the left, so the block direction points left.](https://www.kirupa.com/html5/images/lp_axes.png) In English, the inline direction points right and the block direction points down. Arabic keeps the block direction pointing down, but its lines run from right to left, so the inline direction points left. Japanese can be written horizontally like English, but when it's written vertically, everything turns sideways. Each line runs from top to bottom, so the inline direction points down, and new lines get added to the left, so the block direction points left. Two things in our HTML and CSS decide these directions. The `dir` attribute (or the `direction` property) says whether lines run left to right (`ltr`) or right to left (`rtl`). The `writing-mode` property says whether lines are horizontal (`horizontal-tb`, the default) or vertical (`vertical-rl` and `vertical-lr`): ```html ``` ```css .vertical-text { writing-mode: vertical-rl; } ``` ## Where Do start and end Land? Each direction has a **start** and an **end**. That gives every box four logical sides: `inline-start`, `inline-end`, `block-start`, and `block-end`. Here's where they land on a box of left-to-right text and a box of right-to-left text: ![Two boxes of text. In the left-to-right box, inline-start is on the left and inline-end is on the right. In the right-to-left box, they swap: inline-start is on the right and inline-end is on the left. In both, block-start is on top and block-end is on the bottom.](https://www.kirupa.com/html5/images/lp_box.png) In the left-to-right box, `inline-start` is on the left, because that's where each line begins. In the right-to-left box, `inline-start` moves to the right, because that's where each line begins there. The block sides stay put in both, with `block-start` on top and `block-end` on the bottom. In vertical text, all four would rotate along with the text. Almost every physical property has a logical twin built from these names. Here are the ones we reach for the most, with the physical property each one replaces in a left-to-right, horizontal page: | Physical | Logical | | --- | --- | | `margin-left` | `margin-inline-start` | | `margin-right` | `margin-inline-end` | | `margin-top` | `margin-block-start` | | `margin-bottom` | `margin-block-end` | | `padding-left` | `padding-inline-start` | | `border-left` | `border-inline-start` | | `width` | `inline-size` | | `height` | `block-size` | | `max-width` | `max-inline-size` | | `top` | `inset-block-start` | | `left` | `inset-inline-start` | | `text-align: left` | `text-align: start` | | `float: left` | `float: inline-start` | | `border-top-left-radius` | `border-start-start-radius` | The positioning properties are the ones that look the least like their physical twins. The `top`, `right`, `bottom`, and `left` properties are collectively known as the **inset** properties, which is where names like `inset-inline-start` come from. The corner radius names are a little different too. They list the block side first and the inline side second, so `border-start-start-radius` is the corner where the block start and the inline start meet. ## Which Shorthands Save the Most Typing? Logical properties come with shorthands that set both ends of a direction at once, and these are where they really start to pay off. Each one takes a value for the start and a value for the end, or a single value for both: ```css .page { max-inline-size: 1100px; margin-inline: auto; padding-inline: 16px; padding-block: 24px 48px; } .overlay { position: absolute; inset-inline: 0; inset-block-end: 0; } ``` The `margin-inline: auto` line is the logical version of the classic `margin: 0 auto` centering trick, except it leaves the top and bottom margins alone. The `padding-inline: 16px` line puts 16px on both inline sides, and `padding-block: 24px 48px` puts 24px at the block start and 48px at the block end. In the overlay, `inset-inline: 0` stretches it from one inline side to the other, and `inset-block-end: 0` pins it to the bottom. There's one shorthand that catches people by surprise. The plain `inset` property, like `inset: 0`, is a shorthand for the physical `top`, `right`, `bottom`, and `left` properties. With `inset: 0`, that doesn't matter, since every side gets the same value. When we give it different values for different sides, they're physical sides, the same as the four-value `margin` and `padding` shorthands. ## How Does a Real Component Flip? Let's look at a quote card with a colored border along one edge and a little speech bubble icon at the start of the text. Here's how it looks on a right-to-left page, once built with physical properties and once with logical ones: ![Two copies of a quote card in right-to-left text. With physical properties, the accent border and the icon's space stay on the left while the text runs from the right, so the icon crowds the text and the border is on the wrong side. With logical properties, the border, the icon, and the space all move to the right, where the text starts.](https://www.kirupa.com/html5/images/lp_card.png) Look at the card on the left. The text correctly starts on the right, because the browser knows the page is right to left. Our CSS doesn't know, though. The border is still on the left, and so is the space we saved for the icon, which leaves the icon sitting at the end of the text instead of the start. The card on the right puts the border, the icon, and the space on the right, next to where the text begins. Here's the physical CSS behind the card on the left: ```css .quote { position: relative; padding: 16px 16px 16px 66px; border-left: 6px solid #e0605a; text-align: left; } .quote .icon { position: absolute; left: 16px; top: 16px; } ``` And here's the logical CSS behind the card on the right: ```css .quote { position: relative; padding-block: 16px; padding-inline: 66px 16px; border-inline-start: 6px solid #e0605a; text-align: start; } .quote .icon { position: absolute; inset-inline-start: 16px; inset-block-start: 16px; } ``` It's the same amount of code, and nothing in it mentions a language or a direction. The `dir` attribute on the page does all of the deciding, so the card works in English, in Arabic, and even in vertical Japanese without a single extra rule. ## Let's Play With Logical Properties Here's that quote card. You can switch its text between English, Arabic, and vertical Japanese, and switch its CSS between the physical and logical versions above: [Live demo: A quote card with a red border and a speech bubble icon, with buttons to switch its text between English, Arabic, and vertical Japanese and its CSS between physical and logical properties, plus the matching CSS](https://www.kirupa.com/html5/examples/logical_properties_lab.htm) **Try it:** 1. Press **Arabic** 2. Press **Physical**, then **Logical** 3. Press **Japanese** and try both again In vertical Japanese, notice that the logical border moves to the top of the card, because that's where each vertical line of text starts. The physical version leaves it on the left, where it has nothing to do with the text at all. ## When Should We Still Use Physical Properties? Logical properties are the right choice whenever a style is about the text: where it starts, how much room it has, and which side gets the decoration. Some styles are about the screen instead, and for those, physical properties are still correct. A drop shadow that pretends light comes from the top of the screen should stay at the top, no matter what language the page is in. The same goes for things like a map, a game board, or a photo, where left and right mean left and right. A few properties simply don't have logical versions. The offsets in `box-shadow` and in `transform: translateX()` are always physical, so a slide-in animation that moves from the left might need a `[dir="rtl"]` rule to slide in from the right instead. ## How Do We Start Using Logical Properties? The good news is that we're already partway there. [Flexbox](https://www.kirupa.com/html5/flexbox_fundamentals.htm) and [CSS Grid](https://www.kirupa.com/html5/css_grid_fundamentals.htm) are logical out of the box. A flex row runs in the inline direction, and values like `flex-start` and `start` follow the text, so they flip on right-to-left pages for free. The same goes for the inline sizes that [Container Queries](https://www.kirupa.com/html5/container_queries.htm) measure. For everything else, a good habit is to reach for the logical version first in any new CSS, and to switch older rules over as we touch them. Margins, padding, borders, sizes, positions, and `text-align` cover most of it. On the HTML side, make sure every page says what direction its text runs in. A right-to-left page should set `dir="rtl"` on its `html` element, and `dir="auto"` on things like comments and usernames lets the browser figure out the direction of text that people type in. Browser support isn't something to worry about. The properties we use every day, like `margin-inline`, `padding-block`, and `inset-inline`, have worked in every major browser since Safari 14.1 in April 2021. ## Frequently Asked Questions **What Are CSS Logical Properties?** They're CSS properties that describe the sides of a box by where text starts and ends instead of left, right, top, and bottom. For example, margin-inline-start is the left margin in English but the right margin in Arabic, so one set of styles works for every writing direction. **What Is the Difference Between Inline and Block?** The inline direction is the way text runs along a line, and the block direction is the way new lines and paragraphs stack. In English, inline goes left to right and block goes top to bottom. In vertical Japanese, inline goes top to bottom and block goes right to left. **What Does margin-inline: auto Do?** It sets both inline margins to auto, which centers a block element that has a width or a max-width. It's the logical version of the classic margin: 0 auto centering trick, except it leaves the top and bottom margins alone. **Is the inset Property Logical?** No. The plain inset shorthand sets the physical top, right, bottom, and left properties, so different values for different sides land on physical sides. For logical positioning, use inset-inline, inset-block, or longhands like inset-inline-start. **Should I Use margin-inline-start Instead of margin-left?** For anything tied to the text, like spacing, borders, and alignment, yes. The everyday logical properties have worked in every major browser since 2021, so there's no downside. Keep physical properties for things tied to the screen, like the direction of a shadow. ## Conclusion Logical properties describe the sides of a box in terms of the text inside it. Once we think in terms of the inline direction (along a line) and the block direction (from one line to the next), names like `margin-inline-start` and `inset-block-end` stop looking strange and start looking precise. Our styles then follow the text wherever it goes, whether that's left to right, right to left, or top to bottom. Even if every page you build today is in English, logical properties are worth the habit. They cost nothing extra to write, the shorthands are often shorter than what they replace, and the day someone translates your site, the layout will already be ready for it. Before you go, here are a few things you can do: 1. Test your knowledge by taking the quiz below. 2. Check out some related content: - [Flexbox Fundamentals](https://www.kirupa.com/html5/flexbox_fundamentals.htm). Line items up in a row that flips along with the text direction. - [Container Queries](https://www.kirupa.com/html5/container_queries.htm). Query a container's inline size so components fit their space. - [Centering a Div Horizontally](https://www.kirupa.com/html5/centering_a_div_horizontally.htm). Center a div horizontally, whether it's positioned normally or absolutely. 3. Stuck on something, or curious about a detail we skipped? [Ask on the forums](https://forum.kirupa.com/), and we'll help you out. ## Test your knowledge! [Take the Reviewing Logical Properties Quiz](https://www.kirupa.com/quizzes/css_logical_properties.htm). Practice with 4 multiple-choice questions and an explanation after every answer.