Flip the parent element's background image using CSS

I have encountered a challenging issue that I am hoping to find a solution for. Let's consider the following HTML structure:

<div id="page">
   <div id="menubar"></div>
</div>

Along with the CSS styling applied to it:

#page {
   background: url("some-image.png") no-repeat center center fixed;
   -webkit-background-size: cover;
   -moz-background-size: cover; 
   -o-background-size: cover;
   background-size: cover;
}

#menubar {
    background-color: rgb(36, 108, 228);
    background-color: rgba(0, 50, 255, 0.6);
}

To elaborate, there is a div element #page with a background image covering it completely and a menubar with a 60% opacity allowing the background of div #page to show through. My challenge lies in wanting the background to shine through the menubar while also inverting the colors of the background where they are visible through the menubar.

Despite my efforts in searching online, I could not find a direct solution as the invert filter typically applies to entire images. Hence, I am reaching out here on SO in hopes that someone might suggest a clever workaround or technique to achieve this effect.

I appreciate any insights offered in advance!

Answer №1

Instead of directly targeting the parent's background image, you can utilize the inherit property in CSS to inherit the properties from the parent element.

#container {
    background: url("http://lorempixel.com/1280/720/nature/") no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover; 
    -o-background-size: cover;
    background-size: cover;
}

/* Inherit the background image */
#child {
    background: inherit;
}

Alternatively, you can combine the selectors for both #container and #child in the same declaration block.

#container, #child {
    background: url("http://lorempixel.com/1280/720/nature/") no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover; 
    -o-background-size: cover;
    background-size: cover;
}

Once the #child element inherits the background image, you can add another rule to adjust the colors using the invert filter.

#child {
    -webkit-filter: invert(100%);
    filter: invert(100%);
}

Check out this JSFiddle

Note: Color inversion and additional styling applied to the child element within #child to revert the colors.

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

Overlaying a parent container DIV on top of its child elements

My JavaScript scrolling view is quite intricate, enclosed within a DIV. The elements inside have event handlers for mouseover and mouseout to manage the scrolling behavior, as well as clickable links. Currently, I am in need of a semi-transparent image th ...

Arranging elements on the top of an image

I am struggling to replicate a design and align some items properly without resorting to absolute positioning. The design I want to achieve looks like this https://i.sstatic.net/HaOXT.jpg https://i.sstatic.net/EovkI.jpg The grey area represents a section ...

Utilizing the power of Angular 15 in conjunction with Bootstrap 5's SCSS

After recently updating to Angular 15 with Bootstrap 5, I noticed that none of my utility classes are working (e.g. mb-3, mt-5, etc.) when including bootstrap in my styles.scss as shown below. @import 'bootstrap/scss/bootstrap.scss'; I understan ...

I find myself facing a roadblock in navigating Servlets and HTML

I'm currently immersed in a project aimed at launching an innovative online food ordering platform. To bring this vision to life, I've harnessed the power of HTML, CSS, and JavaScript for frontend development, Java (Servlets) for backend function ...

Looking for a single solution to have my background image in CSS cover the entire screen on both my computer and cell phone

I used the following code in my demo .html file: body{ background-image: url('https://i0.wp.com/www.socialnews.xyz/wp-content/uploads/2020/10/26/5573c6f0bee7ca021f4a8aecbaf6cbeb.jpg?quality=80&zoom=1&ssl=1'); -webkit-backgroun ...

Clicking on a radio button will instantly update a specific section of the webpage

Is there a way to implement radio buttons with specific filters that can dynamically change content on a website without the need for a full page refresh? Picture having radio buttons on the left side of the navigation, with a table on the right that smoo ...

Appending an empty <li> tag has no effect

I'm facing an issue with this loop where it always generates empty <li></li> tags. Can anyone help me understand why this is happening and suggest a solution? For reference, the loop runs 2 times (verified). function a(){ for (var i ...

utilize javascript to parse and access data from the xml.aspx file

After using F12 in Firefox, an error "NS_ERROR_FAILURE" appeared for html:16 (xml.send();). The opensource website suggests using HTTPS request method: GET. Why is there an error even when following these instructions? Any insight is appreciated. Thanks! ...

What could be causing my slider to malfunction?

No errors are being returned by the console. Currently utilizing Unslider. The setup includes: <div class="slider"> <ul> <li><img src="img/one.jpg" alt=""></li> <li><img src="img/one.jpg" ...

Alter the HTML tag once the text is encompassed within it

Can you configure your HTML code to display (B) when the text in (A) is wrapped? If it's not wrapped, then just display (A) (A) <div> <span>abcdefgabcdefgabcdefg</span> <span>abcdefgabcdefgabcdefg</span> ...

What are the steps to format text into 2 columns with a personalized design?

As I develop a website using HTML, CSS, and jQuery, I encountered a layout challenge. The text on the page is designed in a 2-column style, with an image positioned before it. Currently, the structure looks like this: <div> <div style=" ...

Is it possible to overlay a three.js scene onto an HTML video element?

A project I am working on involves developing an AR app that can display markers for North, East, South, and West using a smartphone camera. The goal is to create a scene with a transparent background that can be overlayed on a video feed from the camera. ...

"Selenium continues to throw a "No Element Found Exception" error despite attempts to copy the element from

My goal is to log in to this page and access LexisNexis. Here is the script I am using: from selenium import webdriver browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver') type(browser) browser.get('https://sfx.carli.i ...

Tips on restricting access based on User Browser type and version

I have a question regarding: My web application is built using PHP, AJAX, Javascript, CSS, and other technologies that may not be fully compatible with older browsers. Certain functions and styles might not work correctly on outdated browser versions. Th ...

How can I retrieve all data from a table in PHP with MySqli without knowing the table name?

Currently, I am immersed in a personal project that resembles a document management system. As part of this project, I am establishing new folders and tables in the database with identical names. I have successfully compiled a list of all these folders an ...

Transforming the color of Material Design Lite badges

Incorporating Material Design Lite (getmdl.io) into my web application, I have implemented a card with a badge displaying the number of notifications. Despite attempting to modify the css code as follows: .mdl-badge { background: #FF3D00; } I am unabl ...

All the optgroups in the select picker are being duplicated from the first optgroup

I am currently facing an issue with a select picker multiple dropdown. When I add optgroups, the values of the second and third optgroups are being copied by the first optgroup. The opt-groups are being populated via JavaScript. <select class="mod ...

Background image on webpage failed to load

I've encountered an issue with a background image I downloaded for my website - it simply will not display. Here is the CSS code I am using: background-image: url(Resources/Icons/background-image.jpg); background-attachment: fixed; Even after tryin ...

Creating dynamic image carousels using the latest versions of Bootstrap and AngularJS

I have created an array that stores various images using angularJS: $scope.docImg = [ '../../Content/Image/BackGrounds/abra.png', '../../Content/Image/BackGrounds/background_black.jpg', '../../Content/I ...

Removing the final BR elements from a div using jQuery

Does anyone know how to remove only the last <BR> tag within a div using jQuery? The code I tried ends up removing all <BR> tags in the div instead of just the last one. HTML Code <div class="topic"> Lorem ipsum dolor sit amet, consect ...