logo
Tags down

shadow

flexbox align div to bottom


By : giedriusl
Date : May 04 2020, 08:05 AM

I'm trying to  building an e-commerce site, so I need many flexboxes to display the products. I'm also using bootstrap in my project.

This is my HTML code:

<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>

And this is how it looks like: How it currently looks like

I want "-67% off" and all the elements below it in the first flexbox to match the same elements height in the second flexbox.

 I am already using style="margin-top:auto;" in the divs that contain the offers(67% OFF NOW & 40% OFF NOW), but it doesn't align at the bottom.

And this is how I want it to look: end result

What is more, when viewing this code in mobile or by making the browser window smaller the three divs are aligned at the bottom successfully. What am I missing?

Answer :

There two ways to go about it.

  1. The general way

    Wrap the image product and title in a wrapper with min-height. Obviously, the min-height should be sufficient for the tallest of them.

  2. The specific way

    This is generally better because instead of hard-coding a min-width of all products on the page, it simply makes them adjusts to the height of the tallest in the row. You still need a wrapper around the image and product title but, instead of the min-height, you give the entire product

display:flex;
flex-direction: column;

... and give the said wrapper flex-grow: 1. This will make all products in the row grow to match the tallest and the extra height is given to the only child element with flex-grow: 1 (which is your wrapper).

Working example:

.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>



Share : facebook icon twitter icon
Related Posts Related Posts :
  • clear array after form submit
  • pass url variable to javascript
  • access data using api
  • div position fixed css
  • add horizontal line between two div
  • disable toggle button css
  • css fixed line height
  • background image not working
  • table cell border
  • text line break css
  • overlay div on div
  • text direction rtl css
  • javascript convert string to an object
  • bind inner div click to outer div
  • jquery ajax oncomplete
  • how to use promise in for loop
  • jquery get element using variable
  • javascript send textbox value
  • pass div class content to another class or id
  • css new line after element
  • css calc not working properly
  • link disabled jquery
  • sql join table to subquery
  • MS Access SQL Issue with OR AND Operators
  • set textarea value using php
  • php merge two arrays into one
  • php form with google captcha
  • update current row in php
  • php date format mysql
  • mysql pdo select query
  • php function return an array
  • PHP mysqldump database to sql file
  • display image query
  • php create array using foreach loop
  • associative array php
  • group_concat in mysql laravel
  • php array_push empty
  • mysql on update cascade not working
  • pdo insert prepared statement
  • mysql insert ignore vs on duplicate key
  • mysql insert into existing row
  • mysql multi insert query
  • mysql group by show all rows
  • php change profile image
  • combine multiple arrays into one array php
  • how to sum values in an array
  • how to get sum of the total time
  • How to Create Facebook live stream without notification using API ?
  • on button click make div visible
  • css affect another element on hover
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org