【发布时间】:2020-07-21 08:59:56
【问题描述】:
我正在使用 CSS flexbox 制作一个在页面调整大小时换行的项目网格。我希望网格水平居中,这要归功于justify-content: center;。问题是最后一个框(在某些情况下)居中而不是左对齐。如何使它与左侧对齐?
我尝试在容器上使用align-content: flex-start;,但它似乎不起作用,我不知道为什么。
@import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap');
@-ms-viewport {width: device-width;}
.flex-container {
max-width: 100%;
margin: 0 auto;
background-color: whitesmoke;
}
.flex-row {
display: flex;
flex-wrap: wrap;
overflow: hidden;
justify-content: center;
align-content: flex-start;
}
.flex-item {
width: 300px;
position: relative;
box-sizing: border-box;
border: dashed 1px navy;
overflow-wrap: break-word;
word-wrap: break-word;
word-break: break-word;
-webkit-hyphens: auto;
-ms-hyphens: auto;
hyphens: auto;
padding: 1em;
}
.flex-item .flex-wrapper {
height: 100%;
width: 100%;
overflow: hidden;
background-color: crimson;
}
.flex-item a.flex-permalink {
text-decoration: none;
color: black;
display: block;
background-color: lightpink;
height: 100%;
}
.flex-item img { width: 100%;}
.flex-item h1 {
font-family: 'Roboto', sans-serif;
font-size: 1.5em;
font-weight: 500;
margin: 0;
padding: 1em;
}
<div class="flex-container">
<div class="flex-row">
<!-- ------ Let the loop begin ------ -->
<div class="flex-item">
<div class="flex-wrapper">
<a class="flex-permalink" href="">
<img src="SVG_fallback_image.svg" alt="">
<h1>Exercitation cupidatat ex non aliqua dolore veniam veniam officia ex dolore.</h1>
</a>
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
<a class="flex-permalink" href="">
<img src="SVG_fallback_image.svg" alt="">
<h1>Sint eiusmod est laborum reprehenderit.</h1>
</a>
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
<a class="flex-permalink" href="">
<img src="SVG_fallback_image.svg" alt="">
<h1>Adipisicing quis tempor duis irure magna quis occaecat.</h1>
</a>
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
<a class="flex-permalink" href="">
<img src="SVG_fallback_image.svg" alt="">
<h1>Culpa dolore sint sit non voluptate nostrud nulla dolor laborum.</h1>
</a>
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
<a class="flex-permalink" href="">
<img src="SVG_fallback_image.svg" alt="">
<h1>Tempor consectetur do elit magna sunt cillum dolor.</h1>
</a>
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
<a class="flex-permalink" href="">
<img src="SVG_fallback_image.svg" alt="">
<h1>AEu eiusmod qui nostrud anim nulla dolore non veniam excepteur adipisicing sed.</h1>
</a>
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
<a class="flex-permalink" href="">
<img src="SVG_fallback_image.svg" alt="">
<h1>Lorem ipsum duis non laboris</h1>
</a>
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
<a class="flex-permalink" href="">
<img src="SVG_fallback_image.svg" alt="">
<h1>Dolor occaecat laboris enim duis eiusmod</h1>
</a>
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
<a class="flex-permalink" href="">
<img src="SVG_fallback_image.svg" alt="">
<h1>Exercitation do enim sint pariatur consectetur</h1>
</a>
</div>
</div>
<!-- ------ Let the loop end ------ -->
</div>
</div>
【问题讨论】:
-
justify-content: center;将使行中的项目居中。如果你想将它们向左对齐,你必须使用justify-content: flex-start; -
是的,我确实想将框向左对齐并将整个网格居中,但无论我将
justify-content设置为什么,它都不会改变任何东西。