繁体   English   中英

css 用连字符断词

[英]css break word with hyphen

我想在第一行末尾用连字符打破长词。

预期的:

BERUFSBILDUNGSZENT-
RUM

明白啦:

BERUFSBILDUNGSZENT
RUM

这是我的html和css:

<div class="content">BERUFSBILDUNGSZENTRUM</div>

.content {
  max-height: 80px;
  width: 200px;
  overflow-x: hidden;
  word-wrap: break-word;
  padding: 10px;
  -webkit-hyphens: auto;
  -moz-hyphens: auto;
  -ms-hyphens: auto;
  hyphens: auto;
  font-weight: bold;
  font-size: 16px;
  border: solid 1px #000;
}

你可以查看我的JSFiddle

Chrome 显然不做断字(至少在 Windows 上)。 使用其他浏览器或平台可能会更好。 你可以使用&shy; (软连字符)如果您事先知道要中断的位置。 否则,至少在 Windows 上的 Chrome 中,当 CSS 中断一个长单词时,没有办法获得连字符,除非它在输入中开始。

 .content { max-height: 80px; width: 200px; overflow-x: hidden; word-wrap: break-word; padding: 10px; font-weight: bold; font-size: 16px; border: solid 1px #000; }
 Using soft hyphen: <div class="content">BERUFSBILDUNGSZEN&shy;TRUM</div> Using automatic hyphenation (doesn't work in Chrome) <div class="content" lang="de" style="hyphens: auto; ">BERUFSBILDUNGSZENTRUM</div> Soft hyphen not displayed if it doesn't break there <div class="content" style="width: 400px; ">BERUFSBILDUNGSZEN&shy;TRUM</div>

另请参阅此答案

2022年解决方案:

如果您将lang属性添加到您的 div 并键入正常大小写的“Berufsbildungszentrum”,使用hyphens: auto; 确实按预期工作。 然后,您可以使用text-transform: uppercase; .

 .content { max-height: 80px; width: 200px; overflow-x: hidden; padding: 10px; -webkit-hyphens: auto; -moz-hyphens: auto; -ms-hyphens: auto; hyphens: auto; font-weight: bold; font-size: 16px; text-transform: uppercase; border: solid 1px #000; }
 <div lang="de" class="content">Berufsbildungszentrum</div>

还要检查更新的JSFiddle

lang="de"属性添加到您的 HTML 标记中,因为浏览器正在使用根据该语言标记决定的算法来决定这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM