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

I am attempting to achieve a smooth transition effect by fading in and out the CSS background color using JQuery and AJAX

Can anyone help me with my issue related to using Ajax to fadeIn a background color in beforeSend and fadeOut in success? I seem to have made some mistakes but can't figure out what went wrong. var data={ ...

Troubles with CSS Child Hover, Rotation, and Clipping in Pie Chart styling

The issue I am facing is with a 3-section pie chart where all sections look virtually identical. However, the :hover effect does not trigger on the first section, but it works on the other two. Each element has an equal number of overlapping parent divs, ...

Tips for submitting a form remotely using Radix Alert Dialog and the form attribute

Currently, I have integrated a Radix UI's Alert Dialog in my Next.js 13 app to confirm the deletion of a contact from the user's contact list: Take a look at the Alert Dialog modal here However, there seems to be an issue with the delete button ...

The properties of margin:auto and text-align:center are failing to function as expected

I'm facing an issue while creating a website. The 'margin: auto' and 'text-align: center' properties in my CSS code seem to not be functioning as expected. Can someone please review my code in inspect element and identify the probl ...

Unable to align text within a DIV using CSS

Recently, I started learning jQuery and building my own website. You can find my project on JSFIDDLE. The issue I'm facing is that when hovering over a new div, the text extends beyond the borders of that div instead of staying within it. I've sp ...

Leveraging the power of React in conjunction with an HTML template

After purchasing an HTML template from theme forest, I am now looking to incorporate React into it. While I find implementing Angular easy, I would like to expand my skills and learn how to use React with this template. Can anyone provide guidance on how ...

Resolve the issue of text overflow in PrimeNG Turbo Table cells

When utilizing Primeng table and enabling the scrollable feature, the table is expected to display a scrollbar while ensuring that the text within the cells does not overflow. Unfortunately, this expected behavior is not occurring. To see an example of th ...

Rotating and scaling an image simultaneously using CSS3

I am feeling very puzzled. Why am I unable to simultaneously scale and rotate? Despite my attempts, it seems to be not working: .rotate-img{ -webkit-transform:scale(2,2); -webkit-transform:rotate(90deg); margin-left:20%; margin-top:10%; } ...

Issue with loading scripts in CodeIgniter, jQuery Mobile, and jQuery UI - scripts are not loading initially, requires a refresh to

I am currently facing issues with my codeigniter application. The problem arises when I have a view/form (view 1) that, upon submission, loads another view (view 2). When view 1 is initially loaded, it does not load the correct JavaScript and CSS. Instead ...

The alignment of elements in the div seems to be slightly skewed

Currently, I am in the process of creating a website with the temporary name yeet.io. I am facing an issue where I am attempting to center both an input and h1 element inside a div vertically and horizontally, but they keep appearing misaligned for some r ...

adjust the dimensions of the clickable icon's background

Is there a way to expand the pressable area of a close icon without altering its size? For example, if the icon is 19X19 pixels, can the pressable area be increased to 39X39 pixels? The concern lies with the div element containing the close button and the ...

How can I make all fields in the CKAN Resource form html the same size as the "Description" field?

I am completely new to CKAN and front-end development. I've been scouring through various html/css files without success in locating where I can adjust the field sizes in the resources form (where users can modify metadata via GUI). I would like to r ...

How can I ensure that the image fits perfectly within the box using HTML and CSS

I'm having trouble displaying an image perfectly within my div. The image doesn't cover the entire box. Here is my code: .card-background{ background-image: url("../../../assets/imgs/back-blank.png"); border-radius: 10px; margin-top: 2%; ...

Is there a way to make a button on a single div only affect that specific div

I have a PHP query that echoes a div for each row in the table. I want the div to slide up and then, when the user clicks the "read more" button, the div slides down. However, since it is echoed in a loop, all the divs have the same IDs and classes. I wo ...

Create a static div and a separate scrolling div

I am looking to achieve a layout where the top div is fixed at the top of the screen and the second div is scrollable below it. Both divs should have the same column width. I attempted to use position: fixed, but the columns are not aligned directly benea ...

The range slider's color value is not resetting correctly when using the <input>

Before repositioning, the slider is at this location: (both thumb and color are correctly positioned) https://i.stack.imgur.com/J3EGX.png Prior to Reset: Html <input id="xyzslider" class="mdl-slider mdl-js-slider" type="range" min="0.0" max="1.0" va ...

How to customize a disabled paper-input element in Polymer 1.0?

Help with Polymer 1.0: I've encountered an issue where setting a paper-input element to 'disabled' results in very light gray text and underline, making it hard to read. I've been trying to use CSS to change the text color but haven&ap ...

On smaller screens in portrait mode, CSS automatically incorporates margin adjustments

Here is the code I am working with: <html> <head> <style> body { margin: auto; width: 720px; } </style> </head> <body> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ip ...

Issue with displaying the Bootstrap paginator stylingSomething seems to be

Currently, I am working on a PHP project that involves creating a table with Bootstrap styling. To achieve this, I referred to a tutorial which guided me through the process. After linking the necessary Bootstrap CSS file using the provided code snippet, ...

Adjust the space between spans by utilizing the margin

There are two spans (although there could be many more) on this web page that I need to adjust the distance between. I tried using the margin-bottom attribute, but it doesn't seem to have any effect. The spans remain in their original positions, which ...