Advertisement
  1. Web Design
  2. UX/UI
  3. Web Typography

How to Use CSS Logical Properties to Control Layout

Scroll to top

CSS logical properties define a new way of working with layout. Their primary goal is to help developers support different writing systems such as right-to-left (RTL) and vertically displayed scripts. These new features make it possible to control layout through logical rather than physical properties. For example, with logical properties you might describe the start and end of an element, rather than its left and right boundaries. See the difference?

Even if you don’t work with non-LTR languages and multi-directional websites, it’s worth getting familiar with the new specifications. Logical properties will help establish a new philosophy of creating more flexible layouts with CSS.

Browser Support

As browser support still has much space for improvement, logical properties are not yet production-ready. However, newer versions of Firefox, Chrome, and Safari already support them, so you can certainly get started with testing. 

Older versions of Firefox, Chrome, and Safari also provide partial support with the -moz- and -webkit- prefixes. Currently, Internet Explorer and Edge don’t support the feature at all.

Logical properties browser supportLogical properties browser supportLogical properties browser support

Content Directions in CSS

English is written with a left-to-right (LTR) and top-to-bottom (tb) horizontal script (the Latin script). However, not every language is like that. There are many examples of languages which use a right-to-left (RTL) script, like Arabic and Hebrew, and others which can be vertically displayed, like Japanese, Chinese, and Mongolian.

“Languages don't have a direction. Scripts have a writing direction, and so languages written in a particular script, will be written with the direction of that script.” – W3C

In CSS you can set content direction on a page with the direction and writing-mode properties.

1. Writing-mode

The writing-mode property defines how block content (such as paragraphs) flows on the screen. It can take three values:

  1. horizontal-tb (default): horizontal content, from top to bottom;
  2. vertical-lr: vertical content, from left to right;
  3. vertical-rl: vertical content, from right to left.

2. Direction

The direction property defines how inline content (such as characters in a paragraph) flows on the screen. It can take two values:

  1. ltr (default): left-to-right
  2. rtl: right-to-left

CSS logical properties automatically map layouts to the content direction set with the direction and writing-mode properties.

A New Approach to Layout

1. Flexbox

The new approach to logical layouts started with the flexbox specifications. Flexbox doesn’t take into account physical dimensions such as left, right, top, and bottom. It uses values like flex-start and flex-end that rely on the direction of the main and cross axes

Whether flex items flow horizontally or vertically also depends on the position of the two axes you can define with the flex-direction property. All in all, concepts like "start" and "end" are entirely relative in flexbox and can easily be flipped.

2. CSS Grid

CSS Grid follows the same logic. CSS Grid doesn’t know left, right, top, and bottom, either. It lays out items in rows and columns, along two non-hierarchical axes. 

You can define the position of grid items with properties such as grid-row-start, or with named grid areas using the grid-template-areas property. Similarly to flexbox, CSS Grid also has logical properties instead of physical ones.

3. Logical Properties

CSS logical properties take this new approach to the next level. They change the way we define frequently used properties such as margin, paddingwidth, and height.

Physical vs. Logical Dimensions

Top, bottom, left, and right are the physical dimensions of the screen. Currently, CSS maps properties to these physical directions. However, when you have an RTL website, you don’t want to start writing from the left. For example, if you want to add a margin before the text, you need to use the margin-right property instead of margin-left, which you would use on an LTR page. 

And, this is just setting the margin of one content box. If you want to add RTL support to your whole site you either have to rewrite your entire CSS or perform the conversion with Sass or another CSS preprocessor.

Logical properties change this modus operandi. Instead of vertical and horizontal dimensions, they use block and inline dimensions:

  • The block dimension is how block items such as paragraphs are laid out on a page (in English from top to bottom). 
  • The inline dimension is how inline items such as characters in a paragraph are laid out on a page (in English from left to right). 

Here’s how logical dimensions map to physical dimensions in English and other LTR/horizontal-tb languages:

Logical dimensions Physical dimensions (English) 
block-start top
block-end bottom
inline-start left
inline-end right
Logical properties in English scriptLogical properties in English scriptLogical properties in English script

The logical-to-physical mapping looks like this in RTL/horizontal-tb scripts like Arabic:

Logical dimensions Physical dimensions (Arabic script)
block-start top
block-end bottom
inline-start right
inline-end left
Logical properties in Arabic scriptLogical properties in Arabic scriptLogical properties in Arabic script

And, this is how logical dimensions work with a vertical-rl script like Japanese when written in tategaki (縦書き) style:

Logical dimensions Physical dimensions (Japanese)
block-start right
block-end left
inline-start top
inline-end bottom
Logical properties in Japanese tategaki  vertical style scriptLogical properties in Japanese tategaki  vertical style scriptLogical properties in Japanese tategaki  vertical style script

As you can see, CSS logical properties will allow developers to flip layouts around on a whim.

CSS Logical Properties

CSS logical properties are currently defined by Level 1 specifications (Editor’s Draft; provided for open discussion). 

Below, you can find examples of the most important logical properties. They are the logical equivalents of frequently used physical properties such as margin and float. If you are interested in the full list of logical properties check out MDN’s reference guide, too.

1. Text Align

You can use the text-align property with the start and end values in browsers that support CSS logical properties. In English and other languages which are displayed LTR, start equates to left; and end equates to right

In RTL examples such as Arabic, it works reversely: start equates to right and end equates to left.

1
.align-start {
2
  /* Physical property | English */
3
  text-align: left;   
4
  
5
  /* Logical property */
6
  text-align: start;
7
}
8
.align-end {
9
/* Physical property | English */
10
  text-align: right;   
11
  
12
  /* Logical property */
13
  text-align: end;
14
}

In the demo below, you can test how text-align works with logical properties if you change direction or writing-mode of the page:

2. Margin, Padding, Border

You can also define margin, padding, and border with logical properties. You need to use the -inline-start postfix to indicate the start of the inline dimension, which is left in English and other LTR/tb languages. Similarly, the -inline-end postfix is used for the end of the inline dimension, which is right on English-language websites.

The -block-start postfix is for the start of the block dimension—in English: top, while  -block-end is for the end of the block dimension—in English: bottom.

1
.add-border {
2
  /* Physical properties | English */
3
  border-left: 0.625rem red solid;
4
  border-top: 0.625rem blue solid;
5
  border-right: 0.625rem purple solid;
6
  border-bottom: 0.625rem green solid;
7
    
8
  /* Logical properties */
9
  border-inline-start: 0.5rem red solid;
10
  border-block-start: 0.5rem blue solid;
11
  border-inline-end: 0.5rem purple solid;
12
  border-block-end: 0.5rem green solid;
13
}
14
15
.add-margin {
16
  /* Physical properties | English */
17
  margin-left: 0.25rem;
18
  margin-top: 0.5rem;
19
  margin-right: 0.75rem;
20
  margin-bottom: 1rem;
21
    
22
  /* Logical properties */
23
  margin-inline-start: 0.25rem;
24
  margin-block-start: 0.5rem;
25
  margin-inline-end: 0.75rem;
26
  margin-block-end: 1rem;
27
}
28
29
.add-padding {
30
  /* Physical properties | English */
31
  padding-left: 1.25rem;
32
  padding-top: 1.5rem;
33
  padding-right: 1.75rem;
34
  padding-bottom: 2rem;
35
    
36
  /* Logical properties */
37
  padding-inline-start: 1.25rem;
38
  padding-block-start: 1.5rem;
39
  padding-inline-end: 1.75rem;
40
  padding-block-end: 2rem;
41
}

Below, you can test logical properties belonging to the border property.

Note that you could also use the logical equivalents of longhand border properties. For instance, border-inline-start-color (instead of border-left-color) is a valid logical property, too.

You have probably noticed that something is missing here, though. Most likely, you frequently use the margin, padding, and border shorthands instead of longhands such as padding-top. How do logical properties handle these shorthands? Currently, they don’t handle them at all.

The logical equivalents of these shorthands is still an open question, under active discussion. There will be a solution in the future, however, right now, you need to use the longhand properties of logical margins, paddings, and borders. For more information, check out Issue 1282 in the GitHub repo of the CSS Working Group.

3. Float

You can also define floats using logical properties. In LTR scripts,  inline-start equates to left and inline-end equates to right. In RTL languages, it happens reversely, as here the inline dimension starts on the right of the screen.

1
.float-inline-start {
2
  /* Physical property | English */
3
  float: left;   
4
  
5
  /* Logical property */
6
  float: inline-start;
7
}
8
9
.float-inline-end {
10
  /* Physical property | English */
11
  float: right;   
12
  
13
  /* Logical property */
14
  float: inline-end;
15
}

You can test logical floats in the demo below:

4. Width and Height

Sizing properties, namely width and height, also have their logical equivalents. In top-to-bottom writing scripts, inline-size equates to width, as this is the dimension in which inline elements (e.g. characters in a paragraph) flow on the screen. And, block-size equates to height, as this is the dimension in which block elements (e.g. paragraphs) flow.

In vertical writing scripts, height is the inline dimension (inline-size), as characters flow vertically and width is the block dimension (block-size), as paragraphs flow horizontally.

As you can see below, max-width, max-height, min-width, and min-height have their own logical properties, too.

1
.add-dimensions {
2
   /* Physical property | English */
3
   width: 600px;
4
   height: 200px;
5
  
6
   /* Logical property */
7
   inline-size: 600px;
8
   block-size: 200px;
9
}
10
11
.max-dimensions {
12
   /* Physical property | English */
13
   max-width: 100%;
14
   max-height: 300px;
15
  
16
   /* Logical property */
17
   max-inline-size: 100%;
18
   max-block-size: 300px;
19
}
20
21
.min-dimensions {
22
   /* Physical property | English */
23
   min-width: 50%;
24
   min-height: 200px;
25
  
26
   /* Logical property */
27
   min-inline-size: 50%;
28
   min-block-size: 200px;
29
}

In the demo below, you can try out how the inline-size and block-size properties work with different directions and writing modes:

5. Position

You can position elements on the screen using logical properties as well. Logical positions replace the physical top, right, bottom, and left properties.

The start of the block dimension (top in English) is defined by the inset-block-start logical property. Similarly, the start of the inline dimension (right in English) is defined by the inset-inline-start property.

Logical positions also have a cool shorthand property: inset, which follows the 

  1. inset-block-start
  2. inset-inline-start
  3. inset-block-end
  4. inset-inline-end 

order.

1
.add-position {
2
   /* Physical properties | English */
3
   top: 0;
4
   right: 100px;
5
   bottom: 200px;
6
   left: 300px;
7
   
8
  
9
   /* Logical properties | Longhand */
10
   inset-block-start: 0;
11
   inset-inline-start: 100px;
12
   inset-block-end: 200px;
13
   inset-inline-end: 300px
14
   
15
   /* Logical properties | Shorthand */
16
   inset: 0 100px 200px 300px;
17
}

The demo below sticks a div to the start of both the block and inline axes, using the inset-block-start and inset-inline-start properties (in English top and left):

Conclusion

Logical properties are evidently logical and convenient, but it won’t be easy to get used to them as they require a completely different mindset. No doubt they will be a huge win for developers who need to support RTL and/or vertical language scripts. 

Thinking in terms of inline and block dimensions rather than physical directions will also change the way we think about layout. Let’s hope this new approach will result in some interesting layouts and multi-directional websites in the future, too.

Learn More About CSS Layout

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.