2

I have text like this in HTML:

 <p>text texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext text</p>

Is it possible to make it like this:

text texttext texttext texttext text.....
Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
Siva
  • 113
  • 1
  • 2
  • 7

6 Answers6

2

Demo: http://cssdeck.com/labs/otf9h9pn

HTML:

<p class="ellipsis">text texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext</p>

CSS:

.ellipsis {
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
  overflow: hidden;
  width: 20em;
}
Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
1

Apply below CSS style for <p> tag.

p{
    width: 400px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

JSFIDDLE DEMO

shanidkv
  • 1,118
  • 1
  • 9
  • 12
0

yeah that's possible with ellipsis

dl dt {
    text-overflow: ellipsis;
    width: 200px;
    white-space: nowrap;
    overflow: hidden;
}
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
ikkez
  • 2,052
  • 11
  • 20
0

Here's what I use for my projects. You need JQuery.

How it works: assign the class .more to the div containing your text.

Check my DEMO

JQUERY

$(document).ready(function() {
// Configure/customize these variables.
var showChar = 100;  // How many characters are shown by default
var ellipsestext = "...";
var moretext = "Show more >";
var lesstext = "Show less";


$('.more').each(function() {
    var content = $(this).html();

    if(content.length > showChar) {

        var c = content.substr(0, showChar);
        var h = content.substr(showChar, content.length - showChar);

        var html = c + '<span class="moreellipses">' + ellipsestext+ '&nbsp;</span><span class="morecontent"><span>' + h + '</span>&nbsp;&nbsp;<a href="" class="morelink">' + moretext + '</a></span>';

        $(this).html(html);
    }

});

$(".morelink").click(function(){
    if($(this).hasClass("less")) {
        $(this).removeClass("less");
        $(this).html(moretext);
    } else {
        $(this).addClass("less");
        $(this).html(lesstext);
    }
    $(this).parent().prev().toggle();
    $(this).prev().toggle();
    return false;
  });
});

HTML

 <html>
     <head>
    <title>jQuery Read More/Less Toggle Example</title>
  </head>
  <body>
    <span class="more">
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </span>

  </body>
</html>

CSS

    .morecontent span {
    display: none;
}
.morelink {
    display: block;
}
U r s u s
  • 6,680
  • 12
  • 50
  • 88
0

Yes, this is possible.

For a single line of text use text-overflow:ellipsis;, like so

.truncate {
  width: 250px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

(Source CSS-Tricks)

For multi-line text it gets trickier. Check out this link http://html5hub.com/ellipse-my-text/

Personally for multi-line text I used a jQuery plugin, called dotdotdot.

Mo.
  • 26,306
  • 36
  • 159
  • 225
Daniel Apt
  • 2,468
  • 1
  • 21
  • 34
0

It is possible to do in pure JavaScript with substring()

var str = document.getElementById("longlist").innerHTML;
document.getElementById("shorten").innerHTML = str.substring(0,36)+".....";
<strong>Actual text</strong>
<p id="longlist">text texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext texttext text</p>
<hr />
<strong>Result:</strong>
<p id="shorten"></p>
Mo.
  • 26,306
  • 36
  • 159
  • 225