微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html – 避免垂直切断文本/字符

我已经限制了Bootstrap中缩略图框内文本区域的高度,但是在标题是两行或更多行的情况下,它会将文本的最后一行垂直切成两半.看这里:

http://www.bootply.com/zBXG6in5R5

由于文本框有两种字体大小(标题较大,描述较小,动态且长度不同),设置行高无济于事.溢出:隐藏不会隐藏整行文本,只会溢出溢出的部分.添加文本溢出:省略号或类似不会阻止半行显示.

我已经回顾了以前的这些帖子,但它们似乎没有提供适用于我的案例的答案:

> Can I completely hide lines of text that are half cut off?
> Sentence showing half at end of the div,overflow: hidden is cutting the sentence

我已经提到过Bootstrap,以防任何人在使用他们的缩略图类时找到解决方案,但这确实是一个更普遍的问题.有没有办法阻止切线高度发生,最好是在CSS?

谢谢!

编辑:对于那些不想要bootply链接的人,这是我的CSS:

.container {
  margin-top: 20px;
}

.thumbnail .caption h3 {
  margin-top: 8px;
  margin-bottom: 8px;
}

.thumbnail-text {
  overflow: hidden;
  height: 160px;
  margin-bottom: 10px;
}

和HTML:

    
nes Longnesciunt. Neque porro quisquam est,qui dolorem ipsum quia dolor sit amet,consectetur,adipisci velit,sed quia non numquam eius modi tempora incidunt ut labore et dolore
最佳答案
我已经写了一些非常好的jQuery来计算可见的行数,然后将其限制为该数量

$(".thumbnail-text").each(function() {
  //start with 0 height
  h=0;
  //calculate height consumed by title
  $(this).find("h3").each(function() {
    h+=$(this).outerHeight();
  });
  //calculate height left over for text
  h=160-h;
  //determine the line height of the text
  lineHeight=parseFloat($(this).find("p").first().css("line-height"));
  //determine the amount of lines that can fit in the height left for the text
  lines=Math.floor(h/lineHeight);
  //set the height of the 'p' to be the lines * lineHeight
  $(this).find("p").first().css("height",(lines*lineHeight)+"px");
});

我也改变了你的css,所以现在p元素被设置为overflow:hidden并且有margin-bottom

.thumbnail-text p {
  overflow: hidden;
  margin-bottom: 10px;
}

链接 – > http://www.bootply.com/1pZoRkUHOj

我知道这是一个特定于案例的解决方案,但解决方案背后的概念将适用于任何事情

原文地址:https://www.jb51.cc/html/426064.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐