P粉7014918972023-08-26 10:07:02
CSS 3 includes a "hyphens
" property that sets hyphens based on the specified lang
property. But this is just a working draft, so support is a bit< a href="https://caniuse.com/#search=hyphens" rel="nofollow noreferrer">yet.
You can set it to
­
is declared, or Works fine in Firefox, Edge and even IE, but the main problem is that webkit doesn't support the "auto" value. Except on Mac and Android, this is the only value they accept. Yes, this is a weird bug.
This is an example, if you are running windows/linux be sure to check the difference between Firefox and Chrome.
p { width: 55px; border: 1px solid black; } p.none { -webkit-hyphens: none; -ms-hyphens: none; hyphens: none; } p.manual { -webkit-hyphens: manual; -ms-hyphens: manual; hyphens: manual; } p.auto { -webkit-hyphens: auto; -ms-hyphens: auto; hyphens: auto; }
<ul> <li><code>auto</code>: hyphen where the algorithm is deciding (if needed) <p lang="en" class="auto">An extremelyasdasd long English word</p> </li> <li><code>manual</code>: hyphen only at &hyphen; or &shy; (if needed) <p lang="en" class="manual">An extreme­lyasd long English word</p> </li> <li><code>none</code>: no hyphen; overflow if needed <p lang="en" class="none">An extreme­lyasd long English word</p> </li> </ul>
A common workaround for webkit's lack of "auto" support is to use hyphens:auto with webkit's work-break:break-all
, so the text will be connected using hyphens:auto on browsers that support it. Character concatenation and no hyphen wrapping on webkit.
P粉9501288192023-08-26 00:05:24
Use containers with display: inline-block
around long words.
body { margin-left: 30px; } div { border: solid 1px black; } .wide { border: solid 1px blue; width: 500px; } .narrow { border: solid 1px red; width: 360px; } h2 { font-family: 'Courier New'; font-weight: 400; font-size: 87px; } code { background-color: #eee; } .unbreakable {display:inline-block}
<p> <code>Super&shy;man</code> looks good in really narrow containers where the full word "Superman" would not fit</p> <p> <div class="narrow"><h2>Hi <span class="unbreakable">Super­man</span></h2></div> <p>And in this case the word "Superman" would fit unhypenhated on the second row.<br/> This uses the same code as the previous example</p> <div class="wide"><h2>Hi <span class="unbreakable">Super­man</span></h2></div>