jquery - break-word on inline-block elements -
this may seem odd request, need set width of individual characters , have them act normal text in flowing paragraph. here's example:
ugliness aside, fine except fact words being split in half when wrapping. ideas on how work around this?
if change display "inline", wrapping works inline elements can't given width.
using letter-spacing instead of width might suffice, i'd need calculate like:
width = letter-spacing - (actual character width rendered)
but again, getting real width of inline element seems tricky.
the html/text side of things can't modified (e.g. solution such adding manual line breaks , setting css nowrap), it's being sent separate service, processing of needs automated.
note: font must arial, not fixed-width font , need support ie8+
edit: sorry, needs support inner formatting tags, strong or em (example updated). isn't supported in letteringjs, i'm using fork prevent these tags being stripped:
https://github.com/maranomynet/lettering.js/blob/master/jquery.lettering.js
if wrap each word in <span />
element, can set white-space:nowrap
on spans , text inside spans won't wrap in middle of word, whole word wrap.
here's example: http://jsfiddle.net/tgz3k/4/
note added js add span elements i'm referring to, used .lettering()
method on spans. had update css work properly.
updated js
$('p').html('<span>' + $("p").text().replace(/\s/g, '</span> <span>') + '</span>').children().lettering();
updated css
p span { white-space:nowrap; } p span span { display: inline-block; width: 10px; }
update
to support nested tags <b />
or <em />
can this:
$('p').html('<span>' + $("p").html().replace(/\s/gi, '</span> <span>').replace(/(<b>|<em>)/gi, '</span>$1<span>').replace(/(<\\b>|<\\em>)/gi, '</span>$1<span>') + '</span>').find("span").lettering();
where opening , closing html tags wrapped in opposing <span />
tags. e.g.
some text
turns into:
</span><b><span>some text</span></b><span>
and old regexp same spaces turns html into:
</span><b><span>some</span> <span>text</span></b><span>
and start our html string opening , end html string closing <span />
tag:
<span></span><b><span>some</span> <span>text</span></b><span></span>
there <span />
s created shouldn't mess flow of html.
Comments
Post a Comment