Responsive image gallery using css grid

I am facing a challenge creating a responsive CSS grid image gallery with 18 divs inside a container in my HTML code. Each div has its own unique class. My first issue arises when I attempt to span the first image horizontally using a class called .vertical. Although I've applied "grid-column: span 2;" in my CSS, the image expands both vertically and horizontally instead of just horizontally. Moving on to the second problem, when I implement "grid-row:span 2;" for the .vertical class image, the lower image shifts up but the vertical spanning doesn't occur as intended. Below is my code snippet. Any advice or assistance would be greatly appreciated.

img{
width:100%;
height:auto;
}

 .container{
   display: grid;
   grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
   grid-gap: 5px;
   
  
 }

 
 .horizontal{
 grid-column:span 2;

}
.vertical{
grid-row:span 2;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>

  <div class="container">
    <div class="horizontal"><img src="christian.jpg"></div>
     <div class="vertical"><img src="1.jpg"></div>
      <div class="tva"><img src="2.jpg"></div>
       <div class="tre"><img src="3.jpg"></div>
       <div class="fyra"><img src="4.jpg"></div>
       <div class="fem"><img src="5.jpg"></div>
       <div class="sex"><img src="6.jpg"></div>
       <div class="sju"><img src="7.jpg"></div>
       <div class="vertical"><img src="christian.jpg"></div>
       <div class="ett"><img src="1.jpg"></div>
       <div class="tva"><img src="2.jpg"></div>
       <div class="tre"><img src="3.jpg"></div>
       <div class="fyra"><img src="4.jpg"></div>
       <div class="fem"><img src="5.jpg"></div>
       <div class="sex"><img src="6.jpg"></div>
       <div class="sju"><img src="7.jpg"></div>
       <div class="sex"><img src="6.jpg"></div>
       <div class="sju"><img src="7.jpg"></div>
       
     </div>

</body>
</html>

link to image

Answer №1

The primary issue I encountered in the provided code is the utilization of the width: 100% property for the img elements. To address this, I recommend adjusting it to max-width, as this will prevent the images from extending beyond the designated columns/rows. Additionally, I incorporated object-fit: cover to the img styling to ensure they appropriately fill the available space.

img{
    object-fit: cover;
    max-width: 100%;
    height: auto;
}

.container{
   display: grid;
   grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
   grid-gap: 5px;
}

.horizontal{
   grid-column: span 2;
}
.vertical{
   grid-row: span 2;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>

  <div class="container">
    <div class="horizontal"><img src="http://placekitten.com/1000/500"></div>
     <div class="vertical"><img src="http://placekitten.com/500/1000"></div>
      <div class="tva"><img src="http://placekitten.com/400/400"></div>
       <div class="tre"><img src="http://placekitten.com/400/400"></div>
       <div class="fyra"><img src="http://placekitten.com/400/400"></div>
       <div class="fem"><img src="http://placekitten.com/400/400"></div>
       <div class="sex"><img src="http://placekitten.com/400/400"></div>
       <div class="sju"><img src="http://placekitten.com/400/400"></div>
       <div class="vertical"><img src="http://placekitten.com/500/1000"></div>
       <div class="ett"><img src="http://placekitten.com/400/400"></div>
       <div class="tva"><img src="http://placekitten.com/400/400"></div>
       <div class="tre"><img src="http://placekitten.com/400/400"></div>
       <div class="fyra"><img src="http://placekitten.com/400/400"></div>
       <div class="fem"><img src="http://placekitten.com/400/400"></div>
       <div class="sex"><img src="http://placekitten.com/400/400"></div>
       <div class="sju"><img src="http://placekitten.com/400/400"></div>
       <div class="sex"><img src="http://placekitten.com/400/400"></div>
       <div class="sju"><img src="http://placekitten.com/400/400"></div>
       
     </div>

</body>
</html>

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

Maintaining an onExit function in the ui-router framework

I am trying to implement an animation on an element within a page when the user is transitioning out of a state. Here is my current code snippet: { ....., views: { ... }, onExit: function(){ someEle.classList.remove("someClass"); // ...

how to dynamically add classes in jsx with a combination of tailwind and regular classes

Why is it that in Nextjs 14 (ReactJS 18), when I conditionally assign css classes within a jsx code like this: <Swiper className={lightbox ? "swiperLightbox h-[80%] md:h-[70vh] z-50" : "swiperMain h-[21vmax] rounded-xl"} > ...

Leveraging Powershell to access and retrieve data from a local .htm file using XPath syntax

Recently, I started working with Powershell and attempting to utilize it to open an existing .htm file and extract certain values through XPath for further use. Here is the current status of my script: $localHtmlFile = 'myfile.htm' $html = New- ...

Displaying the result of a JavaScript function in a textarea

I've been working on getting this function to output to the textarea below. The current code functions correctly, but it displays the output on the whole page instead of in a text field. I know that the connection between the two is not properly estab ...

Expanding the full width of the bootstrap navbar

Let's cut to the chase - I'm working with Bootstrap 4 and trying to make my navbar fill the entire width (excluding margins). I know this question has been asked before, so I checked out a post here which suggested using the .nav-fill class, but ...

Error: Invalid Argument - The argument 'PanelController' is expected to be a function, but it is undefined

Here is a snippet of my HTML code: <body ng-controller="StoreController as store"> ........... <section ng-controller="PanelController as panel"> <ul class="nav nav-pills""> <li ng-class="{active:panel.isSe ...

Displaying the "Ad Blocker Detected" image next to the advertisement

I am attempting to create a feature where if adblock is enabled, an image will display behind the ad banner on my website with the message "Please consider disabling adblock". Unfortunately, it is only showing up as a bordered box. Here is how the box ap ...

Horizontal alignment of Bootstrap columns is not working as expected

While attempting to integrate Bootstrap elements into my portfolio, I encountered issues with aligning columns. Currently, the only columns I have added are in the "welcome-section" (Lines 40-52), but they refuse to align horizontally. I've tried tro ...

What steps should I take to modify the logo in my header?

<div class="main-header" style="background: #484747"> <div class="container"> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12 logo-holder"> <div class="logo"> <a href="in ...

implementing jquery for dynamic pop up placement

When I click the English button, a pop-up appears, but I can only see the full contents of the pop-up after scrolling down. Is there any way to view the entire pop-up without scrolling? Below is the code that I am using: http://jsfiddle.net/6QXGG/130/ Or ...

Eliminating harmful elements in HTML code

I am looking to showcase an HTML-formatted email on a website. I am aware that HTML can contain malicious elements that should be removed before displaying it to users. This HTML email is received from an unidentified source, possibly created by a malici ...

Use sed to extract integers from HTML based on various criteria

Before we continue, I must confess: I realize using regex to parse HTML is generally frowned upon, but if Chuck Norris can do it, why can't I? ;) My goal is to extract specific data from this HTML page: http://pastebin.com/unAifctF based on thre ...

Guide on setting the BackColor of a table cell based on a database value dynamically

In the project I am currently working on, there is a request to change the background color of specific table cells based on their values. In order to achieve this, I am handling the RadGrid_ItemDataBound event and attempting to set the BackColor property ...

Mastering the art of customizing styles in Material UI v5 versus v4

When I first started using Material UI v4, I relied on makeStyles and withStyles from @mui/styles to customize the default styles of components in my application. It was a simple and effective approach to achieve the desired look and feel. However, with th ...

Despite being positioned absolutely, they are able to make z-index work effectively

Currently, I am facing an issue with two elements in my design. The first element consists of dotted circles that should have a z-index of -999 so they remain in the background entirely. The second element is a login form that needs to have a z-index of 99 ...

Positioning Multi-level Drop Down Div in Javascript - How to do it efficiently?

I'm currently working on a horizontal menu using CSS and JavaScript with multiple levels. I've implemented a toggle function to show the submenu container, but it's causing the links below it to be pushed down. Is there a way to make the dis ...

Need to ensure that an AJAX form doesn't resubmit without the need for a page

Encountering a peculiar mystery issue here... I have created a button on a webpage that triggers a form submission. The form is enclosed within a DIV, which dynamically updates to display the modified data. (this is embedded in a smarty template) form: ...

Decrease the font size of text using intervals in CSS

I'm facing a challenge where I need to decrease the font size by 10% every 2 seconds. <div class="shrink"> text </div> .shrink{ transition: 2s linear all; font-size : 20px; } Is there a way to achieve this using only CSS? ...

Seeking help with a Javascript regex inquiry

I am currently utilizing JavaScript regex for the following task: I have gathered the HTML content from a page and stored it within a string. Now, I aim to identify all URLs present on this page. For instance, if the document includes-- <script src = ...

When the image is taller than wider, the background will cover the webpage

How can I effectively handle images with a much larger height than width when trying to cover the background? The image in question is 1026x2258 (width x height) and looks terrible. Any suggestions for managing this situation? There isn't much room ...