如何在div中将文本向右对齐,以使它的开头被overflow:hidden删减? [英] How can I align text to the right in a div so its beginning is cut down with overflow:hidden?

查看:144
本文介绍了如何在div中将文本向右对齐,以使它的开头被overflow:hidden删减?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div style="width:100px; overflow:hidden; text-align:right;" id="pathdiv">

<script>
    document.getElementById("pathdiv").innerHTML="long/path/to/file"
</script>

我的目标是显示一条较长的绝对路径,该绝对路径在一个相对狭窄的div中对齐,以减少其开始的方式(以便显示路径的有趣部分).上面的代码在div合适的情况下可以使文本右对齐,在不合适的情况下可以将其剪切,但是不幸的是,它剪切的是末尾,而不是开始.

My goal is to show a long absolute path right aligned in a relatively narrow div, in a way that its beginning is cut down ( so that the interesting part of the path is shown ). The above code makes the text right aligned if it fits in the div, cuts it if it does not fit, but unfortunately it cuts the end of it, not the beginning.

如果可以手动修剪字符串,我可以对其进行修剪,但是我必须以某种方式计算出适合它的字符数(不清楚).

I could trim the string if it is too long manually, but then I have to calculate somehow how many characters make it fit ( unclear ).

他们是实现我的目标的任何简单方法(使用CSS还是其他方法)?

Is their any straightforward way to achieve my goal ( either with CSS or otherwise )?

推荐答案

<div style="width:100px; overflow:hidden; text-align:right;text-overflow:ellipsis; direction:rtl" id="pathdiv">

<script>
    document.getElementById("pathdiv").innerHTML="long/path/to/file"
</script>

添加路线(rtl)即可.

Adding direction(rtl) would work.

这篇关于如何在div中将文本向右对齐,以使它的开头被overflow:hidden删减?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆