如何留出足够的空间以使元素使用space-evenly和:after对齐?

huangapple go评论64阅读模式
英文:

How to leave enough space to align elements with space-evenly and :after?

问题

I use the pseudo-element :after to align at the left the last line of a list of blocks. I used space-evenly to justify these blocks. My problem is that the blocks of the last line aren't aligned with the users because of the :after that taking up the space. How can I fix that?

.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>
英文:

I use the pseudo-element :after to align at the left the last line of a list of blocks. I used space-evenly to justify these blocks. My problem is that the blocks of the last line aren't aligned with the users because of the :after that taking up the space. How can I fix that?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.exposegrid {
  width: 500px;
  display: flex;
  flex-flow: row wrap;
  justify-content: space-evenly;
  &amp;:after {
    content: &quot;&quot;;
    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;
}

<!-- language: lang-html -->

&lt;div class=&quot;exposegrid&quot;&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

.exposegrid {
   width: 500px;
   display: grid;
   grid-template-columns: repeat(auto-fill, 100px);
   gap: 10px;
}
.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 class="exposetab"></div>
</div>

Just use display: grid; it was meant for that type of layout, for example.

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

   .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);
    }

<!-- language: lang-html -->

&lt;div class=&quot;exposegrid&quot;&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;exposetab&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

just use display: grid; it was meant for that type of layout, for example

.exposegrid {
   width: 500px;
   display: grid;
   grid-template-columns: repeat(auto-fill, 100px);
   gap: 10px
}

如何留出足够的空间以使元素使用space-evenly和:after对齐?

答案2

得分: 0

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

.exposegrid {
  width: 500px;
  display: flex;
  flex-flow: wrap;
  justify-content: space-evenly;
  &::after {
    content: "";
    flex: none; /* 禁用弹性增长/收缩 */
    width: calc((100% - (100px * 4)) / 4); /* 计算边框宽度 */
  }
}

.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;
}

/* 为每一行中的最后一个项目添加 margin */
.exposegrid > *:nth-child(4n+4) {
  margin-right: 0;
}

希望这对您有所帮助。

英文:

The:after pseudo-element is occupying space in the flex container and interfering with the alignment of the items in the last row, which is the root cause of the problem you're encountering. Use of a margin on the:after pseudo-element as an alternative to flex auto is one approach to this issue. Here is your CSS in its most recent iteration:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.exposegrid {
  width: 500px;
  display: flex;
  flex-flow: line wrapping;
  justify-content: spatially even;
  &amp;:after {
    content: &quot;&quot;;
    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 &gt; *:nth-child(4n+4) {
  right margin: 0;
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月14日 00:03:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定