Tips for ensuring that background images with absolute positioning are responsive

I'm facing a challenge with arranging five images in a grid-like layout. To give you a visual idea, here's what I have in mind:

https://i.sstatic.net/sl1B6.jpg

From my understanding, achieving such a design would require positioning the items absolutely. However, the issue arises when the screen is resized in either height or width, as the images may overlap or the spacing between them may increase.

For instance, this is how the gap looks on an extra-large screen:

https://i.sstatic.net/vsBOa.jpg

The demo I've put together demonstrates my current "static" / hardcoded method. While it works to some extent, I believe there might be better alternatives to achieve this design.

If you have any suggestions or guidance, I would greatly appreciate it.

.imageGrid {
  overflow: hidden;
  padding: 120px 0 814px 0;
  position: relative;
}
.imageGrid__images-img {
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  position: absolute;
}
.imageGrid__images-img-1 {
  width: 280px;
  height: 320px;
  top: 485px;
  left: 0;
}
.imageGrid__images-img-2 {
  width: 427px;
  height: 319px;
  top: 485px;
  left: 310px;
}
.imageGrid__images-img-3 {
  width: 737px;
  height: 342px;
  left: 0;
  bottom: 30px;
}
.imageGrid__images-img-4 {
  width: 500px;
  height: 527px;
  right: 0;
  top: 119px;
}
.imageGrid__images-img-5 {
  width: 600px;
  height: 500px;
  right: 0;
  top: calc(500px + 119px + 30px);
}
<section class="imageGrid">
  
 
    <div class="imageGrid__images">
      <div class="imageGrid__images-img imageGrid__images-img-1" loading="lazy" style="background-image: url('https://i.imgur.com/k7fNrV9.png');"></div>
      <div class="imageGrid__images-img imageGrid__images-img-2" loading="lazy" style="background-image: url('https://i.imgur.com/knI2LcY.png');"></div>
      <div class="imageGrid__images-img imageGrid__images-img-3" loading="lazy" style="background-image: url('https://i.imgur.com/Hyn2v1J.png');"></div>
      <div class="imageGrid__images-img imageGrid__images-img-4" loading="lazy" style="background-image: url('https://i.imgur.com/oZG4m8k.png');"></div>
      <div class="imageGrid__images-img imageGrid__images-img-5" loading="lazy" style="background-image: url('https://i.imgur.com/dOvSc80.png');"></div>
  </div>
  
</section>

Answer №1

Using display grid and defining various areas with grid-template-areas creates a responsive layout that adapts to different viewport sizes.

This code snippet provides the basic idea, but for customized layouts on narrower viewports, adjust the areas in a media query.

Check out the CSS code below:

.imageGrid__images {
  width: 100vw;
  display: grid;
  gap: 2vw;
  aspect-ratio: 1 / 1;
  grid-template-columns: 1fr 2fr 3fr;
  grid-template-rows: 2fr 1fr 1fr 2fr;
  grid-template-areas: ". . D"
    "A B D"
    "A B E"
    "C C E";
}
.imageGrid__images-img {
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  width: 100%;
  height: 100%;
  background-color: blue;
}
.imageGrid__images-img-1 {
  grid-area: A;
}
.imageGrid__images-img-2 {
  grid-area: B;
}
.imageGrid__images-img-3 {
  grid-area: C;
}
.imageGrid__images-img-4 {
  grid-area: D;
}
.imageGrid__images-img-5 {
  grid-area: E;
}
<section class="imageGrid">
  
 
    <div class="imageGrid__images">
      <div class="imageGrid__images-img imageGrid__images-img-1" loading="lazy" style="background-image: url('https://i.imgur.com/k7fNrV9.png');"></div>
      <div class="imageGrid__images-img imageGrid__images-img-2" loading="lazy" style="background-image: url('https://i.imgur.com/knI2LcY.png');"></div>
      <div class="imageGrid__images-img imageGrid__images-img-3" loading="lazy" style="background-image: url('https://i.imgur.com/Hyn2v1J.png');"></div>
      <div class="imageGrid__images-img imageGrid__images-img-4" loading="lazy" style="background-image: url('https://i.imgur.com/oZG4m8k.png');"></div>
      <div class="imageGrid__images-img imageGrid__images-img-5" loading="lazy" style="background-image: url('https://i.imgur.com/dOvSc80.png');"></div>
  </div>
  
</section>

Answer №2

Consider using CSS Grid as an alternative solution. CSS grid offers the flexibility to use fractional units for redistributing space, making it a great fit for this type of design. To illustrate, I've provided an example with pixel values and a media query for different screen sizes.

.img {
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}

.grid-container {
  display: grid;
  grid-template-columns: 280px 430px 500px;
/*  grid-template-columns: 1fr 1.5fr 2fr; */
  grid-template-rows: 400px 200px 165px 350px;
  gap: 1rem;
  place-content: center;
}

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

.img-2 {
  grid-column: 2;
  grid-row: 2 / span 2;
}

.img-3 {
  grid-column: 1 / 3;
  grid-row: 4;
}

.img-4 {
    grid-column: 3;
    grid-row: 1 / 3;
}

.img-5 {
    grid-column: 3;
    grid-row: 3 / -1;
}


@media screen and (max-width: 1300px) {
  .grid-container {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: 300px 300px 400px 600px;
    gap: 1rem;
    place-content: center;
  }

  .img-1 {
    grid-column: 1;
    grid-row: 1;
}

  .img-2 {
    grid-column: 2;
    grid-row: 1;
}

  .img-3 {
    grid-column: 1 / -1;
    grid-row: 2;
}

  .img-4 {
    grid-column: 1 / -1;
    grid-row: 3;
}

  .img-5 {
    grid-column: 1 / -1;
    grid-row: 4;
}
}
 <div class="grid-container">
   <div class="img img-1" loading="lazy" style="background-image: url('https://i.imgur.com/k7fNrV9.png');"></div>
      <div class="img img-2" loading="lazy" style="background-image: url('https://i.imgur.com/knI2LcY.png');"></div>
      <div class="img img-3" loading="lazy" style="background-image: url('https://i.imgur.com/Hyn2v1J.png');"></div>
      <div class="img img-4" loading="lazy" style="background-image: url('https://i.imgur.com/oZG4m8k.png');"></div>
      <div class="img img-5" loading="lazy" style="background-image: url('https://i.imgur.com/dOvSc80.png');"></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

What is the best way to adjust the spacing between input tags in bootstrap?

On my webpage, I have implemented two input elements and now I am trying to create some separation between them. <input type="text" class="form-control" placeholder="Username"> <input type="password" class="form-control" placeholder="password"> ...

Internet Explorer (on mobile devices) does not display submenus when touched

Hello, I have a query about displaying a sublist on touch. I have created a sublist that is supposed to show up when you hover over it. You can view the demo here: @import url(//fonts.googleapis.com/css?family=Titillium+Web:400,200,300,600,700); body { ...

Having issues with jQuery Overlay functionality on Internet Explorer 8

Sorry if this question has already been addressed elsewhere, but I wasn't able to locate any information on it. I am currently using jQueryTools Overlay which is functioning properly in Chrome. However, I am encountering an issue in IE-8 where my ove ...

Proper Sequence of Bootstrap Header Configuration

I'm working on creating a header/navbar using bootstrap, but I'm unsure about the correct order and method to achieve this. This is how I want it to look: And when collapsed: Should everything be contained within the NAV tag? I have a header ...

Different methods to retrieve content within a div that is located on a separate, included page

It's a bit tricky to come up with a title for this topic, but essentially, I'm trying to figure out how to get content from an included page into a specific div using HTML and PHP. Let me break it down: In the header.php file, we have the follo ...

Hovering over the Laravel Breeze dropdown will activate a dropdown feature

Recently, I've been working on a project with Laravel Breeze and have been utilizing some default components. The dropdown component used in the navigation bar caught my attention. By default, it requires a click for the menu to drop down below. Howev ...

Are there specific mappings for system colors in CSS across various browsers and operating systems?

The CSS specification outlines a variety of built-in system colors that can be utilized, such as Highlight and Background. Is there a correlation between these built-ins and the settings of different OS/Browsers? For instance, if I implement color: Highl ...

Chart.js malfunctioning - Uncaught reference error:

In an attempt to visualize time series data using chart.js, I am encountering some issues. The data is extracted from a psql database, converted to JSON format, and then passed as a parameter to a JavaScript function for rendering the chart. Although the ...

Featuring my Cookie page prominently on my website's homepage

I am facing a simple issue: I have a cookie verification page (cookies_check.php) that I want to include on my home page (accueil.php). So, currently, the only code on accueil.php is: <?php include ('Controleur/cookies_check.php'); ?> He ...

What is the best way to retrieve the value of a custom attribute from a dropdown list using either JavaScript or jQuery?

I have a dropdown list on a web form and I need to have a 'hidden' variable for each item in the list that can be retrieved using the onchange event on the client side. To achieve this, after databinding the dropdownlist, I am setting a custom at ...

Webpack is notorious for creating multiple copies of images

Having an issue with Webpack where it's generating duplicate images, one of which is broken. I have an original image image, and after running Webpack, two duplicates are created. One works fine: image, but the other one is broken: image. I'm us ...

Struggling to line up text and image next to each other?

I have inserted an image and some text. The text consists of 1 <h6> tag and 2 <p> tags. I attempted to use the float:left property, but the content did not display side by side as intended. Here is the code from content.js: <div className ...

Efficiently input text box values into a canvas in real-time using HTML canvas and keypress

There are three text boxes labeled textbox1, textbox2, and textbox3. I am looking to transfer the values entered into these text boxes directly onto a canvas (cnv) whenever a user types in them, and remove the value from the canvas when it is deleted fro ...

In the context of content security policy, the directive "script-src 'self' blob: filesystem: chrome-extension-resource:" must be observed when attempting to retrieve data

I have implemented a jQuery simple weather plugin to retrieve weather information and am attempting to create a Chrome widget. When loading the file as a Chrome extension, I encountered an error. Despite searching for solutions on Google and here, I have ...

The CSS Specificity Hierarchy

I am currently facing a dilemma with two CSS classes in my codebase. Despite having a stronger specificity, the second class is not being chosen over the first one. Can anyone shed some light on this issue? The CSS class that is currently being applied is ...

retrieve user input from various angular 6 components

Currently, I am in the process of developing a small web-based game using Angular 6. Within this project, I have two key components - play.component and setup.component. The main concept is to allow users to customize elements such as difficulty within the ...

When I click on a link to redirect it, I am confronted with a bizarre rectangle that

When attempting to create redirects using images, I encountered a small issue. Take a look at this screenshot: https://i.sstatic.net/LgStu.pnghttps://i.sstatic.net/eEnNB.png This is the item in question: <p><div><a title="Home" hr ...

I'm noticing that the dropdown content for all my buttons is identical. What could be causing this

I have encountered an issue with the two buttons placed inside my header. Whenever I click on either button, the content displayed remains the same. https://i.sstatic.net/fk4V8.png https://i.sstatic.net/TCL7H.png It seems that when I click on one button, ...

The min-height property in Bootstrap 3 causes a div element to not expand as expected

Having a bootstrap 3 page with a centered box that contains content, I noticed that the box does not expand when the content grows. The bootstrap container grows, but the box remains static. I have tried using "position:relative" and "overflow:hidden", as ...

Problem with Ajax-appended form element not functioning -

I'm struggling with this ajax form method code $.ajax({ type: "GET", url: "/MainPage/GetAllRecords", dataType: "json", contentType: "application/json; charset=utf-8", success: function (data ...