【问题标题】:Issue with the direction: rtl CSS property方向问题:rtl CSS 属性
【发布时间】:2016-07-06 11:15:33
【问题描述】:

我有一个 HTML 元素,我需要在其中显示一个有时可能很长的文件夹/文件路径。

我还想把它放在一行上(在一个宽度受限的容器内),所以我显然需要给它添加一些省略号。

另一个要求是我应该始终看到该路径中最深的文件夹节点(这在路径很长时很有用,因为最新的节点是您真正感兴趣的)。

问题是,如果我要使用direction: rtl; CSS 属性,这很难实现,因为它会移动其他字符,例如/ 甚至括号。

看看这个例子:https://jsfiddle.net/r897duu9/1/(如你所见,我没有使用 text-overflow: ellipsis 属性,因为出于某种原因,这会覆盖 direction: rtl 属性)。

到目前为止,我已经尝试过并且它适用于现代浏览器的方法是添加 unicode-bidi: plaintext; CSS 属性,但根据 Mozilla Developer Network,这是实验性的,并且在不那么现代的情况下没有得到很好的支持 cough IE 浏览器。小提琴就在这里:https://jsfiddle.net/n05b3jgt/1/

有没有人知道更好的方法来实现这一点,这将在各种浏览器中得到很好的支持?

【问题讨论】:

  • 为什么需要拉丁语的rtl
  • 那么第二把小提琴的结果就是你想要的?我可能也会绝对定位文本,没有任何方向/bidi 的东西。 jsfiddle.net/n05b3jgt/2
  • 这是个好问题。但是,如果没有将每个单词都包含在 HTML 元素中的解决方案,我还能如何实现呢?
  • @CBroe:是的,我正在寻找类似第二小提琴的东西。你的小提琴是个好主意,但是当我的路径很短时它看起来很奇怪:jsfiddle.net/Lr2pen09
  • 也许你可以在这里使用一些 javascript 来获取行文本的最大长度,在斜杠上拆分并构建路径备份,直到路径超过最大长度并添加省略号(及其行长度计算中考虑的长度)。我会为这个效果添加一个答案,但我现在没有时间。我想这就是你想要的样子

标签: javascript html css text bidirectional


【解决方案1】:

我查看了其他解决方案,但我认为这更简单、更有效。

.title-wrapper {
  max-width: 200px;
  
  text-align: left;
  
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  
  direction: rtl;
}

.title {
  unicode-bidi: plaintext;
}
  <div class="title-wrapper">
    <span class="title">asdasd/qweqwe/xcvxcv/rtyrty/dfgdfgdfgdfgdfgd</span>
  </div>

【讨论】:

    【解决方案2】:

    您可以在容器上使用方向,然后在文本上重新设置。

    .container {
      width: 340px;
      background:gray;
      direction:rtl;
      overflow:hidden;
      text-align:left;
      position:relative;
    }
    .container:before{
      position: absolute;
      content: '...';
      background: white;
      left: 0;
    }
    
    .text-with-path {
      display:inline-block;
      white-space:nowrap;  
      text-indent:1em;
      direction:ltr;
    <div class="container">
      <div class="text-with-path">
        /Root/someFolder/SomeAnotherFolder/AgainSomeotherFolder/MyPictures/MyDocs (recent)
      </div>
    </div>
    <hr/>
    <div class="container">
      <div class="text-with-path">
       /MyPictures/MyDocs (recent)
      </div>
    </div>

    如果您的主要问题是文本溢出的方式,则仅使用浮点数

    .container {
      width: 340px;
      background:gray;
      overflow:hidden;
      position:relative;
    }
    .container:before{
      position: absolute;
      background:gray;
      content: '...';
      left: 0;
    }
    
    .text-with-path {
      float:right;
      margin-left:-999px;
      }
    <div class="container">
      <div class="text-with-path">
        /Root/someFolder/SomeAnotherFolder/AgainSomeotherFolder/MyPictures/MyDocs (recent)
      </div>
    </div>

    【讨论】:

    • 感谢您的回答。不幸的是,当你的路径很短时,这看起来很不对劲。看到这些小提琴:jsfiddle.net/acur94u3jsfiddle.net/hptydy6z
    • @Zubzob 在第一个 sn-p 上查看我的更新。对我来说看起来很奇怪的是,无论涉及到什么长度的文本,这三个点都显示出来了;)
    • @Zubzob 可能是一种仅在需要时隐藏/显示视觉点的方法codepen.io/gc-nomade/pen/JKyjZq
    • 谢谢!我尝试了一个类似于你的解决方案,现在我意识到我忘了添加 display: inline-block 到带有文本的节点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2014-01-12
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多