I am trying to create a menu system with an image and text above and below. The data is dynamic. I am wanting the menu system to appear so that each image is equal distance from each other image so that they line up in a grid of images both horizontally and vertically.
The problem is the text. If the text is longer than the image, then the div gets enlarged. However, then it creates an awkward looking gap as the images are no longer equally spaced with each other.
To work around this I think the best approach would be to have each of the other divs to adopt the size of the larger div. However, I am not sure how to go about doing this.
I have tried with flexbox using the flex-wrap property. While it is wrapping nicely, I haven't been able to find a way to get each of the images to line up equal-distance from each other.
我该如何实现这一目标?
我的代码如下:
#outer {
display: flex;
}
#main {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
border: solid black 25px;
border-radius: 25px;
width: 450px;
height: 500px;
padding: 20px;
}
.section {
background-color: #ddd;
padding: 15px;
}
.label, .icon {
text-align: center;
}Run Code Online (Sandbox Code Playgroud)
<div id="outer">
<div id="main">
<div class="section">
<div class="label">Label 1AAAAAAAAAAA</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 1B</div>
</div>
<div class="section">
<div class="label">Label 2A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 2B</div>
</div>
<div class="section">
<div class="label">Label 3A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 3B</div>
</div>
<div class="section">
<div class="label">Label 4A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 4B</div>
</div>
<div class="section">
<div class="label">Label 5A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 5B</div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
PS。我也不确定为什么我的分区div会增加其高度。因此,对此的任何见解也将不胜感激。
不确定是否可以使每个元素都增长到与最大元素一样大,但是如果您可以假设每一行都需要特定数量的元素,那么只需使它们的大小相等即可。我认为使用 css 网格而不是 flexbox 更容易做到这一点。请参阅下面的解决方案。我使用网格来定位部分元素,并确保在每个部分内所有三个部分(顶部文本、图像、底部文本)都恰好占据 div 的 1/3 - 这是为了确保所有图像也垂直对齐. 此外,我在标签中使用 flex以确保文本位于中间(水平和垂直)。
#outer {
}
#main {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
border: solid black 25px;
border-radius: 25px;
width: 450px;
height: 500px;
padding: 20px;
}
.section {
display: grid;
grid-template-columns: 100%;
grid-template-rows: 1fr 1fr 1fr;
background-color: #ddd;
padding: 5px;
margin: 5px;
min-width: 80px;
vertical-align: center;
}
.label, .icon {
display: flex;
justify-content: center;
align-items: center;
vertical-align: middle;
text-align: center;
}Run Code Online (Sandbox Code Playgroud)
<div id="outer">
<div id="main">
<div class="section">
<div class="label">Label 1AAAAAAAAAAA</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 1B</div>
</div>
<div class="section">
<div class="label">Label 2A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 2B</div>
</div>
<div class="section">
<div class="label">Label 3A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 3B</div>
</div>
<div class="section">
<div class="label">Label 4A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 4B</div>
</div>
<div class="section">
<div class="label">Label 5A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 5B</div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
您可以尝试向标签容器添加宽度和断字并相应调整宽度
#outer {
display: flex;
}
#main {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
border: solid black 25px;
border-radius: 25px;
width: 450px;
height: 500px;
padding: 20px;
}
.section {
background-color: #ddd;
padding: 15px;
}
.label {
width: 100px;
height: 100px;
overflow-y: auto;
word-break: break-all;
margin-bottom: 10px;
}
.label, .icon {
text-align: center;
}Run Code Online (Sandbox Code Playgroud)
<div id="outer">
<div id="main">
<div class="section">
<div class="label">Label 1AAAAAAAAAAA</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 1B</div>
</div>
<div class="section">
<div class="label">Label 2A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 2B</div>
</div>
<div class="section">
<div class="label">Label 3A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 3B</div>
</div>
<div class="section">
<div class="label">Label 4A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 4B</div>
</div>
<div class="section">
<div class="label">Label 5A</div>
<div class="icon"><img src="https://via.placeholder.com/75"></div>
<div class="label">Label 5B</div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)