gpt4 book ai didi

html - 对齐 flexbox 底部的三个 div

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:06 25 4
gpt4 key购买 nike

这是基本的 css,但我就是做不对。

我正在建立一个电子商务网站,所以我需要很多 flexboxes 来展示产品。我也在我的元素中使用 Bootstrap 。这是我的代码:

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row" style="display: flex; flex-wrap: wrap;">
<div class="col-sm-6 col-md-3" style="cursor:pointer; transition: border .2s ease-in-out; margin:15px; border: 1px solid #ddd; border-radius: 4px; height:auto; box-shadow: 0 2px 3px rgba(0,0,0,.075);">
<img style="vertical-align: middle;" class="img-responsive" src="https://i.dell.com/das/xa.ashx/global-site-design%20web/00000000-0000-0000-0000-000000000000/1/LargePNG?id=Dell/Product_Images/Dell_Client_Products/Notebooks/Latitude_Notebooks/12_7275/global_spi/notebook-latitude-12-7275-black-left-windows-hero-504x350.psd"
alt="...">
<div>
<h4 style="overflow-wrap: break-word; word-wrap: break-word;" class="text-center">Laptop Dell XPS 9250 12.5" (m56Y57/8GB/256GB/ HD)</h4>
</div>
<div class="text-center" style="margin-top:auto;">
<span style="color:yellow;background-color:red;font-size:18px;">-67% OFF NOW!</span>
</div>
<div>
<h5 class="text-center" style="margin-right:5px; font-size:18px;"><span style="text-decoration: line-through; color:black; font-size:22px;"><span style="color:red; font-size:20px">$2499.99</span></span> $1499.99</h5>
</div>
<div class="text-center" style="margin-top:auto;">
<button class="btn btn-success add_to_cart" style="margin-bottom:5px;">Add to cart</button>
</div>
</div>

<div class="col-sm-6 col-md-3" style="cursor:pointer; transition: border .2s ease-in-out; margin:15px; border: 1px solid #ddd; border-radius: 4px; height:auto; box-shadow: 0 2px 3px rgba(0,0,0,.075);">
<img style="vertical-align: middle;" class="img-responsive" src="https://external.webstorage.gr/Product-Images/1312102/quest-slimbook-convertible-1000-1312102.jpg" alt="...">
<div>
<h4 style="overflow-wrap: break-word; word-wrap: break-word;" class="text-center">Laptop Quest Slimbook 360 Convertible - 13.3" (Celeron N3350/4GB/32GB/HD)</h4>
</div>
<div class="text-center" style="margin-top:auto;">
<span style="color:yellow;background-color:red;font-size:18px;">-40% OFF NOW!</span>
</div>
<div>
<h5 class="text-center" style="margin-right:5px; font-size:18px;"><span style="text-decoration: line-through; color:black; font-size:22px;"><span style="color:red; font-size:20px">$349</span></span> $249</h5>
</div>
<div class="text-center">
<button class="btn btn-success add_to_cart" style="margin-bottom:5px;">Add to cart</button>
</div>
</div>
</div>

这就是它的样子: How it currently looks like

我希望第一个 flexbox 中的“-67% off”及其下方的所有元素与第二个 flexbox 中的相同元素高度相匹配。如您所见,我已经在包含优惠的 div 中使用 style="margin-top:auto;"(现在 67% OFF & 40% OFF NOW ),但它没有在底部对齐。

这就是我想要的样子: end result

此外,当在移动设备上查看此代码或通过缩小浏览器窗口时,三个 div 成功地在底部对齐。我错过了什么?

最佳答案

有两种方法。

  1. 一般方式

    min-height 将图片产品和标题包装在包装器中。显然,min-height 应该足以满足其中最高的。

  2. 具体方式

    这通常更好,因为它不是对页面上所有产品的 min-width 进行硬编码,而是让它们调整为行中最高的产品的高度。您仍然需要包装图片和产品标题,但您可以提供整个产品

    ,而不是 min-height
display:flex;
flex-direction: column;

... 并给所述包装器 flex-grow: 1。这将使行中的所有产品增长以匹配最高的产品,并且额外的高度将提供给具有 flex-grow: 1 的唯一子元素(这是您的包装器)。

工作示例:

.products {
display: flex;
flex-wrap: wrap;
}
.product {
cursor:pointer;
transition: border .2s ease-in-out;
margin:15px;
border: 1px solid #ddd;
border-radius: 4px;
height:auto;
box-shadow: 0 2px 3px rgba(0,0,0,.075);
}
.product img {
vertical-align: middle;
width: 100%;
height: auto;
}
.product h4 {
overflow-wrap: break-word;
word-wrap: break-word;
}
.product h5 {
margin-right:5px;
font-size:18px;
}
.add_to_cart {
margin-bottom:5px;
}

/* here's the magic */

.product {
display: flex;
flex-direction: column;
}
.product-body {
flex-grow: 1;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row products">
<div class="col-sm-6 col-md-3 product">
<div class="product-body">
<img style="" class="img-responsive" src="https://i.dell.com/das/xa.ashx/global-site-design%20web/00000000-0000-0000-0000-000000000000/1/LargePNG?id=Dell/Product_Images/Dell_Client_Products/Notebooks/Latitude_Notebooks/12_7275/global_spi/notebook-latitude-12-7275-black-left-windows-hero-504x350.psd"
alt="...">
<div>
<h4 class="text-center">Laptop Dell XPS 9250 12.5" (m56Y57/8GB/256GB/ HD)</h4>
</div>
</div>
<div class="text-center" style="margin-top:auto;">
<span style="color:yellow;background-color:red;font-size:18px;">-67% OFF NOW!</span>
</div>
<div>
<h5 class="text-center">
<span style="text-decoration: line-through; color:black; font-size:22px;">
<span style="color:red; font-size:20px">$2499.99</span>
</span>
$1499.99
</h5>
</div>
<div class="text-center" style="margin-top:auto;">
<button class="btn btn-success add_to_cart">Add to cart</button>
</div>
</div>

<div class="col-sm-6 col-md-3 product">
<div class="product-body">
<img class="img-responsive" src="https://external.webstorage.gr/Product-Images/1312102/quest-slimbook-convertible-1000-1312102.jpg" alt="...">
<div>
<h4 class="text-center">Laptop Quest Slimbook 360 Convertible - 13.3" (Celeron N3350/4GB/32GB/HD)</h4>
</div>
</div>
<div class="text-center" style="margin-top:auto;">
<span style="color:yellow;background-color:red;font-size:18px;">-40% OFF NOW!</span>
</div>
<div>
<h5 class="text-center">
<span style="text-decoration: line-through; color:black; font-size:22px;">
<span style="color:red; font-size:20px">$349</span>
</span>
$249
</h5>
</div>
<div class="text-center">
<button class="btn btn-success add_to_cart">Add to cart</button>
</div>
</div>
</div>
</div>

关于html - 对齐 flexbox 底部的三个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57646086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com