This is a common issue with justified text in RTL contexts. When you use text-align: justify, browsers typically fall back to the default alignment for the last line (or single line) of a paragraph, which is often left-aligned even in RTL contexts.
Here are several solutions you can try:
Solution 1: Use text-align-last
p[dir="rtl"] {
text-align: justify;
text-align-last: right;
}
The text-align-last property specifically controls the alignment of the last line in a block, which includes single-line paragraphs.
Solution 2: Use logical properties
p[dir="rtl"] {
text-align: justify;
text-align-last: end;
}
Using end instead of right makes it more robust for different writing directions.
Solution 3: CSS fallback approach
If you need broader browser support, you can use a combination approach:
p[dir="rtl"] {
text-align: justify;
text-align-last: right;
text-align-last: end; /* This will override the previous line in supporting browsers */
}
Solution 4: JavaScript detection (if needed)
If the CSS solutions don't work in your target browsers, you could detect single-line paragraphs with JavaScript and apply different styling:
function adjustRTLAlignment() {
const rtlParagraphs = document.querySelectorAll('p[dir="rtl"]');
rtlParagraphs.forEach(p => {
if (p.scrollHeight <= p.clientHeight && p.scrollWidth <= p.clientWidth) {
p.style.textAlign = 'right';
}
});
}
The text-align-last approach is the most straightforward and has good modern browser support, so I'd recommend starting with that.