transferring background-image in html between elements

Imagine having a three-level hierarchy HTML code structured like this:

<section>
  <div id="outer">
    <div id="inner">
    </div>
  </div>
</section>

If we set background-image:someImage.jpg to the section, and background:white to the outer div, is there a way to apply the same image - someImage.jpg - as the background to the inner div but in a way that it still shows the section's background instead of duplicating the image? The inner part of the inner div should be transparent so that the background image of the section is visible instead of the white background of the outer div.

Answer №1

If you're looking for something similar to this setup:

<div id="container">
  <div class="box">
    <div class="inner-box">
    </div>
  </div>
</div>

#container, .box, .inner-box {
    padding: 15px;
    display: flex;
}

#container {
    background-color: #f2f2f2;
}

.box {
    background-color: #d9d9d9;
}

.inner-box {
    background-color: #bfbfbf;
}

Check it out on CodePen: http://codepen.io/yourusername/pen/Xyz123/

Answer №2

Instead of using a background for #outer, try adding a border and make #inner slightly see-through. Here's an example:

section {
    float: left;
    padding: 40px;
    background: url(background image) repeat center center;
}

#outer {
    border: 40px solid white;
}

#inner {
    width: 60px;
    height: 60px;
    background-color: rgba(255, 255, 255, 0.5)
}

You can view the result on this Fiddle.

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

Searching for the index of an item in a PHP array using the inArray jQuery method

I came across this code snippet online that seems to achieve what I'm looking for: $(document).ready(function(){ var arrData = [ "j", "Q", "u", "e","r","y" ]; alert(jQuery.inArray("Q", arrData)); }); Now, I have an array generated from a ...

When using Angular, it is important to remember that calling `this.useraccount.next(user)` may result in an error stating that an argument of type 'HttpResponse<any>' cannot be used with a 'Useraccount' balance

When attempting to use this.useraccountsubject(user) to insert information upon login, I encountered an error: ErrorType: this.useraccount.next(user) then Error An argument of type 'HttpResponse' is not allowed against a balance of 'Userac ...

Uncover a section of text from HTML using the Beautiful Soup module

I have an HTML snippet like this: <span id="lbldiv" class="lbl" style="color:Blue;"> Division : First; Grand Total: 3861; Grand Max Total: 4600 </span> By using the get_text method on the span element, I can extract the text: Division : ...

Experiencing difficulty when attempting to save a zip file to the C drive

I came across this code snippet on SO and decided to use it for my project. The goal is to send a simple 1.5mb zip file and save it on my C drive by making a request through Postman with the binary option enabled, sending the zip file to localhost:3012. c ...

Utilizing ng-href in Angular.js Template: A Guide

I am attempting to develop a simple Single Page Application (SPA) with just one index.html file that includes templates. However, I encountered an issue with the ng-href directive: <a ng-href="#/myPage">myPage</a> This works fine in index.h ...

Troubleshooting Problems with Owl Carousel Loading

Having trouble with Owl Carousel loading issue. I've set up my carousel using the Owl Carousel jQuery plugin as guided, but it's showing me an "owl-carousel loading" class added to the DOM (owl-loading). I've tried adding custom styles and J ...

How to open a PDF file in a new tab using Angular

Within my Angular 12 application, I am seeking to enable users to "view" a PDF file stored locally in the application within a new tab on Google Chrome. Currently, when implementing the code below in HTML, the file downloads rather than opening in a new ta ...

Using jQuery to pause execution until a function returns before proceeding

After updating to version 1.10.2, I encountered some issues with jQuery. Despite my attempts to find a solution, none of the methods I've tried have seemed to work. It's possible that I am missing something in my approach. Any assistance would b ...

Is it possible to utilize an npm package in TypeScript without a d.ts definition file?

Is it possible to use an npm package in TypeScript and Node.js without a .d.ts definition file? If so, how can I make it work? Currently, my code looks like this and I'm getting an error that says "cannot find module 'node-rest-client'" bec ...

I'm stumped by this code; I can't figure out what's going wrong

$(document).ready(function() { $.ajax({ type: 'GET', url: 'data.json', dataType: 'json', success: jsonParser }); }); $(".btn_val1").click(function() { ...

Tips for finding group words within a div panel

Check out this interactive demo I created to demonstrate what I need help with. There are multiple panels containing words, each separated by a line break. I am trying to create a filter that will hide panels that do not match the words entered in the sea ...

Share on your Twitter feed

Currently seeking assistance in implementing a function on my website that allows users to post their individual posts to Twitter. For example: Message: "hello world" [twitter] By clicking on the twitter button, the message will be posted along with the p ...

site.js problem

Recently, I decided to create my very own CS:GO gambling website. After successfully setting it up on my localhost using VPS, I moved on to getting a domain and finalizing everything. However, when attempting to run site.js, an error message popped up: [e ...

Nuxt.js ERROR: Unable to find reference to 'window' object

Currently working with Nuxt.js and encountering an issue while configuring vuex-persist. Seeking assistance from someone familiar with this problem. store/index.js store/LangModule.js ...

Trouble with passing the function variable to setState efficiently

Although I haven't worked with React for a while, it seems like this issue is more related to plain JS. Here is the setState function that I am using: // click event on parent for performance reasons ``Component:`` const [active, setActive] = useSta ...

Tips for resolving CORS problems when trying to pull data from an API by utilizing jQuery AJAX and a JAVA backend

Currently, I am working on developing a front-end application to display data fetched from an API. This particular API was created using JAVA and Swagger.io by an android engineer. At the moment, the API does not have any authentication mechanism in place, ...

Create a custom sorting pipe in Angular 10 that allows for a specific value hierarchy to be passed, without adhering

I am attempting to sort a list of form objects in angular 10 using a custom pipe. The goal is to order them based on a specific property with the following priority: [{VALID: 1}, {INVALID: 2}, {DISABLED: 3}]. I have defined this order as an argument for th ...

Strategies for organizing your week with a calendar

Hello, I am working on creating a weekly calendar using PHP and I want to add events to the calendar like in this example. However, I am unsure how to display the events in the calendar at the correct time of day. Here is the code snippet I am currently u ...

Employ various iterations of the leaflet library

Creating a web application using React and various libraries, I encountered an issue with conflicting versions of the Leaflet library. Currently, I am incorporating the Windy API for weather forecast, which utilizes Leaflet library version 1.4.0. However ...

Unable to attach the event listener for the 'onchange' event to a div nested within a ul element

Here is the HTML code I am working with: <ul> <li> <a href="#"> <div></div> Actions <div></div> </a> <ul> <li> <a> &l ...