current theme: placeholder

CSS Logical Properties Cheat Sheet

Published on

Syntax

Logical properties replace physical properties like left/right/top/bottom with properties that are relative to the writing system the layout uses, e.g. right-to-left for Arabic, top-to-bottom in Japanese, etc.

Instead of thinking “layout,” think of the writing direction. So, taking English as an example:

  • The block dimension is top-to-bottom.
  • The inline dimension is left-to-right.

Both dimensions have a start and an end.

Here are a few examples (for English):

Logical propertiesPhysical properties
block-sizeheight
inline-sizewidth
inset-block-starttop
inset-inline-endright
margin-block-endmargin-bottom
padding-inline-startpadding-left

A more accurate definition (on MDN) of inline and block is:

  • The block dimension is perpendicular to the flow of text within a line. For standard English text, it is the vertical dimension.
  • The inline dimension is parallel to the flow of text within a line. For standard English text, it is the horizontal dimension.

This new syntax also gives us some convenient new shorthands, for example:

Logical propertiesPhysical properties
inset-blocktop + bottom
inset-inlineleft + right
margin-blockmargin top + bottom
padding-inlinepadding left + right

The full list

Here’s the full list on MDN, and below is a table of the properties you’ll likely use most often.

Note: inset is a shorthand for physical properties even though it sounds like it should be part of the logical properties. (That’s probably why it’s mistakenly listed on the MDN page.)

Physical propertiesLogical properties
widthinline-size
heightblock-size
max-widthmax-inline-size
max-heightmax-block-size
min-widthmin-inline-size
min-heightmin-block-size
topinset-block-start
bottominset-block-end
leftinset-inline-start
rightinset-inline-end
top + bottom (New)inset-block
left + right (New)inset-inline
Shorthand for physical properties!inset
margin-topmargin-block-start
margin-bottommargin-block-end
margin-leftmargin-inline-start
margin-rightmargin-inline-end
margin top + bottom (New)margin-block
margin left + right (New)margin-inline
padding-toppadding-block-start
padding-bottompadding-block-end
padding-leftpadding-inline-start
padding-rightpadding-inline-end
padding top + bottom (New)padding-block
padding left + right (New)padding-inline
border-topborder-block-start
border-bottomborder-block-end
border-leftborder-inline-start
border-rightborder-inline-end
border top + bottom (New)border-block
border left + right (New)border-inline

Use case

In a nutshell, I agree with Chris Coyier, you should use logical properties almost all the time, with a few exceptions:

Where it doesn’t work

  • Media queries
  • Transform function
  • Background position

Where it doesn’t make sense

  • Image dimensions
  • Purely aesthetic design elements, outside the content context.

Resources

RTL Styling 101

This guide on how to style for RTL in CSS is a bit outdated (it doesn’t use logical properties) but it’s written by someone who speaks Arabic and it is full of useful nuggets!

Horizontal-in-Vertical Composition

The text-combine-upright property is useful in languages like Japanese, when you need to combine vertical text with things like roman numerals that need to stay horizontal.

Reply by email