Complete the missing elements of a CSS grid layout

I'm currently working on creating a grid layout that aligns with this wireframe design:

https://i.sstatic.net/AdsMX.png

Here is the markup I have implemented so far:

<div class="grid">
  <div class="spanWidth">
    <img src="https://loremflickr.com/290/130?random=1" />
  </div>
  <div>
    <img src="https://loremflickr.com/140/130?random=2" />
  </div>
  <div>
    <img src="https://loremflickr.com/140/180?random=3" />
  </div>
  <div>
    <img src="https://loremflickr.com/140/130?random=4" />
  </div>
  <div>
    <img src="https://loremflickr.com/140/130?random=5" />
  </div>
  <div>
    <img src="https://loremflickr.com/140/180?random=6" />
  </div>
  <div>
    <img src="https://loremflickr.com/140/130?random=7" />
  </div>
</div>

Accompanied by this CSS:

.grid {
  display: grid;
  grid-gap: 10px;
}

.spanWidth {
  grid-column: 1 / span 2;
}

The issue I am encountering pertains to the images of varying sizes, as they disrupt the 'row' layout and occupy more than one row. As a result, there are empty spaces in the grid:

https://i.sstatic.net/uwDPq.png

Do you have any suggestions on how I can adjust this to match the wireframe design?

Answer №1

To ensure a perfect aspect ratio, you can specify the width and height as 100% and then adjust it using object-fit: cover. Here is an example:

.grid > div > img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Check out the demonstration here: https://jsfiddle.net/s2yqfgjh/

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

Javascript malfunctioning - exhausted all troubleshooting options

My JavaScript code consists of a single line, but I keep encountering the "getElementById null" error. document.getElementById("menu-background").style.left = "0"; HTML: <html> <head> <title></title> <link rel="style ...

Tips for creating multiple diagonal lines with CSS and HTML

Is there a way to create multiple diagonal lines within a rectangle using CSS and HTML from the start? I am interested in incorporating diagonal lines at the onset of the rectangle. Here is an example code that displays the rectangle: <div className={ ...

How to Make a Doughnut Chart Using CSS

For a while now, I've been working on filling in 72% of a circle with a hole cut out of the middle. Throughout this process, I have been consulting different resources - like this method for calculating and this one for other aspects. However, I’ve ...

Transitioning the style code from inline to the head of the document disrupts the straightforward JavaScript intended to

As I delve into the world of web development, I encountered a simple issue that has been causing me frustration for the past hour. It involves code to display the border color of a div element using an alert. The code works perfectly fine when the style is ...

What can I do to prevent an anchor link from automatically scrolling to a different section of the page?

I've been attempting to prevent a page from scrolling after selecting an anchor link. Currently, the page layout includes a larger version of an image appearing in full view on the right panel after clicking on an image in the left panel (with text u ...

Responsive Bootstrap tables nested within responsive tabs

Utilizing footable in conjunction with Boostrap Responsive Tabs leads to an issue post-resize. When the page is initially loaded on a desktop, everything functions properly until the screen is resized. Once the screen is resized, the tables within the tabs ...

Sending user input from search component to main App.js in React

I'm currently working on an app that searches a Movies database API. I have a main fetch function in App.js, and in tutorials, people are using a search bar within this main APP component. I'm wondering if it would be better to create a separate ...

Utilizing CSS to smoothly transition elements as they fill in empty space

As I have multiple blocks of content, I slide one to the side and then remove it. Once that is completed, I want the blocks below it to smoothly move up and fill the space. Check out this JSFiddle Example for reference HTML Code <div id="container"&g ...

Incorporating the fadeout technique in conjunction with the append() function

Here is some code that I am working with: $('body').on('click' , function(){ $('body').append('<div class="ajax-success"><p>Its Done !!!</p></div>').fadeOut(2000); }); The goal was to a ...

Ways to assign dynamic widths to inner divs within a parent div automatically

I am working with divs <div id='top' > <div id='top-border' > </div> <div id='top-menu' > <jdoc:include type="modules" name="top-menu" style="well" /></div> </div> and adjust ...

Styling React components using class names

Struggling with implementing a sidebar feature using the guidance provided in this example. However, I am facing difficulty in applying the necessary styles to each className... Here's what I've attempted so far, but unfortunately, no styles see ...

Click to Rotate Angular Chevron

Is it possible to animate the rotation of a chevron icon from left-facing to right-facing using Angular? CSS: .rotate-chevron { transition: .1s linear; } HTML: <button [class.button-open]="!slideOpen" [class.button-close]="slideOpe ...

How about trying out some dropdowns?

Following the issue I mentioned in my previous question titled Margin-Right on CSS not working, I am currently engaged in a creative endeavor to redesign my school's website homepage. The progress so far has been significant, but I now require some as ...

What is the difference in performance between using element.class { ... } compared to just .class { ... } in CSS?

Is there any impact on performance when specifying div.class or p.class instead of just .class if a certain class will only be used on divs or paragraphs? ...

Tips for adding jQuery UI styling to elements generated dynamically

I have a challenge with generating text fields using jquery while applying jquery ui styling to the form. The issue is that the dynamically created elements do not inherit the css styles from jquery ui: let index = 0; $('#btn_generate').on(& ...

In need of a solution for this peculiar problem with an html table and css. Let's

Encountering an unusual issue with my table content .table { position:absolute; top:12px; left:3px; width:87px; height:74px; } .table tr { vertical-align:middle; } .table td { text-align:center; padding:1px;color:#000000; font-siz ...

Exploring the Fusion of Different Styles in Material-UI Using React

I have two different styles that I use in my code. One style is specific to certain components, while the other style is global and used across various components. For example, consider the following file tree: index.tsx -App.tsx -globalConstants.ts In ...

Creating a responsive 'media object' in Bootstrap 4: Tips and tricks

I have a media object that displays an image with text next to it, and it looks good on larger screens. However, when I switch to a smaller screen, the text overflows and the images appear in different sizes and positions. <div class="container&quo ...

Is there a way to reset my div back to its initial background color when clicked a second time?

I am currently utilizing the jQuery addClass and removeClass animate feature for my project. Basically, what is happening is that when the user clicks on a particular link, a dropdown navigation will appear. This feature is essential for ensuring responsi ...

How to modify the background image of a div using CSS from a specific folder

<head id="Head1" runat="server"> <title>Title</title> <style> .parallax { /* The image used */ background-image: url(Pictures/BackUS.png); height: 100%; /* Create the parallax scrolling effect */ back ...