问题
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:

That's correct. However:

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.
回答1:
The div inside of .text
is still being displayed as a block element. Use this to fix:
.text > div { display: inline; }
http://jsfiddle.net/csYjC/2880/
回答2:
DEMO: http://jsbin.com/hatuv/1/edit
<div class="text">
<p>Lorem ipsum dolor sit amet, Lorem Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet, Lorem Lorem i </p> <a href="#">More</a>
</div>
CSS
.text {
overflow: hidden /* just for clearing */
}
.text p {
display: inline-block;
text-overflow: ellipsis;
width: 100%;
white-space: nowrap;
overflow: hidden;
margin-top: 0;
box-sizing: border-box;
padding-right: 40px;
margin-right: -40px;
}
.text a {
float: right
}
回答3:
a pure css method base on -webkit-line-clamp, and you can custom the ...more content like a boss:
@-webkit-keyframes ellipsis {/*for test*/
0% { width: 622px }
50% { width: 311px }
100% { width: 622px }
}
.ellipsis {
max-height: 40px;/* h*n */
overflow: hidden;
background: #eee;
-webkit-animation: ellipsis ease 5s infinite;/*for test*/
/**
overflow: visible;
/**/
}
.ellipsis .content {
position: relative;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-box-pack: center;
font-size: 50px;/* w */
line-height: 20px;/* line-height h */
color: transparent;
-webkit-line-clamp: 2;/* max row number n */
vertical-align: top;
}
.ellipsis .text {
display: inline;
vertical-align: top;
font-size: 14px;
color: #000;
}
.ellipsis .overlay {
position: absolute;
top: 0;
left: 50%;
width: 100%;
height: 100%;
overflow: hidden;
/**
overflow: visible;
left: 0;
background: rgba(0,0,0,.5);
/**/
}
.ellipsis .overlay:before {
content: "";
display: block;
float: left;
width: 50%;
height: 100%;
/**
background: lightgreen;
/**/
}
.ellipsis .placeholder {
float: left;
width: 50%;
height: 40px;/* h*n */
/**
background: lightblue;
/**/
}
.ellipsis .more {
position: relative;
top: -20px;/* -h */
left: -50px;/* -w */
float: left;
color: #000;
width: 50px;/* width of the .more w */
height: 20px;/* h */
font-size: 14px;
/**
top: 0;
left: 0;
background: orange;
/**/
}
<div class='ellipsis'>
<div class='content'>
<div class='text'>text text text text text text text text text text text text text text text text text text text text text </div>
<div class='overlay'>
<div class='placeholder'></div>
<div class='more'>...<a href="http://hai.li">more</a></div>
</div>
</div>
</div>
回答4:
well using flexbox make it simple. Try this around
.text {
display: flex;
> div {
flex: 0 0 40%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
}
https://codepen.io/baagii95/pen/byeNqZ
btw i used sass. If you want in css version try this.
.text {
display: flex;
}
.text > div {
flex: 0 0 40%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
回答5:
Just add to div some class and give it display:inline;
:
<div class="text">
<div class="loremclass">Lorem ipsum dolor sit amet, Lorem Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet, Lorem Lorem ipsum dolor sit amet</div>
<a href="#">More</a>
</div>
and
body {
margin: 20px;
}
.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;
}
.loremclass{display:inline;}
来源:https://stackoverflow.com/questions/26002974/css-text-ellipsis-including-more-link