代码之家  ›  专栏  ›  技术社区  ›  GoldenAge

当浏览器窗口大小基于纯CSS更改时,在范围中显示点

  •  1
  • GoldenAge  · 技术社区  · 6 年前

    我希望在一个跨度元素内最多显示两行文本。

    例如,浏览器窗口的完整大小:

    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    

    例如,当浏览器窗口宽度变小时:

    Lorem Ipsum is simply dummy text 
    of the printing and typesetting…
    

    如果显示文本需要两行以上,我希望向客户机显示三个点。 我试图找到一些基于纯CSS的解决方案。我试过用

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

    但它不适用于浏览器窗口的动态大小更改。我也有一个用文本区域替换跨距的想法,但在我的案例中它也不起作用。我相信这一定是微不足道的,但是经过数小时的研究,我在网上找不到任何有意义的解决方案。请记住,“span”元素是侧边栏部分的子元素。如果我对它做了一些更改,我不希望它对更改属性(如其他元素的大小)产生影响。 在不使用任何JavaScript库的情况下,是否有实现我想要的功能的选项?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Who Me Dunno Mate    6 年前

    尝试如下操作:

    span::after {
           content: "Lorem Ipsum is simply dummy text of the printing and 
           typesetting industry blah blah blah...";
           }
    
    @media screen and (max-width:600px){
          span::after {
          content: "Lorem Ipsum is simply dummy text";
          }
        }
    

    确保你设定了你的临界点!

    https://codepen.io/robbiemcmullen/pen/ajddGK

        2
  •  1
  •   GoldenAge    6 年前

    我已经创建了自己的实现 https://codepen.io/mpiwowarski/pen/qybrqY : 示例HTML代码:

    <table>
      <tr>
        <td>
      <div class="ellipsis">
      <span>
      Call me Ishmael.  Some years ago &ndash; never mind how long precisely &ndash; having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.  It is a way I have of driving off the spleen, and regulating the circulation.  
       </span>
      <div>
        </td>
        <td style="width:70%">
        </td>
      </tr>
      </table>
    

    CSS:

    .ellipsis {
        overflow: hidden;
        height: 50px;
        line-height: 25px;
         }
    .ellipsis:before {
        content: "";
        float: left;
        width: 5px; 
      height: 50px; 
         }
    .ellipsis > *:first-child {
        float: right;
        width: 100%; 
        margin-left: -5px;
        background: #AFF; }
    .ellipsis:after {
        content: "...";
        float: right;
      position: relative;
        top: -25px;
      left: 100%;
        width: 100px; 
      margin-left: -100px;
        padding-right: 5px;
        background: white; 
       }