Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Hussein Al Hammad
Hussein Al Hammad

Posted on

     

Bidirectional horizontal rules in CSS

Thedir attribute in HTML and thedir property in CSS can be used to set the direction of text and horizontal flow. Some languages are written Left-To-Right (LTR), while others are written Right-To-Left (RTL). So having this level of control is vital for creating documents and interfaces for the web.

Building an interface to support both LTR and RTL layouts is a challenge. Flexbox and Grid certainly makes things easier, but they don't cover all our styling needs.

There are many CSS rules we write in which we specify a physical direction or side. For instance, when we write CSS to layout horizontal elements, it is common to set a margin only on a single side and set a margin of0 (on the same side) to the first or last sibling element.

.element{margin-right:1rem;}.element:last-child{margin-right:0;}
Enter fullscreen modeExit fullscreen mode

Or

.element:not(:last-child){margin-right:1rem;}
Enter fullscreen modeExit fullscreen mode

In the above example we are adding a margin on the right side of each element except the last. That's how it is written. Though what we mean when we write this is to add a marginafter the element in the direction of the horizontal flow of the document (or the parent element).

A common way to styleblockquote is to add a border to one side of the quote; the side we consider to comebefore the quote.

This isBulma's<blockquote> styling:

.contentblockquote{background-color:#f5f5f5;border-left:5pxsolid#dbdbdb;padding:1.25em1.5em;}
Enter fullscreen modeExit fullscreen mode

And there are many other styling choices we make in which we specify a side/direction (left/right) in CSS, but what we actually mean is before/after.

So when building interfaces that support both LTR and RTL layouts, one option would be to write CSS rules like:

.class{}html[dir="rtl"].class{}
Enter fullscreen modeExit fullscreen mode

Or perhaps even load different stylesheets for LTR and RTL layouts. However, both options require we either write more CSS orset up our tooling to generate the appropriate flipped styles. And there are alsotools that convert LTR to RTL styles for you.

Wouldn't it be nice to work with a lower level of abstraction instead? What if we can tell the browser we are targeting the side before/after an element instead of referring to the physical directions left/right? At the end of the day this is what we mean a lot of the times. This reminds me of this tweet:

keith•j•grant profile image
keith•j•grant
twitter logo
When you code CSS, you’re writing abstract rules to take *unknown* content and organize it in an *unknown* medium. That shit is hard.
13:26 PM - 17 Mar 2017
Twitter reply actionTwitter retweet actionTwitter like action

CSS has what is calledlogical properties. We can now tell the browser what we actually mean. Instead of using-left and-right, we can use-inline-start and-inline-end:

.element:not(:last-child){margin-inline-end:1rem;}
Enter fullscreen modeExit fullscreen mode

Similarly instead of using-top and-bottom, we can use-block-start and-block-end.

This means we can write one set of rules that target both LTR and RTL layouts. Here is an example using themargin-inline-end property:

Using theborder-inline-start andpadding-inline-start properties:

Firefox also supportsborder-*-*-radius so you can target different corners withborder-start-start-radius,border-end-start-radius, etc.

All demos:

For a deeper explanation, you can refer to Rachel Andrew's articleUnderstanding Logical Properties And Values. This is not just about RTL interfaces or horizontal bidirectional CSS rules. Rachel's article also covers logical dimensions.

Browser Support

Can I Use?:

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
abhinav1217 profile image
Abhinav Kulshreshtha
  • Location
    India
  • Joined

So much to learn in such a small lifetime.. Thanks mate. You have just reduced my css file by half. I actually used to build 2 separate css file from scss. One for left, and one for right. But if someone set's to right, We actually had to load both. Now I can suggests improvements to my boss for his clients.

It's good to know a lot of css accessibility features are gaining popularity.

CollapseExpand
 
hus_hmd profile image
Hussein Al Hammad
Lead developer at flourishworld.com (Dubai 🌴).
  • Location
    Dubai
  • Education
    Flinders University
  • Work
    Lead developer at Flourish Dubai
  • Joined

Great to hear! Note that logical properties are not supported in IE11. The Chromium-based Edge will support logical properties though:caniuse.com/#feat=css-logical-props

CollapseExpand
 
jabranr profile image
Jabran Rafique
Staff Engineer at Rated People | Sometimes maker
  • Location
    London, UK
  • Education
    MSc Computer Science (Web Development)
  • Work
    Staff Engineer at Rated People
  • Joined

Thank you for sharing. Extremely useful. Always good to learn some new CSS properties!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Lead developer at flourishworld.com (Dubai 🌴).
  • Location
    Dubai
  • Education
    Flinders University
  • Work
    Lead developer at Flourish Dubai
  • Joined

More fromHussein Al Hammad

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp