Home  >  Q&A  >  body text

Why is the text wrapped but no hyphens are shown?

This is the code:

<!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;
}

The words are wrapped the way I want them to be, but the hyphen itself ("-") is not showing up where the words wrap.

Additionally, I want to explicitly tell the browser that the text in

  • is in English (en), # The text in
  • ## is in German (de). I know not all browsers have language-specific built-in directives for hyphens, but I wanted to give it a try.

    I also saved this code in JS Bin.

    Thanks!

  • P粉060112396P粉060112396424 days ago671

    reply all(1)I'll reply

  • P粉920199761

    P粉9201997612023-09-15 18:21:03

    1.) Don't use word-break: break-all; - it will break the word anywhere, regardless of hyphen rules.

    2.) Use the lang attribute in the html tag combined with hyphens: auto; to enable automatic hyphens.

    3.) You can use different lang attributes in any element that contains other languages ​​- see below how I put lang="de" Applies to the last li element.

    .meaning {
      list-style-type: none;
      counter-reset: item;
      font-size: calc(0.7em + 2.5vw);
      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;
    }
    <!DOCTYPE html>
    <html lang="en">
    <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" lang="de">Das ist ein Beispiel für einen Satz, der außergewöhnlich überlange Wörter enthält, welche am Zeilenende bei Bedarf geteilt werden sollen.</li>
          </ul>
        </li>
      </ol>
      
    </body>
    </html>

    reply
    0
  • Cancelreply