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

What are the steps to designing a Vertical Curve UI?

Is there a way to replicate the unique vertical curve on the login page, as shown in the image below? I've come across horizontal SVGs that can achieve this effect, but I'm struggling to find resources on implementing it vertically. How is this ...

What is the hexadecimal color code for a specific element's background?

How can I retrieve the background color code of a specified element? var backgroundColor = $(".element").css("background-color"); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="elemen ...

Having trouble retrieving Firebase data to display on a React chart

I am currently utilizing ApexChartJs in my React project. However, when attempting to retrieve dynamic data from my Firebase database, it returns undefined. https://i.stack.imgur.com/8lcnz.png Below is a snippet of code from my project: import React, { u ...

After being redirected from another page using history() in React, the state is initially set to null but later gets updated to the correct value - Firebase integration

After logging in and being redirected to the profile page, I encounter an error that says 'Unhandled Rejection (TypeError): Cannot read property 'email' of null'. How can I ensure that the state is set before proceeding with any additio ...

The chosen element contains a value of -1

When the select element has a selected value of 4, form data is sent to the server and the controller returns a partial view. <script> $(document).ready(function () { var objSel = document.getElementById("IDVacationApplicationTyp ...

Having trouble rendering an HTML webpage using Flask's RESTful API? Your webpage might be displaying improperly

Trying to display an HTML page on local host using a restful flask API. The content of the HTML page appears as a string with "" instead of rendering the actual page. class data(Resource): def get(self): #return "Welcome!" return rende ...

The app.html for the skygear/react-chat-demo is malfunctioning

I followed the instructions provided in the Skygear manual at https://docs.skygear.io/guides/advanced/server/ The skygear-server-darwin-amd64 started successfully. Then, I attempted to run the react-chat-demo project from https://github.com/skygear-de ...

Using ion-icon inside a clickable ion-card while applying float: right does not render properly

I am facing an issue with a list of ion-cards that have clickable icons appearing when you hover over them. The problem is that due to the floating nature of the icons, they are not positioned correctly (as shown in the image below) and end up getting cove ...

Creating a seamless navigation experience using Material UI's react Button and react-router-dom Link

Is there a way to have the Material UI react Button component behave like a Link component from react-router-dom while preserving its original style? Essentially, how can I change the route on click? import Button from '@material-ui/core/Button' ...

Using jquery to toggle the visibility of input fields

I'm encountering an issue with this straightforward code snippet: $("#additional-room").hide(); var numAdd = 0; $("#add-room").click(function(e) { e.preventDefault(); $("#additional-room").show(""); if (numAdd >= 3) return; numAd ...

Is there a way to replicate table cells in the style of Excel using jQuery?

Excel has a convenient feature that allows cells to be copied by dragging and dropping with the mouse. This same functionality is also available in Google Spreadsheets. I am trying to understand how Google has implemented this feature using JavaScript cod ...

Tips for Showing jGrowl Messages When Redirecting Pages

After setting up jgrowl, everything appears to be functioning properly. Currently, I have to manually press a button in order for the message to appear in the top right side of the screen as shown below: <input type="button" onclick="$.jGrowl('You ...

Trouble aligning CSS UL/LI menu in the center

Could someone help me with centering my CSS UL menu? I've posted the code below, as I'm still learning CSS and would appreciate any guidance. I've only been able to center it by setting a fixed width and using HTML center tags, but I need t ...

Encountering an empty array in a PHP script when transmitting form data via AJAX using the POST method

I have been using the serialize() method to create a URL encoded string, but despite trying various solutions, nothing seems to work. Below is the code I am working with: <form id="idEditTaskForm"> <div class="form-group"&g ...

What causes useEffect to run twice in React and what is the best way to manage it effectively?

I'm currently facing an issue with my React 18 project where the useEffect hook is being called twice on mount. I have a counter set up along with a console.log() inside the useEffect to track changes in state. Here's a link to my project on Code ...

Arranging elements on a website using CSS styling

I'm interested in creating a button <Button id="Button" text="Hey" />. I would like to know how I can position it on a webpage exactly where I want it, rather than it just appearing randomly without content. ...

What is the correct method for embedding a javascript variable into a Twig path?

I am looking to include a variable declared in JavaScript into the path for redirecting my page. Here is my code: var id = $(this).attr('data-id'); windows.location = {{ path("mylink", {id: id}) }}; Unfortunately, I am getting an error when ...

Setting an error state on the parent div of a checkbox

I am facing an issue with styling a standard HTML checkbox when it is in an error state. Since it's not possible to style the checkbox directly, I am attempting to apply the styling to its parent div instead. Consider this example: <div class="fo ...

Button click event is not being triggered by Ajax rendering

I am facing an issue with my Django template that showcases scheduled classes for our training department. Each item in the list has a roster button which, when clicked, should display the class roster in a div. This functionality works perfectly. However, ...

Update the displayed locations on Google Maps by fetching and displaying marker data

I am able to retrieve and display information from my MySQL table, but I need assistance with refreshing this data every 5 seconds using my current code. The data being shown is not extensive, just about 5 or 8 markers at a time. Below is the code I curren ...