Creating a gallery layout using Flexbox that maintains the aspect ratio of images of varying sizes

I've been working on creating a gallery layout using flexbox similar to this design: Example of desired outcome

The goal is to have images displayed in two columns next to each other, resizing to the size of the largest image without distortion. On mobile, it should switch to one image per row.

You can view my current attempt on Codepen.

After trying different methods, I'm currently focusing on resolving the stretched image issue closest to what I want.


    <div class="wrapper">
  <header>
    MY HEADER
  </header>
  <section>
    <a href="" class= "unitie">
      <img src="http://www.landscapes.org/london/wp-content/uploads/sites/8/2015/09/roadmap-to-landscapes-finance.jpg" />
    </a>
    <a href="" class= "meow">
      <img src="https://s-media-cache-ak0.pinimg.com/originals/bd/99/3e/bd993e9921e1131fef606fcd99a03494.png" />
    </a>
</section>
  <footer>
    2016
  </footer>
</div>

CSS:

.wrapper {
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
}

section {
  display: flex;
  flex-flow: row wrap;
}

a {
  width: 48%;
  margin-left: 10px;
}

img {
  max-width: 100%;
  height: 100%;
}

I came across this technique online: Using aspect ratio for flex property However, I'm unsure how to determine the aspect ratio of an image and convert it into the flex-grow property value as demonstrated.

Any assistance would be greatly appreciated.

Answer №1

There are several things to consider here.

Firstly, it is important to ensure that all HTML tags are closed properly. Secondly, the correct syntax is class="name", not class "name".

Lastly, when trying to make an HTML inline image fit a container, you may need to remove it from the page flow and position it absolutely so that it either exceeds the height or width of its container based on the image properties.

An easy solution is to convert it into a CSS background image like this:


  <a href="" class="unitie grid--image" style="background-image:url('image.jpg')>
    </a>

.grid--image {
 background-size: cover;
}

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

- bullet point: tabulated

I need to create an unordered list with URLs included and indented, while keeping all lines justified to the left. * line one text text ted more text text text * line two text ted more text text text * line three text ted more text text text ...

Storing the CSS styling for a unique directive in its own dedicated file

Currently exploring Angular and seeking guidance on the most effective methods, as well as a general roadmap for delving deeper into the topic: In my efforts to develop a web application with Angular consisting of various independent widgets, I have opted ...

Tips for restricting text within a div container

Currently, on my to-do list website, I am facing an issue with displaying long event titles in a div on the home screen. The text overflows, and even though I have set the x-overflow to hidden, it's not providing the desired result. I have tried using ...

What could be causing my HTML form to only accept one response at a time?

While conducting my study, I required a Likert scale and came across a well-designed one at https://codepen.io/Buttonpresser/pen/poXVod However, I encountered an issue where selecting an answer for one question would deselect the answer for a different qu ...

Arrangement of elements in Bootstrap

Is there a way to prevent my frame from overflowing compared to its parent class when using bootstrap's row class? How can I address this issue? https://i.sstatic.net/gR7Ep.png Here is my code: html: <div class="container"> <div cla ...

Positioning of Responsive Slider

I'm currently working on a responsive website, but I am facing challenges with the placement of the slideshow dots. When I switch to the device toolbar, they seem to change position. I have tried various methods such as using relative and absolute uni ...

Experience the convenience of Visual Studio Code's auto-completion feature for HTML tags even when working within an

My HTML file has a babel script embedded within it. <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>React tutorial</title> <script src="https://unpkg.com/react@16/umd/react.development.js" ...

What is the best way to decrease the font size of every element in the DOM?

Is there a way to decrease the size of all DOM elements by a specific amount? Currently, I am using Bootstrap styles where h5 is set at 14px and I need it to be 12px, and h1 is at 36px when I want it to be 34px, and so forth. I have considered two option ...

What is the best way to reset an event back to its original state once it has been clicked on again

As a newcomer to web development, I'm currently working on creating my own portfolio website. One of the features I am trying to implement is triangle bullet points that can change direction when clicked - kind of like an arrow. My idea is for them to ...

Sending files to the server through HTML5

Could someone assist me with uploading a file after it has been selected using HTML5? Here's the code snippet for selecting the file: <input type="file" .... /> The input field above allows you to choose a file, but I also need help with uploa ...

Add design to the footer during a specific event using Javascript

Here is the CSS code I am working with: .footer { font-family: "lato", sans-serif; padding: 20px; line-height: 1.2; text-align: right; background: #eee; color: #0D47A1; font-size: 13px; height: 80px; position: fixed; right: 0px; botto ...

Creating a div that is positioned below an input form when it is focused can be achieved using

I am aiming to achieve the following: https://i.sstatic.net/AxIXs.png My goal is to generate a new div positioned directly below the input form that has been selected (each of the 3 input fields displayed will have a distinct div underneath). The selectio ...

Nuxt: Issue with Head function affecting title and meta tags

I need to customize the titles and meta descriptions of different pages on my Nuxt website. However, I am struggling to make it work using the head() method as indicated in the documentation. For example, in my contact.vue file: export default class Cont ...

Storing personalized HTML content in a database for individual users: A step-by-step guide

I have a JavaScript code snippet similar to this fiddle: http://jsfiddle.net/nj4N4/7/ On my webpage, it displays like this: image. When you click on the "add a year" button, a table resembling year2 appears above the previous year. Now, I'm looking ...

Angular 2: Implementing a Universal CSS Style Sheet

Is there a way to include a universal CSS file in Angular 2 applications? Currently, I have multiple components that share the same button styling, but each component has its own CSS file containing the styles. This setup makes it difficult to make changes ...

Execute various settimeout or display functions on various elements of a webpage

Just a heads up, I'm not really experienced in development. More on the newbie side of things, but eager to learn. I've been looking for an answer to my question, but so far haven't come across anything that fits my specific situation. It m ...

Removing inline elements from a Bootstrap dropdown can be achieved by customizing the dropdown

I have a query regarding the use of dropdowns in Bootstrap. When I inspect the element after applying dropdown bootstrap, I notice that some properties are added inline: position: absolute; inset: auto auto 0px 0px; margin: 0px; transform: translate(0px, - ...

Troubleshooting problems with printing HTML divs and page breaks caused by using float and position styles

Look at this class: .divDutySlip { width: 90mm; min-height: 120mm; padding: 2mm; /*float:left; position: relative; */ margin: 2mm; border: 1px solid #000000; page-break-inside: avoid; } I've modified two styles. Dis ...

Conceal a child div through the use of AJAX technology

Utilizing AJAX, I am loading pages on my website. Each page consists of two common div elements - the header div and the content div. When navigating from one page to another, I load the content div of the next page into the current page using the followin ...

Arranging rows of a specific height within a table at any location

I need to create a virtual table to browse through a very large dataset. Is there a way to position the rows (with fixed height) anywhere within the table? The issue I am facing is that the table properties automatically adjust the row height, disregarding ...