Why don't hyphens work with inner <span> s?
I am trying to get hyphens working on text that contains <span>
elements inside to be emphasized. This seems to break the hyphen algorithm. Is there a way to fix the behavior so that the hyphens are the same as without the elements <span>
? I am not asking for a workaround like­
Code (sandbox: https://codepen.io/anon/pen/ayzxpM ):
.limit {
max-width: 50px;
hyphens: auto;
font-size: 20px;
background-color: #eee;
}
span {
color: red;
}
<div class="limit">
<p>
Appletreefruitthing
</p>
<p>
Apple<span>tree</span>fruitthing
</p>
</div>
Using the lang attribute
Adding a lang attribute, as suggested by Vadim Ovchinnikov ( <div class="limit" lang="en">
), may yield better results in some platform / browser combinations. In Firefox 54, Windows 10, this is the output:
But even that feels like a buggy. The hyphen should be black in my opinion, and the hyphen algorithm seems to be missing out on the chance to make a line break between "fruit" and "tree", also completely ignoring max-width
which is set for the container.
source to share
Chrome only has partial support for the property hyphens
(Mac and Android platforms only), so you can't get it to work on Windows.
I see no difference between presence and absence span
in Firefox, IE and Edge (all on Windows) for this code.
For it to work you need to set lang
for container and add vendor prefixes (for -ms-hyphens
IE / Edge and -webkit-hyphens
for Safari). Demo video:
.limit {
max-width: 50px;
font-size: 20px;
/* Safari */
-webkit-hyphens: auto;
/* IE, Edge */
-ms-hyphens: auto;
hyphens: auto;
background-color: #eee;
}
span {
color: red;
}
<div class="limit" lang="en">
<p>
Appletreefruitthing
</p>
<p>
Apple<span>tree</span>fruitthing
</p>
</div>
To work in all browsers you shouldn't use a CSS property hyphens
, just insert ­
manually where you want the hyphens.
.limit {
max-width: 50px;
font-size: 20px;
background-color: #eee;
}
span {
color: red;
}
<div class="limit">
<p>
Apple­tree­fruitthing
</p>
<p>
Apple­<span>tree</span>­fruitthing
</p>
</div>
source to share
hyphens: manual
togteher with
­
may work see documentation here https://css-tricks.com/almanac/properties/h/hyphenate/
this code works on codefen
<div class="limit">
<p>
Appletreefruitthing
</p>
<p>
Apple­<span>tree</span>­fruit­thing
</p>
</div>
CSS
.limit {
hyphens: manual;
}
source to share