如何使混合RTL<pre>标记中的英文文本从右向左呈现?

huangapple go评论58阅读模式
英文:

How to make English text render right-to-left in mixed RTL <pre> tag?

问题

You can achieve the desired result by modifying the HTML structure and using CSS to position the elements differently. Here's the modified code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .rtl {
      direction: rtl;
      font-size: 16px;
      font-family: Arial, Helvetica;
    }
    .container {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  </style>
</head>
<body>
<div class="container">
  <div>
    <b>Right Text</b>
  </div>
  <pre class="rtl">
    שלום עולם ..... Left Text חמש
  </pre>
</div>
</body>
</html>

This code places "Right Text" to the left of "שלום עולם" and "Left Text" to the right of "חמש," achieving the desired result.

英文:

I have the following code:

&lt;html&gt;
&lt;head&gt;
  &lt;style&gt;
    .rtl {
      direction: rtl;
      font-size: 16px;
      font-family: Arial, Helvetica;
    }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;pre class=&quot;rtl&quot;&gt;
שלום עולם &lt;b&gt;Right Text&lt;/b&gt; ..... &lt;b&gt;Left Text&lt;/b&gt; חמש
&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;

JSFiddle: https://jsfiddle.net/xq809b4g/2/

Currently it renders "LeftText" left to שלום עולם, and "RightText" right to "חמש".

Usually, this makes sense, because when I embed English text in RTL text, I'd usually want it to display the English sentence Left-to-Right. However, in this case, because I'm writing a table of contents - The subjects should appear on the right, and the authors should appear on the left.

Can I make it render the opposite way, while still keeping the &lt;pre&gt; tag? In other words, "Right Text" should appear left to "שלום עולם", and "Left Text" right to "חמש".

Current State:

如何使混合RTL<pre>标记中的英文文本从右向左呈现?

Desired result:

如何使混合RTL<pre>标记中的英文文本从右向左呈现?

答案1

得分: 2

你应该使用 dir 属性。

使用 dir 属性时,可以使用以下值:

ltr,表示从左到右,适用于从左到右书写的语言。

rtl,表示从右到左,适用于从右到左书写的语言。

auto,让用户代理程序决定。它在解析元素内的字符时使用基本算法,直到找到具有强方向性的字符,然后将该方向性应用于整个元素。

<pre dir="auto">
  שלום עולם <b dir="ltr">RightText</b> ..... <b dir="ltr">LeftText</b> חמש
</pre>
英文:

You should make the use of dir attribute.

When using dir attribute, you can use these values:

ltr, which means left to right and is to be used for languages that are written from the left to the right.

rtl, which means right to left and is to be used for languages that are written from the right to the left.

auto, which lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element.

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-html -->

&lt;pre dir=&quot;auto&quot;&gt;
  שלום עולם &lt;b dir=&quot;ltr&quot;&gt;RightText&lt;/b&gt; ..... &lt;b dir=&quot;ltr&quot;&gt;LeftText&lt;/b&gt; חמש
&lt;/pre&gt;

<!-- end snippet -->

答案2

得分: 1

除了Newbee的出色答案外,在句点前后添加&amp;rlm字符允许在句中改变文本方向。

英文:

In addition to Newbee's excellent answer, adding an &amp;rlm character before and after the dots allows mid-sentence changing of the direction of the text.

&lt;html&gt;
&lt;head&gt;
  &lt;style&gt;
    .rtl {
      direction: rtl;
      font-size: 16px;
      font-family: Arial, Helvetica;
    }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;pre class=&quot;rtl&quot;&gt;
שלום עולם &lt;b&gt;Right Text&lt;/b&gt; &amp;rlm;.....&amp;rlm‏; &lt;b&gt;Left Text&lt;/b&gt; חמש
&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年4月17日 13:05:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定