Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Text ellipsis including "More" link

So I have the following Fiddle that has set an ellipsis in a text into two lines. Then I want to have a 'More' link inline with the text.

http://jsfiddle.net/csYjC/2876/

So if our text has more than two lines, it should look like this:

enter image description here

That's correct. However:

enter image description here

That's not correct (should be inline with the text).

Code is like follows:

<div class="text">
  <div>Lorem ipsum dolor sit amet, Lorem Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet, Lorem Lorem i</div>
  <a href="#">More</a>
</div>

And the css:

.text{
   display: inline;
   overflow: hidden;
   text-overflow: ellipsis;
   display: -webkit-box;
   line-height: 24px;     /* fallback */
   max-height: 48px;      /* fallback */
   -webkit-line-clamp: 2; /* number of lines to show */
   -webkit-box-orient: vertical;
}

.text a {
    position: absolute;
}

I guess must be easy... Thank you in advance.

like image 486
Ferran Negre Avatar asked Sep 23 '14 19:09

Ferran Negre


People also ask

How do you do ellipses in CSS?

Draw a simple rectangle. Your choice of height and width , of the rectangle, will dictate the size and shape of the ellipse. The border-radius refers to the curvature at the corners of the ​shape; it should be set to a very high value (50% to 100%). An ellipse has been created!

How do you overflow text in CSS?

A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. The white-space property must be set to nowrap and the overflow property must be set to hidden. The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string.


1 Answers

Well, here's a kind of a funny solution with pure CSS using pseudo elements. http://jsfiddle.net/j8ekzvLq/

.text {
   display: inline;
   overflow: hidden;
   text-overflow: ellipsis;
   display: -webkit-box;
   line-height: 24px;     /* fallback */
   max-height: 48px;      /* fallback */
   -webkit-line-clamp: 2; /* number of lines to show */
   -webkit-box-orient: vertical;
   position: relative;
   padding-bottom: 24px;
}

.text::before {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 24px;
    background-color: #fff;
    z-index: 1;
}

.text div {
    background-color: #fff;
    display: inline;
    position: relative;
}

.text div::after {
    content: "";
    background-color: #fff;
    position: absolute;
    bottom: -24px;
    left: 0;
    width: 100%;
    height: 24px;
    z-index: 2;
}

.text a::before {
    content: "More";
    position: absolute;
    bottom: 0;
    left: 0;
    text-decoration: underline;
    z-index: 1;
}
like image 118
Adam Avatar answered Nov 10 '22 18:11

Adam