css - 我可以把省略号放在左边,句点放在右边吗?

标签 css

将省略号 (...) 向左移动很容易:

.overflow-box{
    direction: rtl;
    text-overflow: ellipsis;
    overflow: hidden;
}

但是,如果末尾有标点符号,如句点 (.) 或问号怎么办?它也显示在左侧。我还是想看看右边的句号。有解决这个问题的简单方法吗?

演示:https://jsfiddle.net/JoelMac/Lx4L15o3/5/

最佳答案

一种方法是将 div 的内容放在 dir="ltr" 的范围内,强制内容以标准顺序呈现,同时仍根据需要向左截断。

div {
  width: 300px;
  white-space: nowrap;
  border: solid 1px #000;
  margin-bottom: 10px;
  direction: rtl;
  text-overflow: ellipsis;
  overflow: hidden;
}
<div>
  <span dir="ltr">This is a sentence that is too long to fit in the space provided.
  The ellipsis is where I want it.</span>
</div>

<div>
  <span dir="ltr">Where is the question mark?</span>
</div>

然而,这不是唯一可能的解决方案。正如@reallenramos 的回答所示,您还可以在内容末尾插入 ;那也行。
后一种方法的另一个好处是您不需要更改标记;您可以完全在 CSS 中完成,如下所示:

div {
  width: 300px;
  white-space: nowrap;
  border: solid 1px #000;
  margin-bottom: 10px;
  direction: rtl;
  text-overflow: ellipsis;
  overflow: hidden;
}

div::after {
  content:'\200E';
}
<div>
  This is a sentence that is too long to fit in the space provided.
  The ellipsis is where I want it.
</div>

<div>
  Where is the question mark?
</div>

这将是我的首选方法,但您可以选择任何您想要的方法。

关于css - 我可以把省略号放在左边,句点放在右边吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529985/

相关文章:

html - 调整表格单元格宽度

html - 在 Outlook html 消息中使用相对位置

html - 当什么都没有时,CSS 网格填充空间

表单中输入占位符字段中的 Html 仅颜色 (*) 符号

html - CSS定位

javascript - 如何将非文字添加到像 jQuery 这样的变量中?

html - 如何让我的标签从中间打开,而不是从左边打开?

javascript - 创建一个带有 Clipped overflow 的 'trapezium' 形状的 div

css - 绘制始终具有起始线和结束线的 CSS 网格线

css - 还有另一种调整 Blockly 工作区大小的方法吗?