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

Setting the focus on an input when updating a property in the Pinia store using Vue

When a component I have is clicked, it triggers a function in the store: <button @click="this.store.foo()"></button> Within the store, I am updating a specific property: state: () => ({ focusIn: false, }), actions: { foo() { ...

Storing information in the DOM: Choosing between Element value and data attribute

When it comes to storing values in a DOM element, we have options like using the data attribute. For example, $("#abc").data("item", 1) can be used to store values and then retrieve them with $("#abc").data("item"). However, I recently discovered that th ...

Ascending and descending functions compared to Ext.getCmp()

I'm feeling a bit lost trying to figure out which method to use when using grep object - should I go with up(), down(), or Ext.getCmp(ID)? Personally, I find it simpler to assign an ID to the object and then retrieve it using Ext.getCmp('ID&apos ...

What is the best way to pass an array to a JavaScript function from a different page?

My website has a static settings page where my JavaScript function uses AJAX to retrieve data from a MySQL table and display it in a table on an HTML document. It's working perfectly, gathering all the necessary data efficiently. Here's the code ...

Is it not possible to include Infinity as a number in JSON?

Recently, I encountered a frustrating issue that took an incredibly long time to troubleshoot. Through the REPL (NodeJS), I managed to replicate this problem: > o = {}; {} > JSON.stringify(o) '{}' > o.n = 10 10 > JSON.stringify(o) ...

"Encountered a TypeError: Cannot read property 'params

I've encountered an issue with passing the id to my product page. Despite trying various solutions and searching for answers, I still can't get it to work. Below is my index.js code: import React from "react"; import {render} from &quo ...

I'm having trouble getting my if statement to function properly with a random number

I'm currently working on creating a natural disaster sequence for my game, and I'm using Math.random to simulate different outbreak scenarios. However, I've encountered an issue at the end of my code where an if statement is supposed to trig ...

Is there a way for me to instruct jQuery to revert an element back to its initial value, prior to any dynamic alterations?

My question is regarding changing the content of a div using the `html()` method in JavaScript. I am currently working on implementing a "cancel" button and would like to revert the content back to its original value without having to completely reset it w ...

Ways to exchange information among Vue components?

My role does not involve JavaScript development; instead, I focus on connecting the APIs I've created to front-end code written in Vue.js by a third party. Currently, I am struggling to determine the hierarchy between parent and child elements when ac ...

This route does not allow the use of the POST method. Only the GET and HEAD methods are supported. This limitation is specific to Laravel

I am encountering an issue while attempting to submit an image via Ajax, receiving the following error message: The POST method is not supported for this route. Supported methods: GET, HEAD. Here is the Javascript code: $("form[name='submitProfi ...

"Exploring the Power of Logarithmic Slider with Vue and Quasar

I'm currently working on a project utilizing Vue 2 and Quasar 1, where I am attempting to develop a logarithmic slider. Initially, I managed to create a basic example using a native input slider in this code pen: https://codepen.io/tonycarpenter/pen/Z ...

"Displaying 'Object Object' as a title when hovering over an element

I am currently using an accordion element to display several FAQs on a webpage. The code snippet for the accordion with content is as follows: { Object.keys(FAQS).map((key, index) => { return ( <div key={key}> ...

Exploring the distinctions between Django template variables and JavaScript variables

I am currently trying to populate a table in a Django template. The challenge I am facing is comparing cell values between a JavaScript variable and a Django template variable within the same context. Is there a way to perform this comparison without conve ...

What methods can be used to initiate form error handling in React/Javascript?

I am currently utilizing Material-UI Google Autocomplete, which can be found at https://material-ui.com/components/autocomplete/#google-maps-place, in order to prompt users to provide their address. https://i.stack.imgur.com/nsBpE.png My primary inquiry ...

In HTML, data can be easily accessed, however, JavaScript does not have the same

When trying to access the 'data' element in a JSON object, I have encountered an issue. The element is accessible when called from HTML, but not when called in JavaScript. HTML: <p>{{geoJson.data}}</p> JavaScript: let scope; let d ...

Why isn't my heading showing up as expected in my document?

Can anyone help me understand why my title appears so low on certain screens? I've previously tried removing the negative margin when the title was hidden, but now it's positioned too far down. Here is the link to my webpage: ...

"Typescript: Unraveling the Depths of Nested

Having trouble looping through nested arrays in a function that returns a statement. selectInputFilter(enteredText, filter) { if (this.searchType === 3) { return (enteredText['actors'][0]['surname'].toLocaleLowerCase().ind ...

Anticipating the asynchronous completion of all nested recursive functions

I have a recursive function that makes asynchronous calls, using the results of those calls as arguments for subsequent recursive calls. I am trying to find a way to wait for all recursion levels to complete simultaneously, without any delays at each leve ...

Is there a way to automatically advance to the next question in Surveyjs without needing to click the Continue

I'm currently integrating SurveyJs into my web application. The first 3 pages of my survey contain HTML elements that I would like to automatically slide after a few seconds, similar to the functionality on the following sample site using SurveyJS: O ...

Django enables anonymous Ajax requests to reach a Generic View

Here is the current progress I have made: from django.views.generic import View from django.views.decorators.csrf import csrf_exempt class ConfigurationView(View): @csrf_exempt def dispatch(self, *args, **kwargs): return super(Configurati ...