包含“更多”链接的CSS文本省略号

gudnpqoy  于 2023-03-05  发布在  其他
关注(0)|答案(7)|浏览(139)

所以我有下面的Fiddle,它将文本中的省略号设置为两行。然后我想在文本中内联一个“More”链接。
http://jsfiddle.net/csYjC/2876/
因此,如果文本超过两行,则应如下所示:

是的。但是:

这是不正确的(应该与文本内联)。
代码如下:

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

我想一定很容易...先谢谢你。

njthzxwz

njthzxwz1#

.text内部的div仍显示为块元素。使用此选项可修复:

.text > div { display: inline; }

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

frebpwbc

frebpwbc2#

<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>
    • 中央支助组**
.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
}
bfnvny8b

bfnvny8b3#

一个基于-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>
wlp8pajw

wlp8pajw4#

好吧,使用flexbox使它变得简单。试试这个吧

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

https://codepen.io/baagii95/pen/byeNqZ
顺便说一句,我用了sass。如果你想在css版本试试这个。

.text {
  display: flex;
}

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

h7appiyu5#

这里有一个有趣的解决方案,它使用纯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;
}
js81xvg6

js81xvg66#

我用canvas得到了div中内容所占字符串的宽度(因为字体不同,度量会有细微的差别),这里我用string的宽度(即content.textContent)除以div内容的宽度来计算内容被划分的行数
当它大于或等于3时,锚标记的显示属性将更改为块,当它小于3时,将恢复为内联。

输出:

∮这会成功∮

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

lzfw57am7#

只需向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;}

相关问题