【问题标题】:How to align spans or text in paragraph from end to start如何从头到尾对齐段落中的跨度或文本
【发布时间】:2021-08-25 02:38:14
【问题描述】:

我想知道是否有一个简单的 CSS 解决方案来从它的末尾开始一段内容。我试过 text-aligntext-align-lasttext-方向 和其他属性可能会起作用,但到目前为止还没有找到任何运气。

这是我的代码 sn-p:

<p class="tags_holder">
  <span class="record_tag">HTML5 canvas</span>
  <span class="record_tag">Adoby Animation</span>
  <span class="record_tag">Timelines</span>
  <span class="record_tag">Flash Movie</span>
  <span class="record_tag">Event Sounds</span>
</p>

所以它包装了将最后一个跨度带到第二行的跨度。 我想要的是将其中四个放在最后一行,将包装的一个放在第一行。就像从结尾开始段落一样。

【问题讨论】:

    标签: html css styles


    【解决方案1】:

    您可以使用flexflex-direction 来实现此目的。将您的 parent 元素设置为 display: flex。然后使用flex-direction: row-reverse,这将完全反转父级的内容。在flex-wrap 属性上,您可以使用wrap-reverse。很确定这就是你要找的东西。

    .tags_holder {
      display: flex;
      justify-content: start;
      flex-direction: row-reverse;
      width: 550px;
      background: #222;
      flex-wrap: wrap-reverse;
      font-family: sans-serif;
    }
    
    .record_tag {
      margin: .5rem;
      border-radius: .5rem;
      padding: .5rem;
      background: #333;
      color: #DDD;
    }
    <p class="tags_holder">
      <span class="record_tag">HTML5 canvas</span>
      <span class="record_tag">Adoby Animation</span>
      <span class="record_tag">Timelines</span>
      <span class="record_tag">Flash Movie</span>
      <span class="record_tag">Event Sounds</span>
    </p>

    另一种方法是使用显示网格。

    .tags_holder {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 1fr;
      grid-template-rows: 1fr 1fr;
      grid-template-areas: 
        "five . . ." 
        "four three two one";
      width: 550px;
      background: #222;
      font-family: sans-serif;
    }
    
    .five {
      grid-area: five;
    }
    
    .four {
      grid-area: four;
    }
    
    .three {
      grid-area: three;
    }
    
    .two {
      grid-area: two;
    }
    
    .one {
      grid-area: one;
    }
    
    .record_tag {
      margin: .5rem;
      border-radius: .5rem;
      padding: .5rem;
      background: #333;
      color: #DDD;
    }
    <p class="tags_holder">
      <span class="record_tag one">HTML5 canvas</span>
      <span class="record_tag two">Adoby Animation</span>
      <span class="record_tag three">Timelines</span>
      <span class="record_tag four">Flash Movie</span>
      <span class="record_tag five">Event Sounds</span>
    </p>

    【讨论】:

    • 感谢您的建议。这个也试过了。它确实将行从左侧还原为写入,但不会还原行。它仍然将最后一个跨度包装到第二行,但我需要从段落末尾开始,以便最后一行有四个跨度,第一行有一个包装。
    • @AlexandrPasko 我添加了flex-wrap: wrap-reverse 并且它这样做了 --> 我需要从段落的末尾开始,所以最后一行有四个跨度和包装在第一个。
    • 很好,这正是我想要的。谢谢!
    • @AlexandrPasko 太棒了,很高兴我能帮上忙,如果这是您要找的,请随时标记为已回答且编码愉快;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多