suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Wie lässt man genügend Platz, um Elemente gleichmäßig und :after auszurichten?

Ich habe Pseudoelemente :after 在左侧对齐块列表的最后一行。我使用space-evenly来证明这些块的合理性。我的问题是最后一行的块与用户不对齐,因为 :after verwendet, um Platz einzunehmen. Wie kann ich dieses Problem lösen?

.exposegrid {
  width: 500px;
  display: flex;
  flex-flow: row wrap;
  justify-content: space-evenly;
  &:after {
    content: "";
    flex: auto;
  }
}

.exposetab {
  width: 100px;
  height: 66px;
  background-color: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(0, 0, 0, 0.4);
  border-radius: 5px;
  box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
  margin-bottom: 10px;
}
<div class="exposegrid">
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
  <div class="exposetab"></div>
</div>

P粉129168206P粉129168206439 Tage vor550

Antworte allen(2)Ich werde antworten

  • P粉099000044

    P粉0990000442023-09-13 17:13:43

    :after 伪元素占用了 Flex 容器中的空间并干扰了最后一行中项目的对齐,这是您遇到问题的根本原因。在:after 伪元素上使用边距作为 flex auto 的替代方案是解决此问题的一种方法。这是最新版本的 CSS:

    .exposegrid {
      width: 500px;
      display: flex;
      flex-flow: line wrapping;
      justify-content: spatially even;
      &:after {
        content: "";
        flex: none; /* disable flex grow/shrink */
        width: calc((100% - (100px * 4)) / 4); /* calculate border width */
      }
    }
    
    .exposetab {
      width: 100px;
      height: 66px;
      background-color: rgba(255, 255, 255, 0.2);
      border: 1px solid rgba(0, 0, 0, 0.4);
      border-radius: 5px;
      box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
      margin-bottom: 10px;
    }
    
    /* Add margin to last item in each line */
    .exposegrid > *:nth-child(4n+4) {
      right margin: 0;
    }

    Antwort
    0
  • P粉938936304

    P粉9389363042023-09-13 17:00:22

       .exposegrid {
          width: 500px;
          display: grid;
          grid-template-columns: repeat(auto-fill, 100px); /* this will create cells that will fill the space , if there is space of five, then each row will have five*/
          gap: 10px /*instedt of margin, use gap to space cells*/
        }
        .exposetab {
          width: 100px;
          height: 66px;
          background-color: rgba(255, 255, 255, 0.2);
          border: 1px solid rgba(0, 0, 0, 0.4);
          border-radius: 5px;
          box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
        }
    <div class="exposegrid">
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
        <div class="exposetab"></div>
      </div>

    Antwort
    0
  • StornierenAntwort