The issue you're encountering is due to how browsers handle the text-align: justify property in combination with dir="rtl" (right-to-left direction) for single-line paragraphs. When a paragraph is short enough to fit on one line, the justification doesn't apply in the same way as it does for multi-line paragraphs, leading to the behavior you've described.
To correct this and ensure that single-line paragraphs with dir="rtl" and text-align: justify are right-aligned, you can use the following approaches:
1. Using text-align: right for Single-Line RTL Text
If you specifically want to target single-line paragraphs or ensure that any RTL text is right-aligned, you can override the text-align property for RTL elements. However, since you want to maintain justification for multi-line paragraphs, this approach requires a bit more specificity.
2. Applying a Minimum Width
Another workaround is to ensure that the paragraph always spans more than one line by setting a minimum width that is larger than the width of the text. However, this is not practical for dynamic content.
3. Using :after Pseudo-Element
A more dynamic approach involves using the :after pseudo-element to add a "justifying" element that will make the browser treat the line as if it needs justification, thus aligning it to the right for RTL text.
Here's an example of how you can implement this:
.rtl-justify {
direction: rtl;
text-align: justify;
}
.rtl-justify:after {
content: "";
display: inline-block;
width: 100%;
}
And your HTML would look something like this:
<p class="rtl-justify">Your short paragraph here.</p>
This method works by adding an invisible element at the end of the paragraph that takes up the full width of the container, prompting the browser to justify the text, which for RTL means aligning it to the right.
Note:
The behavior of text-align: justify with dir="rtl" can vary slightly between browsers, especially in how they handle the last line of a justified paragraph. The pseudo-element method is a common workaround for ensuring consistent behavior across different browsers for single-line paragraphs.