javascript - Optimizing text length of limited space -


i displaying text strings of varying characters , length in div of fixed width. i'd fit text possible in allotted space. current approach limit number of text characters in following way

var n = //some number string = string.substr(0,n); 

the problem approach different characters have different widths while can cap on spill leaves unused space. i've tried giving containing div following css properties.

div{    width:200px    white-space:nowrap } 

but text still wraps next line down once on spills width. can me out practice way of addressing problem.

you might try text-overflow

from quirks mode:

text-overflow comes play when:

the box has overflow other visible (with overflow: visible text flows out of box) box has white-space: nowrap or similar method of constraining way text laid out. (without this, text wrap next line)

style change:

div{    width:200px;    white-space:nowrap;    overflow: hidden;    text-overflow: ellipsis; } 

working demo


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -