Two ways to handle ltr/rtl on a page.

I am a web developer from Greece, working mostly with Laravel.
Search for a command to run...

I am a web developer from Greece, working mostly with Laravel.
I wish that would also work for Hashnode posts! Lol
Hello. Today I'm making a simple slideshow using cool CSS and JS features. I'm gonna be using: CSS variables CSS transformations CSS functions CSS grid JS Proxy The HTML <div class="slideshow"> <div class="slides-container"> <div class="slid...

In this post I will demonstrate how to create a brand logo grid like the one below: The tricky part of such a grid is the colored gap between the logo tiles. The solution I came up with, I think is acceptable. I added a 1px box-shadow to the grid it...

Hello again. In this post, I'm going to share a thought I had. For the last six months, I've been struggling with not-so-interesting work. It was some things that I had to do that were so boring I almost believed that I didn't like my job anymore. I ...

When we need to show a list of items, such as products in a product list page, we have the following two css alternatives: display: grid display: flex I will demonstrate both here and I will explain why I choose one over the other. Usually, we have...

When creating a multilingual web page you might need to handle languages written from right to left. This comes with some design issues.
First, you must add the dir attribute to your root (<html>) element. But the dir attribute alone cannot tackle everything. Sometimes you might have an image next to a paragraph, and a margin-left spacing them out. When you switch to the rtl language, you end up with a paragraph with a left margin, and an image stuck on the right side of it.
I will show you two ways that I have used to solve this problem.
The first way I thought was using CSS variables and some calculations to play with the left margin.
html[dir="ltr"] {
--ltr: 1;
--rtl: 0;
}
html[dir="rtl"] {
--ltr: 0;
--rtl: 1;
}
p {
margin-left: calc(var(--ltr) * 20px);
margin-right: calc(var(--rtl) * 20px);
}
What I do here is assign a number (0, 1) to the "boolean" variables --ltr and --rtl, declaring which direction is enabled. When the dir changes, the variables swap values. Then I calculate the left and right margins by multiplying the variables with the desired pixel value. So in ltr the left margin will be 1 × 20px = 20px, and the right margin will be 0 × 20px = 0. When we change to rtl the margins will swap values.
This is a clever way of setting the margins based on a boolean enabled/disabled variable. But, there is a better way by using modern CSS.
p {
margin-inline-start: 20px;
}
And that's it. Way less code, and more elegant. It actually instructs the browser to add a 20px margin at the "start" of the x-axis. The start of the x-axis is relative to the direction set by the dir attribute. So in this case the browser will set a 20px left margin on ltr direction, and a 20px right margin on rtl direction.