Bootstrap grid with Flexbox to create a visually appealing layout with images of varying sizes

I am facing a challenge with images of different dimensions that I need to display at the same height within a Bootstrap grid.

Despite trying various methods like using "flex 1" on child items and "flex: 1 0 0%;" on the source, I have not been successful in achieving the desired equal size display in Bootstrap.

While I believe that creating equal-sized rectangle divs might be a solution, I am also intrigued by the possibility of achieving equal height divs with flexbox, regardless of their original proportions.

Answer №1

If you have control over the image files, your most effective solution may involve manually cropping the images to the correct dimensions.

However, if you are unable to modify the source images (for example, when loading from an external service like Instagram in the demonstration), a better approach would be to specify a fixed height for your div elements and then use the images as backgrounds with the background-size: cover; property.

For more information on using background-size property in CSS, you can visit the following link.

Answer №2

In their design, there is a wrapper containing all the rows with a fixed responsive width. They use media queries to define a fixed width for each screen size and apply display:flex and flex-direction:column to stack the rows on top of each other. Each row contains three divs with flex:1 0 0% to evenly split the width. Inside each div, there is an image with defined sizes in the srcset attribute, ensuring equal heights.

The fixed width of the wrapper is divided among the children, and the height is determined by the images.

Example:

I have divided your example into two rows, each with three images. Instead of using the srcset attribute for responsive images, I have assigned a fixed height to each image for the sake of this explanation.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-direction: column;
  max-width: 935px !important;
  /* Because in here bootsrtap comes after the editor css*/
}

.container>.row {
  display: flex;
  flex-direction: row;
}

.container>.row>div {
  flex: 1 0 0%;
}

.container>.row>div img {
  max-width: 100%;
  height: 293px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">

<div class="container">

  <h1 class="my-4 text-center text-lg-left"></h1>

  <div id="" class="row text-center text-lg-left">

    <div class="col-lg-4 col-sm-6">
      <a href="" class="d-block mb-4 h-100" target="_blank">
        <img class="img-fluid img-thumbnail" src="https://www.instagram.com/p/BjiCy5uDPHp/media/?size=l" alt="">
      </a>
    </div>

    <div class="col-lg-4 col-sm-6">
      <a href="" class="d-block mb-4 h-100" target="_blank">
        <img class="img-fluid img-thumbnail" src="https://www.instagram.com/p/Bjhw_0DDAKB/media/?size=l" alt="">
      </a>
    </div>

    <div class="col-lg-4 col-sm-6">
      <a href="" class="d-block mb-4 h-100" target="_blank">
        <img class="img-fluid img-thumbnail" src="https://www.instagram.com/p/BjhXUb5DVo4/media/?size=l" alt="">
      </a>
    </div>
  </div>
  <div id="" class="row text-center text-lg-left">
    <div class="col-lg-4 col-sm-6">
      <a href="" class="d-block mb-4 h-100" target="_blank">
        <img class="img-fluid img-thumbnail" src="https://www.instagram.com/p/BjhF2fDjc6E/media/?size=l" alt="">
      </a>
    </div>

    <div class="col-lg-4 col-sm-6">
      <a href="" class="d-block mb-4 h-100" target="_blank">
        <img class="img-fluid img-thumbnail" src="https://www.instagram.com/p/Bjgz6KJDw2S/media/?size=l" alt="">
      </a>
    </div>

    <div class="col-lg-4 col-sm-6">
      <a href="" class="d-block mb-4 h-100" target="_blank">
        <img class="img-fluid img-thumbnail" src="https://www.instagram.com/p/Bjggi3mjTxf/media/?size=l" alt="">
      </a>
    </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

Modifying the CSS class of an element does not produce the desired effect after altering its styles using JavaScript

html: <input id="myinput" class="cinput" type="image" src="http://www.foodwater.org.au/images/triple-spiral-3-small-button.jpg"/> <br><br><br><br> <button id="s">set to visible class</button> <button id="h"> ...

Designing tables and adherence to WCAG 2 accessibility standards

I'm aware that WCAG 2.0 allows tables for layout purposes, but I keep encountering an error in achecker despite this: Error 245: Data table with multiple rows/columns of headers does not utilize id and headers attributes to identify cells Resolution ...

Customize your JQuery/HTML5 Slider to accept user input

Currently, I am developing a time tracking mobile application in MVC4, which includes the use of a slider input type. <input type="range" name="time_verdi" id="slider" value="7.5" min="0.0" max="24" step="0.5" data-highlight="true"/> When using thi ...

Unable to retrieve session information from a different PHP page

I have been searching for solutions here but haven't had any luck so far. I am currently learning HTML and while working on a simple login feature, I have run into some issues. I am using XML as a makeshift "database" and PHP to retrieve the informati ...

Can a list be transformed into a toolbar in a responsive design?

Here is my card with a nav section that includes multiple items (although I've only included one here for simplicity). <div class="col-md-3"> <div class="card"> <div class="card-header"> ...

A technique for adding a border to a Mat Card that mimics the outline of a Mat Form Field within a custom

I am faced with a unique design challenge where I am utilizing the MatCardComponent and each card contains a table. I would like to incorporate a floating label within the border gap similar to what is seen in MatFormField. https://i.stack.imgur.com/51LBj ...

Adjustment of Facebook button positioning in floating bar on Firefox

My floating bar currently has 5 social buttons, but I've noticed that the Facebook button is shifting about 20 pixels to the left. Could this be an issue with the CSS elements? After inspecting the elements, I found that when I remove position: absol ...

The onClick function for the "div" element is failing to append

I am currently working on a project that involves searching for strings in a database and then appending the selected string to a text box. So far, I have been successful in retrieving the search results from the database and appending them below the text ...

Unable to present the information due to a codeigniter error in retrieving the data

Encountering an issue where the data cannot be displayed and it's showing an error message of undefined variable: avg. Below is the script being used: The controller function: public function rate_config() { $rt = $_POST['hasil_rating&a ...

Arrange the footer content into multiple columns

Hello! I'm currently working on creating a footer for my website. I've written this code and everything seems to be functioning correctly, except for the fact that the second row is positioned under the first row and taking up half of the bottom ...

Difficulty with Line Breaks in Spans

I've noticed that my multi-line address in the footer is not breaking correctly at certain screen widths. Each line of the address is enclosed within a <span> tag with a specific class and then styled either as block or inline-block in the CSS ...

Ways to display an image within a div when hovering over a specific element?

I am working on recreating a specific effect found at in the section titled 'Capture and share anything quickly'. The effect involves displaying an image in the right block when hovering over the left text. I have successfully implemented this e ...

Tips on utilizing a local file as a background image URL

How can I modify my CSS to link to a local file path like C:\xampp\htdocs\Folder instead of the URL https://source.unsplash.com/1600x1050/?nature. I am currently using Bootstrap 4 and below is my CSS code: .jumbotron{ background: ...

CSS3 Marquee Effect - puzzled

After trying numerous solutions for the marquee effect, I find myself at a dead end. Even webkit examples have failed me, as the demos don't seem to work either. My browser of choice is Chrome, but my website needs to function properly in both Chrome ...

The pattern() and onkeyup() functions are unable to function simultaneously

When trying to display a certain password pattern using regex while typing in the fields, I encountered a problem. The onkeyup() function works for checking if both passwords match, but it causes the pattern info box not to appear anymore. I'm curiou ...

Issues with the dropdown menu functionality occurring on a .Net6 MVC app razor page

I recently created a dropdown menu using bootstrap. It functions perfectly when I load an HTML file in VScode. However, I encountered an issue when I tried using the same dropdown markup in a .NET6 MVC app's .cshtml file. When I click on the dropdown ...

Load Image Timing Discrepancy in Web Development (jQuery/PHP/HTML)

I am currently using jQuery to develop a basic slideshow feature. Within my home.php file, I have created a slideshow. Additionally, there is a menubar that allows users to navigate to different pages. Upon clicking on "Home" in the menu, the home.php con ...

Is there a way to adjust the font size for the label in mat-step?

Is there a way to update the design using only inline styles and CSS? Below is the code snippet I am working with: <div class="statusbar"> <mat-horizontal-stepper linear > <mat-step label="One" [complet ...

What is the best method for incorporating an Angular Component within a CSS grid layout?

I recently started learning Angular and am currently working on a project that requires the use of a CSS grid layout. However, I'm facing an issue with inserting a component inside a grid area specified by grid-area. My attempt to achieve this in app ...

Prevent the default cursor behavior in a textarea when keys are pressed with this simple guide

Here is some code I'm working with: textarea#main-text-area( rows="1" ref="textArea" maxlength="4096" v-model="message" :placeholder="placeholder" @keyu ...