Achieve a centered content with a stretched background using Bootstrap 5

Having some trouble with aligning items in a div using align-items-stretch to stretch the background and align-items-center to vertically center the content. It seems that the alignment issue occurs when there's a very long title present.

https://i.stack.imgur.com/kfLx8.png The 'Devote' should be centered vertically along with the other card

https://i.stack.imgur.com/dQqmB.png The desired outcome is to align the images at the same height vertically

What could I be doing incorrectly?

Update: Included CSS from personal style + additional bootstrap styles Update: Adding inline min-height centers the content vertically (which works well).

<div class="col-6 grow d-flex align-items-stretch">
  <div class="col-12 tile elevation-1">
  <div class="row no-gutters align-items-center">
    <div class="col-12 col-md-4">
      <div class="col-12">
        <img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
      </div>
    </div>
    <div class="col-12 col-md-8 tile-product-content-horizontal" style="min-height:300px;">
    <div class="col-12 ">
        <span class="brand"> Brand name </span>
        <h3> Short title </h3>
    </div>
    </div>
  </div>
  </div>
</div>
  <div class="col-6 grow d-flex align-items-stretch">
    <div class="col-12 tile elevation-1">
    <div class="row no-gutters align-items-center">
      <div class="col-12 col-md-4">
        <div class="col-12">
          <img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
        </div>
      </div>
      <div class="col-12 col-md-8 tile-product-content-horizontal" style="min-height:300px;">
      <div class="col-12 ">
          <span class="brand"> Brand name </span>
          <h3> This is a very long title which stretches the div </h3>
      </div>
      </div>
    </div>
  </div>
</div>

CSS:

.tile-product-content-horizontal {
          padding: 20px 40px 20px 5px;
}
.tile {
  border-radius: 16px;
  background-color: #FFF;
  margin-bottom: 20px;
  background-image: url(assets/card-chevron.png);
  background-repeat: no-repeat;
  background-position: right 30px center;
} 

/* BOOTSTRAP SPECIFIC */
.align-items-stretch {
    align-items:stretch !important
}

.align-items-center {
    align-items:center !important
}

.d-flex {
    display:flex !important
}

Answer №1

To achieve the desired layout, you must stretch the columns vertically and align their content accordingly.


Follow these steps:

  1. Replace the inner .col-12 with .tile-product-content-horizontal
  2. Add .h-100 to the .row and remove .align-items-center
  3. Apply .d-flex and .alig-items-cener to the shortest columns
<div class="col-6 grow d-flex align-items-stretch">
  <div class="col-12 tile elevation-1">
    <div class="row no-gutters h-100">
      <div class="col-12 col-md-4">
        <div class="col-12">
          <img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
        </div>
      </div>
      <div class="col-12 col-md-8 d-flex align-items-center" >
        <div class="tile-product-content-horizontal">
          <span class="brand"> Brand name </span>
          <h3> This is a very long title which stretches the div </h3>
        </div>
      </div>
    </div>
  </div>
</div>

.tile-product-content-horizontal {
  padding: 20px 40px 20px 5px;
}

.tile {
  border-radius: 16px;
  background-color: #FFF;
  margin-bottom: 20px;
  background-image: url(assets/card-chevron.png);
  background-repeat: no-repeat;
  background-position: right 30px center;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a0805051e191e180b1a2a5f445a445a47080f1e0b58">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">


<div class="col-6 grow d-flex align-items-stretch">
  <div class="col-12 tile elevation-1">
    <div class="row no-gutters h-100">
      <div class="col-12 col-md-4">
        <div class="col-12">
          <img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
        </div>
      </div>
      <div class="col-12 col-md-8 d-flex align-items-center">
        <div class="tile-product-content-horizontal">
          <span class="brand"> Brand name </span>
          <h3> Short title </h3>
        </div>
      </div>
    </div>
  </div>
</div>
<div class="col-6 grow d-flex align-items-stretch">
  <div class="col-12 tile elevation-1">
    <div class="row no-gutters h-100">
      <div class="col-12 col-md-4">
        <div class="col-12">
          <img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
        </div>
      </div>
      <div class="col-12 col-md-8 d-flex align-items-center">
        <div class="tile-product-content-horizontal">
          <span class="brand"> Brand name </span>
          <h3> This is a very long title which stretches the div </h3>
        </div>
      </div>
    </div>
  </div>
</div>

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

Attempting to achieve a carousel animation using jQuery

Upon loading the page, only one character out of four is displayed. Two arrows are provided - one on the left and one on the right. Clicking on the left arrow causes the current character to fade out and the previous character to fade in. Clicking on the r ...

Adjust the height of the outer div automatically

Currently, I am attempting to dynamically set the height of div#container so that it adjusts based on its relative positioning and the absolute positioning of inner divs. Essentially, I want the inner divs to be centered within the container. <div id=" ...

Increasing the height of nested Bootstrap columns by stretching them out

I am trying to ensure that the first two columns in both the green and violet rows always have the same height. I initially thought of using d-flex align-items-stretch, but it seems like it is not working because those elements are not within the same row ...

Placing two parent flex containers on top of each other

I'm in a situation where I have two .container elements, both set at a height of 50%. Within these containers are the child divs .element, which belong to their respective parent divs .container. The problem arises when applying media queries with f ...

The centrally-aligned flexbox div remains at a fixed size, preventing any text truncation within its nested elements

My goal is to truncate the lengthy text in the span with the id of first-item when the screen size decreases. However, I am encountering an issue where setting margins on the parent element prevents it from shrinking, thus causing the truncation not to wor ...

Troubleshooting: Angular 2 property binding animations not functioning as expected

I'm attempting to create a fade-in animation for a div element. Here is the code snippet: import { Component, trigger, state, style, transition, animate, keyframes, group } from '@angular/core'; @Component( ...

The inclusion of float: left disrupts the formatting of the list

My webpage features a visual element known as a "plan", which consists of a small table and an image. I want these two elements to appear side by side without any styling. Currently, they are displayed one below the other. You can view how it looks like he ...

Enhance Functionality Using Bootstrap 5 Utility API

I've rearranged my utilities map merge code throughout the file in various locations, but it still isn't functioning properly. I even attempted placing the bootstrap import at the end as some have suggested, but the issue persists. Any guidance o ...

What is preventing me from setting the height of a span to 0 pixels?

It seems that when the height is set to 0px, the element doesn't visually shrink... <div id="bg"> <div id="animate"><span>WINNER ALERT! Click here to get a million dollars!!!</span></div> </div> #bg { back ...

Adding an input field and icon at the center using ::after

Having trouble centering an input field with an icon after it in a div. I can center the input text easily using text-align:center, but struggling to center the icon added using ::before. The search text is centered, but not the icon How can I center bot ...

Adjustable height within embedded object tag

I've been struggling to adjust the height of the content on my site automatically. I have attempted using iframes as well, but nothing seems to work despite trying numerous code examples from various sources including CSS and JS scripts. Any help wou ...

Solving the style upload issue with NextJS and Module Federation in ReactJS

My current project utilizes React 18 and webpack 5 with the module federation plugin. I am exploring the possibility of integrating Next JS with module federation, but I am encountering an issue where the host project is not able to fetch styles from the r ...

Solving the Issue of Assigning a Random Background Color to a Dynamically Created Button from a Selection of Colors

Trying to create my own personal website through Kirby CMS has been both challenging and rewarding. One of the features I'm working on is a navigation menu that dynamically adds buttons for new pages added to the site. What I really want is for each b ...

Customizing a ControlsFX PopOver in a JavaFX application

I am looking to personalize the appearance of a JavaFX PopOver control. I have a button that triggers the display of the PopOver. Below is a functional example: package custompopover; import javafx.application.Application; import javafx.event.ActionEvent ...

Items that share identical height and margin values will appear with divergent visual presentations

I am trying to align three horizontal lines so that they are the same height and have an equal spacing between each other. However, due to variations in height and margins, some lines appear larger or smaller than others. How can I ensure they all have th ...

How can you fix a navbar that won't stay in place?

I'm facing an issue with this example: When I scroll, the navbar moves along due to the fixed property, which affects the overall look. How can I make it disappear when scrolling, while keeping the logo intact? Can someone suggest if this layout is ...

An HTML button generated by a .js file is able to execute a .js function without any issues, but it inadvertently removes all .css styling

Currently grappling with an issue in my JavaScript self-teaching journey that I can't seem to resolve or find examples for. The problem at hand: When the HTML button calls a .js function, it successfully executes the function but also wipes out all C ...

"Capturing the essence of your online presence: The

I recently completed a website for my Students Organization, which includes a special page dedicated to recognizing the individuals who helped organize our activities. Each member is represented with a photo on this page, sourced from their Facebook profil ...

Adjusting the color of an HTML slider button as it moves

In my setup, I have a straightforward slider that I plan to use for controlling the movement of a stepper motor based on the slider's position. I wanted to give the website where this will be hosted a professional look, so I've spent quite some t ...

JavaScript-generated div not recognizing CSS in Internet Explorer

Once again, dealing with Internet Explorer has become a major headache. On headset.no, we have incorporated a small blue search field. However, when you input "jabra" into the field, it should generate suggestions in a div underneath. This feature operates ...