javascript CSS 文本省略号,包括“更多”链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26002974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS Text ellipsis including "More" link
提问by Ferran Negre
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/
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:
和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.
我想一定很容易......提前谢谢你。
回答by Adam Fratino
The div inside of .text
is still being displayed as a block element. Use this to fix:
里面的 div.text
仍然显示为块元素。使用它来修复:
.text > div { display: inline; }
回答by Christina
DEMO: http://jsbin.com/hatuv/1/edit
演示: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
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
}
回答by Defims
a pure css method base on -webkit-line-clamp, and you can custom the ...more content like a boss:
基于 -webkit-line-clamp 的纯 css 方法,您可以像老板一样自定义 ...更多内容:
@-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>
回答by batgerel.e
well using flexbox make it simple. Try this around
很好地使用 flexbox 使它变得简单。试试这个
.text {
display: flex;
> div {
flex: 0 0 40%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
}
https://codepen.io/baagii95/pen/byeNqZ
https://codepen.io/baagii95/pen/byeNqZ
btw i used sass. If you want in css version try this.
顺便说一句,我使用了 sass。如果你想在 css 版本中尝试这个。
.text {
display: flex;
}
.text > div {
flex: 0 0 40%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
回答by developppper
Just add to div some class and give it display:inline;
:
只需添加到 div 一些类并给它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;}