Place a lone div at the bottom of a row containing divs with varying heights

I've been working on aligning a div inside a (bootstrap v4) col within a (bootstrap v4) row that has a variable height. Here's a quick reference codepen link - https://codepen.io/ajmajma/pen/veNRdY

Here's the code snippet:

html

<div class="basket-reccomendations">
  <div class="basket-reccomendations__title col-12"><span>Know what else is great?</span></div>
  <div class="basket-reccomendations__body col-12">
    <div class="row">
      <a title="Test" class="col-6 singleReccomendation" href="">
        <div class="singleReccomendation__brand">Brand</div>
        <div class="singleReccomendation__name">Name</div>
        <div class="singleReccomendation__variant">variant</div>
        <div class="singleReccomendation__price">$10.00</div>
        <div class="singleReccomendation__addToBag"><span>Add to your bag</span></div>
      </a>
      <a title="Test" class="col-6 singleReccomendation" href="">
        <div class="singleReccomendation__brand">Brand</div>
        <div class="singleReccomendation__name">Name</div>
        <div class="singleReccomendation__variant">Charcoal</div>
        <div class="singleReccomendation__price">$10.00</div>
        <div class="singleReccomendation__addToBag"><span>Add to your bag</span></div>
      </a>
      <a title="Test" class="col-6 singleReccomendation" href="">
        <div class="singleReccomendation__brand">Brand</div>
        <div class="singleReccomendation__name">Name</div>
        <div class="singleReccomendation__price no-variant">$10.00</div>
        <div class="singleReccomendation__addToBag"><span>Add to your bag</span></div>
      </a>
      <a title="Test" class="col-6 singleReccomendation" href="">
        <div class="singleReccomendation__brand">Brand</div>
        <div class="singleReccomendation__name">Extra long name that might skew the row height - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ultricies tellus massa, quis fringilla nunc aliquet quis. Etiam non bibendum lacus. Nullam at nibh cursus, convallis tellus id, rhoncus tellus. Mauris interdum justo nec ex interdum gravida. In hac habitasse platea dictumst. Cras vitae ex ligula. </div>
        <div class="singleReccomendation__variant">variant</div>
        <div class="singleReccomendation__price">$10.00</div>
        <div class="singleReccomendation__addToBag"><span>Add to your bag</span></div>
      </a>
    </div>
  </div>
</div>

css (scss)

.basket-reccomendations {
  background: #fff;
  padding: 1rem;
  margin-top: 2.5rem;

  &__title {

  }

  &__body {

    .singleReccomendation {
      text-decoration: none;

      &__wrapper {
        max-width: 20rem;
        position: relative;
        margin: 0 auto;
      }

      &__img {
        width: 15rem;
        height: 15rem;
      }

      &__brand {

      }

      &__name {

      }

      &__variant {

      }

      &__price {
        color: #000000;
      }

      &__addToBag {
        text-transform: none;
        margin-bottom: 2rem;
      }
    }
  }
}

If you notice in the second row of divs - there is extra text affecting the height, and I'm aiming to align all the "add to bag" buttons to the bottom of the singleReccomendation div.

I attempted setting position relative on singleReccomendation and using position: absolute, bottom: 0 on the add to bag but it didn't work as expected. Using flex with bootstrap v4, any suggestions on the best approach to tackle this issue would be highly appreciated. Thank you!

Answer №1

To align the bottom of __addToBag, you can apply

display: flex; flex-direction: column;
to .singleReccomendation and then add margin-top: auto.

Check out the updated codepen here

Stack snippet

.basket-reccomendations {
      background: #fff;
      padding: 1rem;
      margin-top: 2.5rem;
    }
    .basket-reccomendations__body .singleReccomendation {
      text-decoration: none;
      display: flex;
      flex-direction: column;
    }
    .basket-reccomendations__body .singleReccomendation__wrapper {
      max-width: 20rem;
      position: relative;
      margin: 0 auto;
    }
    .basket-reccomendations__body .singleReccomendation__img {
      width: 15rem;
      height: 15rem;
    }
    .basket-reccomendations__body .singleReccomendation__price {
      color: #000000;
    }
    .basket-reccomendations__body .singleReccomendation__addToBag {
      text-transform: none;
      margin-bottom: 2rem;
      margin-top: auto;
    }
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>

<div class="basket-reccomendations">
  <div class="basket-reccomendations__title col-12"><span>Know what else is great?</span></div>
  <div class="basket-reccomendations__body col-12">
    <div class="row">
      <a title="Test" class="col-6 singleReccomendation" href="">
        <div class="singleReccomendation__brand">Brand</div>
        <div class="singleReccomendation__name">Name</div>
        <div class="singleReccomendation__variant">variant</div>
        <div class="singleReccomendation__price">$10.00</div>
        <div class="singleReccomendation__addToBag"><span>Add to your bag</span></div>
      </a>
      <a title="Test" class="col-6 singleReccomendation" href="">
        <div class="singleReccomendation__brand">Brand</div>
        <div class="singleReccomendation__name">Name</div>
        <div class="singleReccomendation__variant">Charcoal</div>
        <div class="singleReccomendation__price">$10.00</div>
        <div class="singleReccomendation__addToBag"><span>Add to your bag</span></div>
      </a>
      <a title="Test" class="col-6 singleReccomendation " href="">
        <div class="singleReccomendation__brand">Brand</div>
        <div class="singleReccomendation__name">Name</div>
        <div class="singleReccomendation__price no-variant">$10.00</div>
        <div class="singleReccomendation__addToBag"><span>Add to your bag</span></div>
      </a>
      <a title="Test" class="col-6 singleReccomendation" href="">
        <div class="singleReccomendation__brand">Brand</div>
        <div class="singleReccomendation__name">Extra long name that might skew the row height - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ultricies tellus massa, quis fringilla nunc aliquet quis. Etiam non bibendum lacus. Nullam at nibh cursus, convallis tellus id, rhoncus tellus. Mauris interdum justo nec ex interdum gravida. In hac habitasse platea dictumst. Cras vitae ex ligula. </div>
        <div class="singleReccomendation__variant">variant</div>
        <div class="singleReccomendation__price">$10.00</div>
        <div class="singleReccomendation__addToBag"><span>Add to your bag</span></div>
      </a>
    </div>
  </div>
</div>

Answer №2

To ensure proper functionality, apply position: relative to .singleRecomendation, and use position: absolute along with bottom: 0 for addToBag. Add padding as needed to avoid button overlap with text.

Check out the following CSS code snippet:

.basket-reccomendations {
  background: #fff;
  padding: 1rem;
  margin-top: 2.5rem;

  &__title {

  }

  &__body {

    .singleReccomendation {
      text-decoration: none;
      position: relative;
      padding-bottom: 40px;
      margin-bottom: 2rem;

      &__wrapper {
        max-width: 20rem;
        position: relative;
        margin: 0 auto;
      }

      &__img {
        width: 15rem;
        height: 15rem;
      }

      &__brand {

      }

      &__name {

      }

      &__variant {

      }

      &__price {
        color: #000000;
      }

      &__addToBag {
        text-transform: none;
        position: absolute;
        bottom: 0;
      }
    }
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to eliminate the dotted line from the image map in Internet Explorer 11

Below you will find the code I am working with: <img alt="Testing 2015" border="0" src="images/Test-2015.jpg" usemap="#Map" /> <p><map name="Map"><area coords="790,100,653,135" href="http://www.google.com/" shape="rect" style="cursor ...

Sub menus are not appearing in the drop-down menu

Below is the code for my dropdown button with an additional level. Initially, the button works as intended. However, upon attempting to click within the dropdown submenu, I noticed that all four items displayed the same last submenu only. .icon-cadet-l ...

`After the initial instance, the menu title will be altered.`

I have a responsive menu using Bootstrap. When the menu is collapsed, I want it to display the name of the page, and when it is open, I want it to show "Menu." Currently, I have named it "Watch the Trailer" (so let's assume that we are on that page). ...

The content within the div section is failing to align precisely in the center of the page

The headers and paragraphs inside the class "center" are not aligning to the center. I have attempted different code combinations but nothing seems to work. Here is my HTML code: It was copied from the Bootstrap website and edited for my project. [HTML c ...

What is the best way to ensure that this jQuery window has a minimum size?

Recently, I came across a helpful jQuery demo that demonstrated how to create a popup window on my website. For those interested, the demo link can be accessed here: http://jqueryui.com/dialog/#modal-message The design of the window I am aiming to replica ...

How come my ejs files are all affected by the same stylesheet?

Currently, I am in the process of developing a nodeJS application utilizing Express, MongoDB, and EJS template view engine. The server.js file has been successfully created to establish the server. In addition, a separate header.ejs file has been implement ...

Using gradients in the app bar with ReactJS and Material UI is not currently supported

Recently, I decided to create my own custom appbar for a personal project and opted to use the Erica One font. As it is still in its initial stages, there isn't much code written yet. However, I've encountered two issues related to linear and ra ...

How come my script that is dependent on the viewport does not apply classes?

I created a code to add a class to a div, but it's not working as expected. I need help troubleshooting this issue. The code works fine on codePen, but not on my WordPress website. How can I make sure the browser executes this code properly? Link to ...

Create a division that will remain visible on the screen and allow scrolling when a certain class is

Having some trouble with a fixed class div that is not continuing to scroll after the class is removed on scroll. I've attempted to fix this issue but the div keeps getting hidden instead of continuing to scroll. If anyone has any advice or can poin ...

Creating button groups with a centered .btn using Bootstrap 4

My button group has a link in the middle, so it needs to be an a tag. I could change it to a button and use JavaScript for navigation, but I'd prefer not to. Here is the code: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bo ...

Alter the color as the text moves beneath a see-through layer

Imagine a scenario where there is a page with text and on top of that, there is a transparent div that is smaller than the text area. Is it feasible to dynamically alter the color of the text that scrolls underneath the transparent div? Picture the trans ...

Instructions for developing an HTML element slider using mouse dragging

I've come across plenty of slider plugins that either only allow clicking to view the next image, or if they do support mouse drag or touch capabilities, they are limited to images. Does anyone know of a plugin or method to create a mouse drag slider ...

After positioning is a technique used to strategically place elements on

During the hours of 11:30 to 16:00, I tuned in to a video on frontend development. This period of time led me to explore different design elements and coding techniques. <div class="title-wrapper"> <h2 class="title title ...

Get rid of strange border from the input text field

I'm struggling to remove a border from an input text field on my contact form. Whenever I enter data into the Name field and then move on to the email field, there seems to be an invisible border around the input text field. I've tried tweaking ...

What is the best way to incorporate bootstrap's form-control class into a collection_select for Ruby on Rails?

Within my collection_select tag, users are able to choose a web user to assign to a ticket. The options in the collection_select are pulled from the database. Is there a way to apply the bootstrap form-control class to this collection_select tag? After a ...

The back button on the page does not reset the CSS styling

I have a button that activates navigation to a different page. When this button is clicked, it changes the current section/page's display settings to none and sets the new section/page's display to block. <div class="listLeft">&l ...

Implementing a Cancel Button in a Form Using Vue.js Bootstrap-Vue

I am trying to implement a cancel button in my code, similar to the reset button functionality, however, the method is not being triggered and the validation for required fields is still taking place. In my NewUser.vue file, I have added the @cancel="onCa ...

What could be causing my CSS styles from a HTML/CSS/JS website to not function properly on my React website?

I came across this animated coursel on Codepen that I really liked and wanted to integrate it into a basic create-react-app website. After moving everything over, including the compiled SCSS into CSS, I noticed that only one element was visible, possibly s ...

Tips for arranging HTML image sources to prepare for future updates

Incorporated into the HTML are a series of images, each accompanied by text and/or transitions. To streamline positioning, each image set along with other elements (text, video, etc...) is enclosed within a div. Furthermore, every image is labeled with a n ...

Displaying a Bootstrap card with ellipsis text-overflow following a short excerpt of text

When working on a web application using the Bootstrap library, I utilized card classes. The goal was to truncate text that was too long, as shown in the image below: https://i.sstatic.net/4wKyT.jpg However, the result I achieved looked different than exp ...