简体   繁体   English

具有绝对 position 的嵌套元素上的文本溢出省略号

[英]Text-overflow ellipsis on a nested element with absolute position

I'm trying to find a way to have text ellipsis at the point of contact with the righthand side of the green container.我正在尝试找到一种在与绿色容器右侧的接触点使用文本省略号的方法。 My impression at this time is that this might actually be impossible to do in CSS when the text box is part of an absolutely position wrapper.我此时的印象是,当文本框绝对是 position 包装器的一部分时,这实际上可能在 CSS 中不可能做到。

Is there a way of doing this with CSS only?有没有办法只用 CSS 做到这一点? Is this only doable via javascript to dynamically calculate the width of the text box to create the illusion that it is bound by the size of the green container?这是否只能通过 javascript 来动态计算文本框的宽度以创建它受绿色容器大小约束的错觉?

The solution cannot involve removing the absolute positioning.该解决方案不能涉及删除绝对定位。 This is a simple extrapolation of a more complicated UX.这是对更复杂的 UX 的简单推断。

在此处输入图像描述

<div class="container">
  <div class="relative">
    <div class="absolute">
      <div class="text">
        Lorem ipsum dolor sit amet, 
        consectetur adipiscing elit, 
        sed do eiusmod
      </div>
    </div>
  </div>
</div>
* { background-color: white; }

.container {
  border: 3px solid lightgreen;
  background: lightgray;
  width: 10%;
}

.relative {
  position: relative;
  height: 100px;
  border: 2px black solid;
  width: 30%;
}

.absolute {
  border: 2px solid blue;
  position: absolute;
  bottom: 10px;
  left: 10px;
}

.text {
  border: 2px solid red;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

You could set the absolute container to go to the right of 0. And set the position relative to the parent container which you want it to take as its relative reference, which, as you say, is the green one.您可以将绝对容器设置为 go 到 0 的右侧。并设置 position 相对于您希望它作为其相对参考的父容器,正如您所说,它是绿色的。

 * { background-color: white; box-sizing: border-box;}.container { position: relative; border: 3px solid lightgreen; background: lightgray; width: 10%; }.relative { height: 100px; border: 2px black solid; width: 30%; }.absolute { border: 2px solid blue; position: absolute; bottom: 10px; left: 10px; right: 0; }.text { border: 2px solid red; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
 <div class="container"> <div class="relative"> <div class="absolute"> <div class="text"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod </div> </div> </div> </div>

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

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