Achieving Consistent Aspect Ratios with Images in Flexbox Grid

I'm attempting to utilize flexbox for arranging elements in the layout shown below:

 -- ------ --
|  |      |  |
|--|      |--|
|  |      |  |
 -- ------ --

Each corner 'box' contains an image with a square aspect ratio of 1:1. The center 'box' has a ratio of 3:2.

The issue arises when the height of the corner boxes and the center box exceeds the actual height of the responsive images within them. I want to align everything perfectly.

Is there a way to ensure that the flex items line up correctly?

Here's the codepen link for reference: https://codepen.io/codybarr/pen/zNgpVK

EDIT: Adding display: flex to .container .item fixed the alignment issue in Firefox, but not in Chrome or Safari. It seems like the images are stretching weirdly...

img {
  max-width: 100%;
}

@media (min-width: 700px) {
  .main {
    display: flex;
    flex-direction: row;
    background-color: orange;
  }
  .container {
    display: flex;
    flex-direction: column;
  }
  .container.second-column {
    flex: 1 0 60%;
  }
  .container .item {
    display: flex;
  }
  .container.second-column .item {}
  .container.first-column .item:nth-child(1) {
    background-color: blue;
  }
  .container.first-column .item:nth-child(2) {
    background-color: yellow;
  }
  .container.third-column .item:nth-child(1) {
    background-color: red;
  }
  .container.third-column .item:nth-child(2) {
    background-color: green;
  }
}
<head>
  <title>Flex Testing</title>
</head>

<body>
  <h1>Flex Test!</h1>
  <div class="main">
    <div class="container first-column">
      <div class="item">
        <img src="http://i-cdn.phonearena.com/images/article/90741-image/Google-touts-advanced-recipe-search-in-mobile-app.jpg" />
      </div>
      <div class="item">
        <img src="https://lh3.googleusercontent.com/-cigJW9TQSRU/AAAAAAAAAAI/AAAAAAAAABg/Pg3e9ogcsHU/s640/photo.jpg" />
      </div>
    </div>
    <div class="container second-column">
      <div class="item">
        <img src="https://www.inetsolutions.org/wp-content/uploads/2016/01/Capital-Letters-in-URLs-Do-Not-Influence-Google-Rankings.png" />
      </div>
    </div>
    <div class="container third-column">
      <div class="item">
        <img src="http://www.sociallyawareblog.com/files/2016/12/GettyImages-610773752_small.jpg" />
      </div>
      <div class="item">
        <img src="https://s-media-cache-ak0.pinimg.com/564x/b1/e2/db/b1e2dbe6c6b547a2ebc53a49fc3ffa8c.jpg" />
      </div>
    </div>
  </div>
</body>

Answer №1

By default, a flex container is set to align-items: stretch, causing the flex items to automatically expand to fill the cross axis of the container.

To change this behavior, you can use align-items: flex-start to align the flex items to the start of the cross axis.


.container .item {
  display: flex;
  align-items: flex-start; /* override */
}

updated codepen example

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

Construct an HTML tag as a string using Jsoup

I'm faced with an element like this: <p> I am lost </p>` My task is to highlight "am" as follows; <p> I <mark style="background-color:#FFFF00;">am</mark> lost </p> This is my code: String newText = "I <mark ...

The content on my HTML webpage exceeds the width of the screen on mobile devices and certain PCs

I've exhausted most of the solutions available online, yet I haven't been able to resolve the issue. The content on my webpage is appearing wider than the screen size on mobile devices and some PCs URL: html, body,{ max-width: 100%; ...

Transitioning to mui5's sx prop instead of makeStyles is generating a typescript error - none of the overloads match this call when passing an object with

I encountered an issue while converting a component to mui 5. Here is the original code snippet: const useStyles = makeStyles({ imageContainer: { display: "flex", width: "65%", float: "left", marginRight ...

choose a distinct value for every record in the table

My goal is to only change the admin status for the selected row, as shown in the images and code snippets below. When selecting 'in-progress' for the first row, I want it to update only that row's status without affecting the others. <td ...

Sleek CSS Slider with Image Transformation on HoverThe requested task has been

I am currently working with a client on the website . One of the features on the site is a slider at the top that allows for image swapping using pure CSS. However, the client recently requested that the images change when hovering over radio buttons or au ...

Prevent the routing on page reload in AngularJS

My route provider setup looks like this: app.config(function ($routeProvider, $locationProvider){ $locationProvider.hashPrefix(''); $routeProvider .when('/', { templateUrl: 'login.html', controller: 'loginCtrl&a ...

Creating a unique custom icon by leveraging the material UI icons - a step-by-step guide

I am looking to create an arrow graphic similar to the one shown in the image below https://i.stack.imgur.com/k9TvH.png Originally, I was able to achieve this using SVG - <svg height='24' width='12' style={{ marginBottom: '-4p ...

Implementing Angular2 with conditional loading

One of the requirements for my project is to redirect users to the login page before loading the Angular2 application, without actually loading it. The project is built using angular2-quicksart. After minifying the Angular2 js file: <script> va ...

Is there a way to align the line under the hyperlinks to match the text size using CSS?

<div className={styles.container}> <ul className={styles.links}> <li className={styles.link}> <a href="/">Home</a> </li> <li className={styles.link}> <a href="/portfolio& ...

Transformation effect when hovering over an SVG polygon as it transitions between two states

const createTransitionEffect = (navLI, startCoord, endCoord) => { const changeRate = 0.1; let currentY = startCoord; const animateChange = () => { if (currentY !== endCoord) { currentY += (endCoord - startCoord) * cha ...

How can I convert the stylesheet link from fonts.google.com into a CSS class and include it in my file.css as an internal style instead of an external one?

Looking to incorporate a font from fonts.google.com? Here's how you can do it: <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@200;300 ...

Control the HTML button's state according to the information received from the server

I am currently working with datatable Jquery and using an ajax call to retrieve data from the server. Let's assume that the database consists of three attributes: "Attribute1, Attribute2, Status". Depending on the Status attribute, I need to enable or ...

HTML email for resetting your password

When a user forgets their password, I would like to send them an email with a link to a page where they can reset it. What is the best way to structure this link? Should I include the username as a parameter in the URL, and if so, how can I ensure that t ...

Experiencing issues with connecting to jQuery in an external JavaScript file

My jQuery formatting in the HTML file looks like this: <!doctype html> <html lang="en"> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script> </head> < ...

Trouble with the display:none attribute in Firefox and Chrome

<tr style="height:5px" id="TRRSHeaderTrialBar" name="TRRSHeaderTrialBar" style='display:none'> <tr id="TREmail" name="TREmail" style="height:1px;" nowrap style='display:none'> Using the code snippet above to hide the bar w ...

Unable to add a border to the thumbnail containing text

Is there a way to achieve a nice border on my thumbnail section below? I attempted using a container but it resulted in the text overflowing on md or sm screen widths. <div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-8 col-sm-offset ...

Adjust the jQuery.ScrollTo plugin to smoothly scroll an element to the middle of the page both vertically and horizontally, or towards the center as much as possible

Visit this link for more information Have you noticed that when scrolling to an element positioned to the left of your current scroll position, only half of the element is visible? It would be ideal if the entire element could be visible and even centere ...

Framework designed to be compatible with Internet Explorer 7 for seamless

Is there a CSS framework that provides full support for IE7 without any issues? Specifically, something similar to Twitter Bootstrap but with guaranteed rounded corners and properly functioning tabs. I have experienced problems with the Popover plugin sp ...

The table row disappears but the value persists

I have designed a table where users can perform calculations. However, when a row is deleted, the values from that row appear in the row below it and subsequent rows as well. What I want is for the user to be able to completely remove a row with its values ...

Tips for showcasing a drop-down menu using Jquery

I am currently utilizing jQuery to showcase a drop-down menu. It is successfully working for a single menu and displaying the appropriate drop-down. However, when I attempt to use more than one menu, it displays all of the drop-down menus simultaneously. I ...