What is the best way to layer four images in a 2x2 grid over another image using CSS as the background

I am attempting to place 4 images of the same size on a 5th image defined as the background.

Currently, it looks like this:

It works perfectly when the height is fixed, but in my case, the height may vary causing this issue:

This problem isn't surprising as I am using '%' instead of 'px'.

When the width changes, the style 'left: 13%' also changes

Crucial!!! I can only use "%"

How can I achieve the layout shown in the first image even if the height changes?

Here is the relevant code snippet:

<!-- BG image -->
<div style="position: relative; right: 0; top: 0; height:100%">   
    <img src="img/groups/pic-shade.png" style="
                             position: relative;
                             top: 0%;
                             right: 0%;
                             height: 17.6%;
                             ">  
     <!-- left-top image -->
    <img  
           style="position: absolute;
                  height: 42.25%;
                  top: 0%;
                  left: 0%;" src="img/group_6.png">
<!-- right-top image -->
    <img  
           style="position: absolute;
                  height: 42.25%;
                  top: 0%;
                  left: 13%;" src="img/group_6.png">

<!-- left-bottom image -->
    <img  
           style="position: absolute;
                  height: 42.25%;
                  bottom: 0%;
                  left: 0%;" src="img/group_6.png">

<!-- right-bottom image -->
    <img  
           style="position: absolute;
                  height: 42.25%;
                  bottom: 0%;
                  left: 13%;" src="img/group_6.png">
  </div> 

[EDIT]

I attempted to use right: 0% instead of left: 13%, but no improvement as the background div has a larger width than the background image:

Referenced root DIV:

Answer №1

VIEW DEMO

Avoid using position absolute as it disrupts the flow and relationship between elements, preventing them from responding to changes in width and height.

Instead, try implementing the table technique demonstrated in the provided demo link.

HTML Code:

<div class="bg-image-wrapper">
    <div class="table">
        <div class="row">
            <div class="cell"><img src="http://placehold.it/100x100" alt="" /></div>
            <div class="cell"><img src="http://placehold.it/100x100" alt="" /></div>
        </div>
        <div class="row">
           <div class="cell"><img src="http://placehold.it/100x100" alt="" /></div>
           <div class="cell"><img src="http://placehold.it/100x100" alt="" /></div>
        </div>
    </div>
</div>

CSS Code:

img{
    display:block;
}
.table{
    display: table;
}
.row{
    display: table-row;
}
.cell{
    display:table-cell;
}
.bg-image-wrapper{
    display: inline-block;
    background: url(http://placehold.it/200x200);
    border-radius: 10px;
    overflow: hidden;
}

Answer №2

<!-- Background Image Section -->
<div style="background-image:url("img/groups/pic-shade.png"); position: relative;  right: 0; top: 0; height:100%">   

<table>
<tr>
<td>
                   <img style="height: 100%;width: 100%;src="img/group_6.png">
</td>
<td>
                   <img style="height: 100%;width: 100%;src="img/group_6.png">
 </td>
 </tr>
 <tr>
 <td>
                    <img style="height: 100%;width: 100%;src="img/group_6.png">
</td>
<td>
                    <img style="height: 100%;width: 100%;src="img/group_6.png">
</td>
</tr>

</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

Animating file transfer can be achieved using either JavaScript or CSS

My goal is to create a visual representation of file transfer, such as moving a file from one folder to another. I have successfully achieved this using JavaScript with the following code: <script type="text/javascript> var img; var animateR; var an ...

What is the process of obtaining User properties through a URL and utilizing them as variables in JavaScript?

I need to retrieve the city properties: 918 using req.params.userMosque from the URL '/shalat/:userMosque'. I want to assign it to the variable city for customizing my API url request. However, it seems like it's not working as expected. I h ...

Troubleshooting: Issue with onclick event in vue.js/bootstrap - encountering error message "Variable updateDocument not found"

As a newcomer to frontend development, I have a basic understanding of HTML5, CSS, and Javascript. Recently, I started working on a vue.js project where I integrated bootstrap and axios. Everything seemed to be working fine until I encountered an issue whe ...

Immediate self-invocation occurs within a JavaScript function

My goal is to dynamically create a DOM button object with JavaScript, and it seems to be happening perfectly fine. However, I'm facing an issue where if I try to assign another JavaScript function to one of the buttons, the other script triggers itsel ...

The GraphQL Resolver function that returns an object

When querying with GraphQL, I receive results in the following format: { "data": { "events": [ { "_id": "65f0653eb454c315ad62b416", "name": "Event name", " ...

Ways to make a function inside a Node.js script get called by JavaScript in HTML?

At the moment, I am integrating Node.JS with various systems as the backend. My goal is to trigger a function in the Node.JS script from my webpage and retrieve specific values. This illustration outlines my objective: JS --> triggers function --> ...

Canvas not displaying image in JavaScript

I have a bird object with a draw function defined, but for some reason, the image is not showing up when called in the render function. Can someone please help me figure out what I'm doing wrong? Thanks in advance. EDIT: Upon further investigation, I ...

The footer is not displaying the background image properly

Currently in the process of creating my website. I've successfully added my banner and footer, but unfortunately, I'm facing an issue with the background. It seems to not be stretching out properly, leaving some empty white space. Click here to ...

Issue with React Router functionality not functioning

I am currently facing an issue with my react-router setup. You can find the code I am using by visiting this link - https://github.com/rocky-jaiswal/lehrer-node/tree/master/frontend Although it is a basic setup for react-router, I am experiencing difficu ...

Improving the performance of a function that generates all possible combinations of elements within an array

After coming across this function online, I decided to optimize it. For instance, if we have an input of [1, 2, 3], the corresponding output would be [[1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] Below is the code snippet: const combinations = arr = ...

Switch back and forth between multiple functions

It appears that the code provided is not functioning correctly with jQuery versions higher than 1.8. Can anyone offer insight on how to achieve the same functionality with newer versions? Additionally, should we be structuring our code like element.click(f ...

Tips for dividing the rows within an array using the match() function?

Within my matrix are arrays of rows. let matrix=[['hello'],['world']]; I am looking to duplicate each row within the matrix. matrix=matrix.map(x=>String(x).repeat(2)).map(x=>x.match(new RegExp??)) The desired outcome should be [ ...

Leaving the pipeline of route-specific middleware in Express/Node.js

My implementation involves a sequence of "route specific middleware" for this particular route: var express = require('express'); var server = express(); var mw1 = function(req, resp, next) { //perform actions if (suc ...

Tips for executing a .exe file in stealth mode using JavaScript?

I am currently working on the transition of my vb.net application to JavaScript and I am facing a challenge. I need to find a way to execute an .exe file in hidden mode using JS. Below is the snippet from my vb.net code: Dim p As Process = New Pro ...

Guide to invoking a jQuery function by clicking on each link tab

Below is a snippet of jQuery code that I am working with: <script> var init = function() { // Resize the canvases) for (i = 1; i <= 9; i++) { var s = "snowfall" + i var canvas = document.getElementById( ...

Load Jquery hover images before the users' interactions

Currently, I am in the process of creating a map of the United States. When hovering over any specific state, my goal is to replace the image with one of a different color. The issue I am encountering lies in the fact that the image gets replaced and a ne ...

Removing a section of HTML from an EmailMessage Body in EWS

Is there a way to remove certain parts of the EWS EmailMessage .Body.Text value in C# before replying with a ResponseMessage? The elements that need to be removed include clickable and non-clickable buttons in HTML format. Since custom tags cannot be dec ...

Unsure if values can be imported from one component to another in Vue.js using props

Currently experimenting with Vue and ran into a minor challenge - wondering if there's a solution to this. I am looking to add a new value to my items array using my Button component so that I can display it using v-for. While consolidating everything ...

The hover selector in Material UI and React is not functioning as expected

My code is experiencing an issue where the hover selector does not appear to be working. Although all other styling aspects function correctly, the hover effect is not visible. import React from "react"; import Paper from '@material-ui/core/Paper&apo ...

Styling each list item individually, without interdependence

I am working on a ul that contains several list items comprised of h3s and lis with the "close" class. When I hover, I want to expand the letter spacing on the h3s, but the items with the close class also expand. I have tried using nth child selectors in ...