所以我正在使用 Flexbox 制作一个电子商务页面,一切看起来都很好,甚至是我的父 Flex 容器 .catalog 的子级,但是当我需要在我的子级 .cards 上设置 justify-content: space- Between 时,却没有工作,但证明中心,顶部,底部工作,我不知道
这里是我的html代码
<div class="catalog">
<a class="cards" href="/products/1">
<div>
<h1>Nvidia GTX 1080 ti Founders Edition</h1>
<img src="https://asset.msi.com/resize/image/global/product/product_7_20171122181017_5a154d0982c95.png62405b38c58fe0f07fcef2367d8a9ba1/1024.png" alt=""/>
<h2>precio: $120.000</h2>
</div>
</a>
<a class="cards" href="/products/2">
<div>
<h1>Ryzen 5 5600x</h1>
<img src="/pngs/ryzen5.png" alt="" />
<h2>precio: $85.961</h2>
</div>
</a>
<a class="cards" href="/products/3">
<div>
<h1>Redragon Cobra Chroma M711 White</h1>
<img src="/pngs/redragon.webp" alt="" />
<h2>precio: $20.000</h2>
</div>
</a>
</div>
这是我的CSS代码
.cards {
width: 280px;
padding: 10px;
text-align: center;
background: #101010;
font-size: 10px;
display: flex;
align-items: center;
flex-wrap: nowrap;
flex-direction: column;
justify-content: space-between;
}
.cards img{
max-width: 200px;
}
.cards:hover{
transform: scale(1.03);
transition: 0.04s;
box-shadow: 0px 0px 36px -25px rgba(255,255,255,1);
}
.catalog {
display: flex;
float: inline-end;
flex-direction: row;
flex-wrap: wrap;
gap: 15px;
width: calc(100% - 280px);
justify-content: center;
}
我认为是因为你的CSS“justify-content: space- Between;”必须位于“catalog”类而不是“cards”类中。您必须将 justify-content 变量放置在父元素中才能影响其子元素。不知道你的问题解决了没有。
<style>
.cards {
width: 280px;
padding: 10px;
text-align: center;
background: #101010;
font-size: 10px;
display: flex;
align-items: center;
flex-wrap: nowrap;
flex-direction: column;
/*justify-content: space-between;*/
}
.cards img{
max-width: 200px;
}
.cards:hover{
transform: scale(1.03);
transition: 0.04s;
box-shadow: 0px 0px 36px -25px rgba(255,255,255,1);
}
.catalog {
display: flex;
float: inline-end;
flex-direction: row;
flex-wrap: wrap;
gap: 15px;
width: calc(100% - 280px);
justify-content: space-between;
}
</style>
<div class="catalog">
<a class="cards" href="/products/1">
<div>
<h1>Nvidia GTX 1080 ti Founders Edition</h1>
<img src="" alt=""/>
<h2>precio: $120.000</h2>
</div>
</a>
<a class="cards" href="/products/2">
<div>
<h1>Ryzen 5 5600x</h1>
<img src="" alt="" />
<h2>precio: $85.961</h2>
</div>
</a>
<a class="cards" href="/products/3">
<div>
<h1>Redragon Cobra Chroma M711 White</h1>
<img src="" alt="" />
<h2>precio: $20.000</h2>
</div>
</a>
</div>