Can I use CSS to justify text with hyphenating words at the end of a line?

时间秒杀一切 提交于 2019-12-22 05:14:36

问题


I have a div that is too narrow to text-align:justify (gaps too wide), but yet looks un-uniform when right- or left-justified, because then there is a large gap at the end of lines. Left-justified looks best, but could I use hyphenation, like in books? Using CSS?


回答1:


You can use hyphens: auto provided that you have declared the content language in HTML, e.g. using <html lang=en-US>.

Browser support is still limited but getting better, see http://caniuse.com/css-hyphens

For good quality, you may need to manually control hyphenation e.g. by using soft hyphens (&shy;) in words that might otherwise be hyphenated wrong.




回答2:


Hyphenation is language-dependent; you can't just put overflowing characters on the next line and get the same effect that you do in books. Hyphenation rules are actually pretty complicated (see http://dictionary2.classic.reference.com/writing/styleguide/division.html), and the only way that you can do it properly (like in books) is either manually or via a client-side or server-side script; I'm pretty sure it can't be done using only css.

You might want to use something like http://code.google.com/p/hyphenator/




回答3:


For hyphenation in html, there are javascript programs to add what's needed to the text. such as http://code.google.com/p/hyphenator/

Support for brower buildin hyphenation is unreliable and improvement in its support is doubtful.



来源:https://stackoverflow.com/questions/9511014/can-i-use-css-to-justify-text-with-hyphenating-words-at-the-end-of-a-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!