为什么文本被换行分隔,但没有显示连字符。

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

Why the text is wrapped (hyphenated), but without hyphens shown

问题

这是代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  
  <ol class="meaning">
    <li class="meaning-item">这是一个示例句子,其中长单词应该被连字符分隔
      <ul class="sentences">
        <li class="sentence">这是一个示例句子,其中长单词应该被连字符分隔</li>
        <li class="translation">这是一个示例句子,其中长单词应该被连字符分隔</li>
      </ul>
    </li>
  </ol>
  
</body>
</html>
.meaning {
  list-style-type: none;
  counter-reset: item;
  font-size: calc(0.7em + 2.5vw);
  word-break: break-all;
  hyphens: auto;
}

.meaning > li {
  position: relative;
}

.meaning > li::before {
  content: counter(item);
  counter-increment: item;
  position: absolute;
  top: 0;
  text-align: center;
  margin-left: calc(-0.7em - 2.5vw);
}

.sentences {
  list-style-type: none;
  padding-left: 0;
}

连字符已按预期包装,但连字符本身("-")没有显示在单词断开的位置。此外,我希望明确告诉浏览器<li class="sentence">中的文本是英语(en),而<li class="translation">中的文本是德语(de)。我知道并非所有浏览器都具有针对特定语言的内置连字符指令,但我想试试看。我还将这个代码保存在了JS Bin中。谢谢!


<details>
<summary>英文:</summary>

This is the code:

        &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;/head&gt;
    &lt;body&gt;
      
      &lt;ol class=&quot;meaning&quot;&gt;
        &lt;li class=&quot;meaning-item&quot;&gt;This is the example of the sentence where the long words should be hyphenated
          &lt;ul class=&quot;sentences&quot;&gt;
            &lt;li class=&quot;sentence&quot;&gt;This is the example of the sentence where the long words should be hyphenated&lt;/li&gt;
            &lt;li class=&quot;translation&quot;&gt;This is the example of the sentence where the long words should be hyphenated&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/li&gt;
      &lt;/ol&gt;
      
    &lt;/body&gt;
    &lt;/html&gt;
    
    .meaning {
      list-style-type: none;
      counter-reset: item;
      font-size: calc(0.7em + 2.5vw);
      word-break: break-all;
      hyphens: auto;
    }
    
    .meaning &gt; li {
      position: relative;
    }
    
    .meaning &gt; li::before {
      content: counter(item);
      counter-increment: item;
      position: absolute;
      top: 0;
      text-align: center;
      margin-left: calc(-0.7em - 2.5vw);
    }
    
    .sentences {
      list-style-type: none;
      padding-left: 0;
    }

&lt;br&gt;The words are wrapped as I want them to, but the hyphen itself (&quot;-&quot;) is not shown at the point where the word is wrapped.&lt;br&gt;&lt;br&gt; Besides, I want to *explicitly* tell the browser that text in `&lt;li class=&quot;sentence&quot;&gt;` is in English (en) and in `&lt;li class=&quot;translation&quot;&gt;` - in German (de). I know that not all browsers have built-in instructions for hyphening specific languages, but I want to give it a try.&lt;br&gt;&lt;br&gt; I also [saved][1] this code in the JS Bin. &lt;br&gt;&lt;br&gt;Thank you!


  [1]: https://jsbin.com/nocuqeyeno/edit?html,css,output

</details>


# 答案1
**得分**: 0

1.) 不要使用 `word-break: break-all;` – 它会在*任何*位置断开单词,而不考虑连字符规则。

2.) 在 `html` 标签中需要一个 `lang` 属性,结合 `hyphens: auto;` 来启用自动连字符。

3.) 你可以在包含另一种语言的任何元素中使用*不同的* `lang` 属性 – 看我如何将 `lang=&quot;de&quot;` 应用于下面的最后一个 `li` 元素。

<details>
<summary>英文:</summary>

1.) Don&#39;t use `word-break: break-all;` – it breaks words at *any* position, regardless of hyphenation rules.

2.) You need a `lang` attribute in the `html` tag in combination with `hyphens: auto;` to activate automatic hyphenation.

3.) You can use a *different* `lang` attribute in any element that contain another language – see how I applied `lang=&quot;de&quot;` to the last `li` element below.

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-css --&gt;

    .meaning {
      list-style-type: none;
      counter-reset: item;
      font-size: calc(0.7em + 2.5vw);
      hyphens: auto;
    }

    .meaning &gt; li {
      position: relative;
    }

    .meaning &gt; li::before {
      content: counter(item);
      counter-increment: item;
      position: absolute;
      top: 0;
      text-align: center;
      margin-left: calc(-0.7em - 2.5vw);
    }

    .sentences {
      list-style-type: none;
      padding-left: 0;
    }

&lt;!-- language: lang-html --&gt;

    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;/head&gt;
    &lt;body&gt;
      
      &lt;ol class=&quot;meaning&quot;&gt;
        &lt;li class=&quot;meaning-item&quot;&gt;This is the example of the sentence where the sophisticated extraordinariliy long words should be hyphenated
          &lt;ul class=&quot;sentences&quot;&gt;
            &lt;li class=&quot;sentence&quot;&gt;This is the example of the sentence where the long words should be hyphenated&lt;/li&gt;
            &lt;li class=&quot;translation&quot; lang=&quot;de&quot;&gt;Das ist ein Beispiel f&#252;r einen Satz, der au&#223;ergew&#246;hnlich &#252;berlange W&#246;rter enth&#228;lt, welche am Zeilenende bei Bedarf geteilt werden sollen.&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/li&gt;
      &lt;/ol&gt;
      
    &lt;/body&gt;
    &lt;/html&gt;

&lt;!-- end snippet --&gt;



</details>



huangapple
  • 本文由 发表于 2023年7月14日 03:20:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682625.html
匿名

发表评论

匿名网友

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

确定