英文:
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;
&: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;
}
<!-- language: lang-html -->
<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>
<!-- 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 -->
<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>
<!-- 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
}
答案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;
&: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;
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论