3

我有一些文本显示在具有固定宽度的容器中的单行上。如果文本不能包含在容器的宽度内,则会隐藏溢出。三个状态图标的任意组合都可能出现在容器右侧。如果出现这些图标中的任何一个,我希望在第一个图标出现之前隐藏文本溢出,以便文本不会出现在图标后面。

如何使文本溢出,使其不会在图标下流动(理想情况下只使用 CSS 而不需要诉诸 JavaScript)?

下面是我开始使用的 CSS 和 HTML 的一个示例(两者都没有具体设置)。这是一个代码的实时示例,它也说明了所需的结果(请注意,它有一个使用数据 URI 方案在 CSS 中内联的背景图像,因此在 Internet Explorer 8 之前的版本中不起作用)。

CSS:

.line {
  position: relative;
  width: 200px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.star, .circle, .flag {
  position: absolute;
  top: 3px;
  height: 23px;
  width: 15px;
  background-image: url(icons.png);
}

.star {
  right: 30px;
}

.circle {
  right: 15px;
  background-position: -17px 0;
}

.flag {
  right: 0;
  background-position: -32px 0;
}

HTML:

<div class="line">This is some text that should have overflow that is hidden.</div>
<div class="line">
    This is some text that should have overflow that is hidden.
    <div class="flag"></div>
</div>
<div class="line">
    This is some text that should have overflow that is hidden.
    <div class="circle"></div><div class="flag"></div>
</div>
<div class="line">
    This is some text that should have overflow that is hidden.
    <div class="star"></div><div class="circle"></div><div class="flag"></div>
</div>
<div class="line">
    This is some text that should have overflow that is hidden.
    <div class="star"></div><div class="flag"></div>
</div>
<div class="line">
    This is some text that should have overflow that is hidden.
    <div class="circle"></div>
</div>
4

5 回答 5

1

您可以直接在包含文本的 div 中使用背景图像,并使用填充为图标腾出一些空间。

编辑:

这是一个工作示例,但我无法让“nowrap”工作,所以我使用高度作弊:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style type="text/css" media="screen">
.star {
    background-image: url('data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00.%00%00%00%10%08%06%00%00%00i%C9M%EA%00%00%00tIDATH%C7%ED%96Q%0A%C0%20%0CC%7B%98%C9%EE%7F%C2y%01%91%2C%BE%0C%19%16%FCl%F3%AC5Z%F5%C7%B8%AF%F6%1CpQOYr%11%02%80%00%96X%5CpG%D4%06~%B3%E3Y%A1%95%8E!%A3%91%10ws%A3G%9D%D88v%BB)GP%C0Q%5B%A2%EC%2Cf%C1%AB%A2%04%B8%05%3FJ%A4%BA%1E%7B%F8%14%ABK%8E%9A%DDu%C59%EA%A38%FF%A4%DD%A2%03%C8d%24%84%B6%AC%BF%C0%00%00%00%00IEND%AEB%60%82');
    background-position: right;
    background-repeat: no-repeat;
    padding-right: 30px;
}

.line {
    width: 200px;
    overflow: hidden;
    height: 20px;
}
</style>
</head>
<body>
    <div class="star line">This is some text that that should have overflow that is hidden.</div>
    <div class="star line" style="padding-right: 0px;">This is some text that that should have overflow that is hidden.</div>
</body>
</html>

似乎您必须使用单个图像而不是精灵,但您可以将填充直接添加到星形、圆形和标志类。

于 2009-07-01T08:36:37.297 回答
1

也许我没有正确理解您,但是将 .star、.circle、.flag 的背景颜色简单地设置为“白色”并在它们上使用“z-index”呢?可以这么说,这当然是一个黑客,但也许对你有用。

更新

刚刚根据您的代码进行了尝试。仅适用于 FF、Opera、IE8:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
  <title>Sandbox</title>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  <style type="text/css" media="screen">
  .line{
    position: relative;
    width: 200px;
  }  
  .star,
  .circle,
  .flag{
    top: 3px;
    height: 23px;
    width: 15px;
    background-repeat:no-repeat;
    background-image: url('data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00.%00%00%00%10%08%06%00%00%00i%C9M%EA%00%00%00tIDATH%C7%ED%96Q%0A%C0%20%0CC%7B%98%C9%EE%7F%C2y%01%91%2C%BE%0C%19%16%FCl%F3%AC5Z%F5%C7%B8%AF%F6%1CpQOYr%11%02%80%00%96X%5CpG%D4%06~%B3%E3Y%A1%95%8E!%A3%91%10ws%A3G%9D%D88v%BB)GP%C0Q%5B%A2%EC%2Cf%C1%AB%A2%04%B8%05%3FJ%A4%BA%1E%7B%F8%14%ABK%8E%9A%DDu%C59%EA%A38%FF%A4%DD%A2%03%C8d%24%84%B6%AC%BF%C0%00%00%00%00IEND%AEB%60%82');
  }  
  .star{
    right: 30px;
  }  
  .circle{
    right: 15px;
    background-position: -17px 0;
  }  
  .flag{
    right: 0;
    background-position: -32px 0;
  }
  </style>
  </head>
  <body>
    <h1>Current results:</h1>     
    <div class="line" style="background-color:red;">        
        <div class="star" style="display:inline;float:right"></div>
        <div class="flag" style="display:inline;float:right"></div>
        <div style="overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">This is some text that should have overflow that is hidden.</div>
    </div>
  </body>
</html>
于 2009-07-01T08:44:23.350 回答
0

我无法评论 Merkuro 的回答和您的回答(点太少),所以我在这里添加:您可以只使用不透明 img 覆盖的线条的占位符图像,这样一旦放置,它就会覆盖文本和线条,但背景线条似乎是不间断的,因为图像有一条线条与它后面的真实线条整齐对齐。当线条改变颜色等时,你可以hover:用来切换图像(我现在能想到的最好的)

此外,如果您使用仅带有一个符号(星号、圆形、标志)的单独图像,并将图像右侧填充不透明的空白,直到您想要覆盖的宽度,您就会得到类似的东西(我尽力编码base64 图像,但它只适用于 FF。CSS 应该与正确的链接图像一起正常工作)。

CSS:

.line {
  position: relative;
  width: 200px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.star,
.circle,
.flag {
  position: absolute;
  top: 3px;
  height: 23px;
  right: 0px;
}

.star {
  width: 45px;
  background-image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC4AAAAQCAMAAABeF73YAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZ3bsYwAAABh0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjM2qefiJQAAAE5JREFUKFOd0jEOACAIA8Dy%2F0%2BLDQY3WlhcDlIJCKtQ%2Br1D844D2vhSOk%2FZNf07pxs6GKYaptFX0tALesXNMAwipfmg4sn1u5H20Tsz%2BQHT4AKfEabvTAAAAABJRU5ErkJggg%3D%3D');
  //background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD4AAAAQCAIAAADBBVtVAAAAfUlEQVR4nNVVQQrAIAyz4v%2B%2F3B0K%0AzuEYth0p6UmF1hiSKKraOKvnR4hIfkigfoBeVVnoRnkJ8cSsi9emhwQD3O9m%2FQQTJrXcrN%2Bdb%2FQj%0Aozau9R0l%2BIsgtmkc%2Bi4YcESOZP8UCT7au926Xvy9nYftKW5bIx9AbNN4OJYXccJcMFQtIo2LoIEA%0AAAAASUVORK5CYII%3D');
}

.circle {
  width: 30px;
  background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC4AAAAQCAMAAABeF73YAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAwBQTFRFAAAA%2F%2F%2F%2FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZ3bsYwAAABh0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjM2qefiJQAAAExJREFUKFO10LECACAERVH%2B%2F6dTJJtryNB06CE6KhlpPVyyumbjD%2FdfVd6N3jleFqALJzpXRdjD2LJQO48X9ASn48NNOfSX%2FeXgKpssziECqaokJE4AAAAASUVORK5CYII%3D');
  //background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB8AAAAQCAIAAABhmjO7AAAAbElEQVR4nLWSUQ7AIAhDqdn9r8w%2B%0ASJyJ2pki%2FVPio0Lh7lamVoc2s0d%2BCYBUYyQKnXM7WqSfcEPK3HdBmO%2BvbXXZEnIi%2B%2FQJIeudm9Pp%0AJ5%2FOeufpTNF%2F7bfoP1rgx1mkWrtVPZEfAlvIBTrRC7wOMBc8CAoqAAAAAElFTkSuQmCC');
}
于 2009-07-15T23:29:42.217 回答
0

您的问题是您希望根据特定类型的子级数量影响父级标记的呈现。如果没有一些程序性干预,这是不可能的。

如果您以编程方式生成这些列表,我建议只添加三个类,它们指定行的宽度到外部div

.line {
  position: relative;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.noicon {
   width: 200px
}

.oneicon {
   width: 183px
}

.twoicon {
   width: 168px
}

.threeicon {
   width: 154px
}

<div class="line noicon">This is some text that should have overflow that is hidden.</div>
<div class="line oneicon">
        This is some text that should have overflow that is hidden.
        <div class="flag"></div>
</div>
<div class="line twoicon">
        This is some text that should have overflow that is hidden.
        <div class="star"></div><div class="flag"></div>
</div>
<div class="line threeicon">
        This is some text that should have overflow that is hidden.
        <div class="star"></div><div class="circle"></div><div class="flag"></div>
</div>

否则,您最好的选择可能是使用 Javascript 来动态缩小框。

于 2009-07-01T09:24:25.783 回答
0

让这些图标影响文本宽度的最简单方法是将它们放在它之前(无论如何它们都是绝对定位的),并且也给文本本身一个容器(我很确定你需要那个,不管你怎么做)。所以你最终得到了这个:

<div class="line">This is some text that should have overflow that is hidden.</div>
<div class="line">
    <div class="flag"></div>
    <div class="text">This is some text that should have overflow that is hidden.</div>
</div>
<div class="line">
    <div class="circle"></div>
    <div class="flag"></div>
    <div class="text">This is some text that should have overflow that is hidden.</div>
</div>

这是您完成这项工作所需的唯一新 CSS(除了问题中的 CSS):

.text {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.flag   ~ .text { width: 183px; }
.circle ~ .text { width: 168px; }
.star   ~ .text { width: 154px; }

.text规则与.line div您用于自己的工作示例的规则相同。真正的“魔法”发生在最后这些规则中,这也是我将图像 div 向上移动的原因。这允许您使用CSS3 同级选择器.text根据图标的存在来设置 div 的宽度。您可以通过按从右到左的顺序列出图标类来简单地让 CSS 级联处理宽度。

作为旁注,我不得不说这里有很多 div,所以没有说明它的实际含义。您首先关心的应该始终是创建语义 HTML。

我可能想将文本放在段落标签中,将图标放在列表中,并为每个列表项提供文本描述(用 CSS 隐藏)。再说一次,也许这实际上并不合适(也许图标 div 真的应该文本之后);我说不出来。如果图标进入列表,兄弟选择器将需要一些帮助......

于 2009-07-05T02:07:34.980 回答