HTML & CSS · Beginner · 15 min
Style a notice for both reading directions
Use CSS properties that follow the direction of the text.
Set the reading direction in HTML
Use dir="ltr" for left-to-right text and dir="rtl" for right-to-left text. In horizontal text, the inline start is on the left for the first direction and on the right for the second. The lang attribute identifies the language. The dir attribute sets the direction.
<p class="note" lang="en" dir="ltr">The workshop starts at 10 a.m.</p>
<p class="note" lang="ar" dir="rtl">تبدأ الورشة في العاشرة صباحًا.</p>Expected appearance
dir="ltr": start → end dir="rtl": end ← start
Style the start and end edges
Use border-inline-start for the border at the start of the line. Use padding-inline-start and padding-inline-end for space inside the two edges. These CSS properties follow the direction of each element. Use text-align: start to align the text to its starting edge.
.note {
border-inline-start: 4px solid #1d4ed8;
padding-inline-start: 16px;
padding-inline-end: 8px;
text-align: start;
}Expected appearance
LTR: left border, 16px left padding, 8px right padding RTL: right border, 16px right padding, 8px left padding
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Edit the page content and include CSS in a style element. The preview supports a limited set of HTML elements. Scripts, external resources, navigation to other sites, and form submission are disabled.
Build and check
Keep both notices and their language and direction attributes. In one .note rule, add border-inline-start: 4px solid #1d4ed8, padding-inline-start: 16px, padding-inline-end: 8px, and text-align: start.
Use the same .note rule for both notices. The inline start and end follow the direction of each element.
Show worked solution
<style>
.notices { max-width: 34rem; margin: 1rem auto; font-family: sans-serif; line-height: 1.5; }
.note {
padding-block: 0.75rem;
color: #182338;
background: #eef3fa;
border-inline-start: 4px solid #1d4ed8;
padding-inline-start: 16px;
padding-inline-end: 8px;
text-align: start;
}
</style>
<main class="notices">
<h1>Workshop notices</h1>
<p class="note" lang="en" dir="ltr">The workshop starts at 10 a.m.</p>
<p class="note" lang="ar" dir="rtl">تبدأ الورشة في العاشرة صباحًا.</p>
</main>The same rule places the border and the larger padding on the starting edge of each notice. The English notice starts at the left and the Arabic notice starts at the right.
Page preview
Run to preview your page. Check tests the task requirements, including the requested screen widths. Check results do not replace keyboard and visual review.
Try it yourself
- Preview both notices and check that the border is on the left in the English notice and on the right in the Arabic notice.
- Change the direction of one notice temporarily. Preview it again to see the border and padding move, then restore its original direction before checking.