简体   繁体   English

CSS在流体图像顶部截断文本

[英]CSS Truncate Text on top of Fluid Image

I'm absolutely positioning text on top of images and would like to truncate the text if it expands over the image. 我绝对将文本放置在图像顶部,如果文本在图像上扩展,则希望截断文本。 This wouldn't be a problem if the width of the text was defined. 如果定义了文本的宽度,这将不是问题。 However, the text block over the image is set to 100% and is positioned to the bottom of the image. 但是,图像上方的文本块设置为100%,并位于图像的底部。

How do I hide the text if it expands pass the image? 如果扩展通过图像,如何隐藏文本?

Check out my example: http://jsfiddle.net/qhFUL/ 看看我的示例: http : //jsfiddle.net/qhFUL/

Set overflow to hidden : overflow设置为hidden

.box {
  overflow: hidden;
}

Demo: http://jsfiddle.net/qhFUL/1/ 演示: http//jsfiddle.net/qhFUL/1/

If you want to prevent the text from wrapping as well (so it doesn't hide the image) you can do this: 如果您还想防止文本换行(因此不会隐藏图像),则可以执行以下操作:

  .box {
    position: relative;
    margin-bottom: 20px;
    overflow: hidden;
  }

  .text {
    position: absolute;
    bottom: 0;
    left: 0;
    padding: 20px;
    white-space: nowrap;
  }

Which will truncate long text instead of wrapping. 这将截断长文本而不是自动换行。

You could also specify a height on the .text div so it will truncate and not wrap. 您还可以在.text div上指定一个高度,以便截断而不换行。

You could then add a little jQuery ellipsis script to pretty it up a bit... 然后,您可以添加一些jQuery省略号脚本以使其更加美观...

http://yue.st/notes/code/js/ellipsis.en.html http://yue.st/notes/code/js/ellipsis.en.html

or 要么

https://github.com/rmorse/AutoEllipsis https://github.com/rmorse/AutoEllipsis

nice... :) 很好... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM