DEV Community

Cover image for Mastering RTL with One Line of CSS (No Libraries Needed!) 🚀
ALI ALHARBI
ALI ALHARBI

Posted on

1

Mastering RTL with One Line of CSS (No Libraries Needed!) 🚀

Ever thought RTL layouts were a hassle? Think again! You don’t need complex libraries or a pile of code. Here’s the simple solution to handle RTL like a pro.

The Magic One-Liner 🪄

Here’s all you need:

[lang="ar"] { direction: rtl }
Enter fullscreen mode Exit fullscreen mode

Then, with lang="ar" on <main>, the content inside flows from right to left, while the rest of the page stays untouched.

<main lang="ar">
  //.... RTL Content
</main>
Enter fullscreen mode Exit fullscreen mode

Yup, that’s it! By targeting elements with [lang="ar"], you tell your CSS exactly where to flip things around, making sure only Arabic (or any RTL language) sections flow from right to left. Clean, focused, and without affecting the whole page!

Why Not Just Use direction: rtl on body? 🤔

Good question! Setting direction: rtl on the html or body tags flips everything, which can mess with layouts for other languages. If you’re running a bilingual site, this one-liner keeps things neat and language-specific—no unexpected surprises!

What About Browser Translation? 🌍

Browsers translate and (usually) respect [lang="ar"] for layout direction. This keeps things tidy even if users switch languages. But let’s be real: browser translations aren’t perfect, so test it out and see the magic for yourself!

Got a Better Way? Prove Me Wrong! 🎉

Alright, maybe everyone already knows this 😅. If not, try it out. And if you’ve got a better solution, let me know!

Happy coding ✨

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay