CSS文本省略号包括"更多"链接

时间:2014-09-23 19:16:10

标签: javascript css css3 ellipsis

所以我有以下小提琴,它将文本中的省略号设置为两行。然后我想要一个与文本内联的“更多”链接。

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

因此,如果我们的文本有两行以上,它应该如下所示:

enter image description here

这是对的。但是:

enter image description here

这不正确(应与文字内联)。

代码如下:

<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>

和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;
}

我想一定很容易......提前谢谢你。

7 个答案:

答案 0 :(得分:0)

.text内的div仍然显示为块元素。用此来修复:

.text > div { display: inline; }

http://jsfiddle.net/csYjC/2880/

答案 1 :(得分:0)

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
}

答案 2 :(得分:0)

使用flexbox使其变得简单。试试看

.text {
  display: flex;
  > div {
    flex: 0 0 40%;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
  }
}

https://codepen.io/baagii95/pen/byeNqZ

我曾经用过无礼的话。如果要使用CSS版本,请尝试此操作。

.text {
  display: flex;
}

.text > div {
  flex: 0 0 40%;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

答案 3 :(得分:0)

我用canvas来获取div中内容所占据的字符串的宽度(因为不同的字体在测量上会有细微的差异)。这里我计算了内容被划分的行数为将字符串的宽度(即 content.textContent)除以 div 内容的宽度

当它达到或等于 3 时,anchor tag 的显示属性将更改为阻止,当它小于 3 时将恢复为内联。

输出:

enter image description here

这行得通

var content = document.getElementsByClassName('content')[0];
var more = document.getElementsByClassName('more');

var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var font = "30px times new roman";
context.font = font;
var text = content.textContent;
var width = context.measureText(text).width;
var w = Math.ceil(width);



window.onload = function() {
  change_more();
}

window.onresize = function() {
  change_more();
}


function change_more() {
  var check = Math.ceil(w / content.offsetWidth);

  if (check > 2) {
    more[0].style.display = "none";
    more[1].style.display = "block";
    content.style.textOverflow = "ellipsis";
  } else {
    more[0].style.display = "inline";
    more[1].style.display = "none";
    content.style.textOverflow = "clip";
  }
}
* {
  margin-left: 0px;
  padding: 0px;
}

.content {
  overflow: hidden;
  display: -webkit-box;
  /* fallback */
  font-size: 30px;
  max-height: 90px;
  /* fallback */
  -webkit-line-clamp: 2;
  /* number of lines to show */
  -webkit-box-orient: vertical;
}

a {
  font-size: 30px;
}
<div class="text">
  <div class="content" style="">Lorem ipsum dolor sit amet, Lorem Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet, Lorem Lorem Lorem ipsum dolor sit amet, Lorem Lorem ipsum dolor sit amet, <a href="#/" class="more">MORE</a></div>
  <a href="#/" class="more">MORE</a>
</div>

答案 4 :(得分:0)

好吧,这是一种使用伪元素的纯 CSS 的有趣解决方案。 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;
}

答案 5 :(得分:-1)

一个基于-webkit-line-clamp的纯css方法,你可以自定义......像老板这样的更多内容:

&#13;
&#13;
var container = blobClient.GetContainerReference(containerName);
var blockBlob = container.GetBlockBlobReference(fileName);
return blockBlob.DeleteIfExists();
&#13;
@-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;
    /**/
}
&#13;
&#13;
&#13;

答案 6 :(得分:-2)

只需在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>

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;}