You can either use text-overflow
(see here)
div.cutMyText
{
text-overflow:clip;
}
A plugin like this one to auto change the end of text to an ellipsis if it's too long,
OR
you can create a function to do essentially the same thing. Here's an example of that. You can remove the + '...'
on line 64 of the javascript to just cut off the text
var txt = $(".cut").html();
trimText();
$(window).bind('resize', function(){
trimText();
})
function trimText(){
var totalWidth = $(window).width();
var linkWidth = $(".link").width();
var textWidth = totalWidth - linkWidth - 20;
var trimmedText = getTextThatFits(txt, textWidth, "Times New Roman", 12);
$(".cut").html(trimmedText);
}
function getTextThatFits(txt, w, fSize, fName, fWeight) {
if (fWeight === undefined)
fWeight = "normal";
var auxDiv = $("<div>").addClass("auxdiv").css ({
fontFamily : fName,
fontSize : parseInt(fSize) + "px",
position: "absolute",
height: "auto",
marginLeft : "-1000px",
marginTop : "-1000px",
fontWeight: fWeight,
width: "auto"
})
.appendTo($("body"))
.html(txt);
var ww = (auxDiv.width() + 1);
var str = txt;
auxDiv.html(" ...");
var w2 = (auxDiv.width() + 1) + "px";
if (w2 > w){
auxDiv.remove();
return "";
}
if (ww > w)
{
var i = 1;
var p, u;
var sol = 0;
p = 1; u = txt.length;
while (p <= u)
{
i = (p + u) >> 1;
str = txt.slice(0, i);
str += "...";
auxDiv.html(str);
ww = (auxDiv.width() + 1);
if (ww <= w) {
sol = i;
p = i + 1;
}
else u = i - 1;
}
str = txt.slice(0, sol) + "...";
}
$(".auxdiv").remove();
auxDiv.remove();
return str;
}
EDIT Here is an updated Fiddle based on your added jsFiddle and requests and if you want it to cut instead of use ellipses, look here. Both take account of text-transform
properties
The updated jQuery:
var trans = $('.Liclass.cut').css('text-transform');
var txt;
if( $('.Liclass.cut').css('text-transform') === 'uppercase')
txt = $(".cut").html().toUpperCase();
else { txt = $(".cut").html(); }
trimText();
$(window).bind('resize', function () {
trimText();
});
function trimText() {
var totalWidth = $(this).width();
var trimmedText = getTextThatFits(txt, totalWidth - 20, "Times New Roman", 12, trans);
alert(trimmedText);
$(".cut").html(trimmedText);
}
function getTextThatFits(txt, w, fSize, fName, fWeight, txtTrans) {
if (fWeight === undefined) fWeight = "normal";
var auxDiv = $("<div>").addClass("auxdiv").css({
fontFamily: fName,
fontSize: parseInt(fSize) + "px",
position: "absolute",
height: "auto",
marginLeft: "-1000px",
marginTop: "-1000px",
fontWeight: fWeight,
textTransform: txtTrans,
width: "auto"
})
.appendTo($("body"))
.html(txt);
var ww = (auxDiv.width() + 1);
var str = txt;
if (ww > w) {
var i = 1;
var p, u;
var sol = 0;
p = 1;
u = txt.length;
while (p <= u) {
i = (p + u) >> 1;
str = txt.slice(0, i);
str;
auxDiv.html(str);
ww = (auxDiv.width() + 1);
if (ww <= w) {
sol = i;
p = i + 1;
} else u = i - 1;
}
str = txt.slice(0, sol);
}
$(".auxdiv").remove();
auxDiv.remove();
return str;
}
Edit 2
I updated your Fiddle in the comments to what you seem to want. Really the only changes I made were taking out the float:left;
from the li
s and adding white-space:nowrap;
and display:block;
to the ul
's CSS. Use this for more info
On a side note I'm not sure why you have ID's for the types? For the CSS you can just put div
or li
instead of adding classes or IDs like #div