代码之家  ›  专栏  ›  技术社区  ›  Razvan Zamfir

仅使用css,在html5元素中显示文本的最后3个字母

  •  1
  • Razvan Zamfir  · 技术社区  · 7 年前

    如果我有一个固定宽度和高度的元素,我如何只显示 最后3个字母 HTML5元素中的文本,而不是 前3个字母 是吗?

    .box {
      font-family: Arial, sans-serif;
      display: block;
      text-align: center;
      font-weight: 900;
      font-size: 72px;
      width: 125px;
      height: 125px;
      line-height: 120px;
      overflow: hidden;
    }
    
    span {
      display: block;
      white-space: nowrap;
    }
    <div class="box">
      <span>Lorem ipsum dolor sit amet</span>
    </div>

    省略号不起作用。我正在努力做到这一点 没有 javascript。

    是否只有CSS解决方案?

    3 回复  |  直到 7 年前
        1
  •  2
  •   Asons Oliver Joseph Ash    7 年前

    正在添加 float: right; span 会的。

    堆栈段

    .box {
      font-family: Arial, sans-serif;
      display: block;
      text-align: center;
      font-weight: 900;
      font-size: 72px;
      width: 125px;
      height: 125px;
      line-height: 120px;
      overflow: hidden;
    }
    
    span {
      display: block;
      white-space: nowrap;
      float: right;
    }
    <div class="box">
      <span>Lorem ipsum dolor sit amet</span>
    </div>

    display: flex; justify-content: flex-end; .box

    堆栈段

    .box {
      font-family: Arial, sans-serif;
      display: flex;
      justify-content: flex-end;
      text-align: center;
      font-weight: 900;
      font-size: 72px;
      width: 125px;
      height: 125px;
      line-height: 120px;
      overflow: hidden;
    }
    
    span {
      display: block;
      white-space: nowrap;
    }
    <DIV class=“box”>
    <SPAN>Lorem Ipsum Dolor Sit Amet</SPAN>
    </DIV>
        2
  •  1
  •   Nandita Sharma    7 年前

    使用 direction: rtl;

    .box {
      font-family: Arial, sans-serif;
      display: block;
      text-align: center;
      font-weight: 900;
      font-size: 72px;
      width: 125px;
      height: 125px;
      line-height: 120px;
      overflow: hidden;
    }
    
    span {
      display: block;
      white-space: nowrap;
     direction: rtl;
    }
    <div class="box">
      <span>Lorem ipsum dolor sit amet</span>
    </div>
        3
  •  -1
  •   Shailesh    7 年前

    试试这个

    function myFunction() {
        var str = "Lorem ipsum dolor sit amet";
        var res = str.slice(-3);
        document.getElementById("demo").innerHTML = res;
    }
    window.onload = myFunction;
    <div class="box">
      <span id="demo"></span>
    </div>