【问题标题】:How to hide separator when at newline?如何在换行时隐藏分隔符?
【发布时间】:2016-12-11 06:33:24
【问题描述】:

我认为这是最容易用一个例子来解释的。

假设我在这样的网页上有一个列表:

word one, word two, word three, word four, word five, word six

如果用户的屏幕分辨率很小,列表可能会像这样结束:

word one, word two, word three, word four, word five, 
word six

如您所见,第一行末尾有一个逗号。我想改变它,所以如果发生这样的事情,逗号是隐藏的。这意味着它看起来像这样:

word one, word two, word three, word four, word five
word six

有没有办法用 CSS 或 Javascript 做到这一点?

感谢您的帮助。

【问题讨论】:

    标签: javascript css


    【解决方案1】:

    是的。让它们以负边距溢出,并隐藏溢出。

    div {
      border: 1px solid;
      overflow: hidden;
      animation: size 5s linear infinite alternate;
    }
    span {
      display: inline-block;
      margin-right: 10px;
    }
    span::before {
      content: ',';
      display: inline-block;
      width: 10px;
      margin-left: -10px;
    }
    @keyframes size {
      from { width: 750px; }
      to { width: 0; }
    }
    <div>
      <span>word one</span>
      <span>word two</span>
      <span>word three</span>
      <span>word four</span>
      <span>word five</span>
      <span>word six</span>
    </div>

    【讨论】:

    • +1 您可以通过在span::before 上应用负左边距来修复每个单词和连续逗号之间的空格。
    • @gyre 是的,但确切的保证金金额可能会有所不同。这是How to remove the space between inline-block elements? 问题。
    • 有趣,但你必须使用边框来隐藏第一个逗号。感谢您的努力。
    • @TomBrock 可以去掉边框,隐藏逗号的是overflow: hidden
    • @Oriol 如果删除边框,每个单词(包括第一个单词)之前都会出现一个逗号。奇怪!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    相关资源
    最近更新 更多