Customizable CSS cutout with varying heights on top of a static image background

Imagine a scenario where a cutout is made in an opaque background, revealing a fixed image beneath it as it scrolls. The height of the cutout adjusts dynamically to accommodate its content.

Issue: A not-so-great workaround is employed in the code! You may have noticed that the content Y within the cutout is duplicated in the HTML, although it appears only once visually.

The surprising part is—the workaround "functions". Both duplicates serve a purpose:

  • Removing the first duplicate causes the remaining content Y to show up behind the image, rendering it invisible.
  • Deleting the second duplicate results in the cutout collapsing to zero height, leading to an overlap between the remaining content Y and content Z.

Inquiry: Is there a more elegant solution, using just CSS, to replace this workaround?

<!DOCTYPE html>
<html>
<head>
<title>Simulated Backdrop Image Cutout through Opaque Background</title>
<style>
.opaque-background {
background-color: black;
color: white;
}

.container {
position: relative;
}

.cutout {
background-attachment: fixed;
background-image: url(https://i.sstatic.net/RTFBR.jpg);
background-repeat: repeat;
position: absolute;
width: 100%;
height: 100%;
}
</style>
</head>
<body class="opaque-background">
Content preceding (X)
<div class="container">
<div class="cutout">
Content between (Y)
</div>
Content between (Y)
</div>
Content following (Z)
</body>
</html>

Answer №1

Well, it seems I made quite a rookie mistake there. The solution was actually quite straightforward - all I had to do was adjust the z-index so that the cutout would appear behind the content Y. (I could have sworn I tried that already, but apparently not.)

.cutout {
    background-attachment: fixed;
    background-image: url(https://i.sstatic.net/RTFBR.jpg);
    background-repeat: repeat;
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: -1;
}

This is how the HTML should look:

Content preceding (X)
<div class="container">
    <div class="cutout">
    </div>
    Content between (Y)
</div>
Content following (Z)

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

Issue with fixed header in Vuetify v-data-table not functional

While working with the Vuetify v-data-table component, I have enabled the fixed-header property. However, I have noticed that the table header is scrolling along with the table body. I am using the latest version of Chrome. Does anyone know how to addres ...

Place unchangeable elements both preceding and following an input field

At first glance, this question bears a striking resemblance to this specific one. However, the original poster indicated that it remains unanswered, prompting me to inquire about its feasibility and implementation. Here is what I aim to achieve: I am seek ...

Incorporating RFID reader functionality into a website

I am currently facing an issue with integrating an RFID card reader into a web page. After some research, it seems that the solution involves creating an ActiveX component and using JavaScript. My question is, how can we go about building an ActiveX compo ...

Locate specific phrases within the text and conceal the corresponding lines

I have a JavaScript function that loops through each line. I want it to search for specific text on each line and hide the entire line if it contains that text. For example: <input id="search" type="button" value="Run" /> <textarea id ...

Can JavaScript be used to continuously monitor a window variable object in real-time?

Is there a way to dynamically control a variable in JavaScript? I'm currently working on a code that needs to display a button when it reaches the last signature of an automatic request process. The code for activating/deactivating the button is show ...

When the only source is available, the image undergoes a transformation

Is there a way to dynamically adjust the height of an image using JQuery or JavaScript, but only when the image source is not empty? Currently, I have an image element with a fixed height, and even when there is no source for it, Chrome still reserves sp ...

Using Styled Components to achieve full width for input tag in relation to its parent

I am working with an input field that has a specific width set. My goal is to increase the width of this input field to 100% by selecting it from its parent component. Is there a way to achieve this without passing an explicit width prop in the styled c ...

The background on my modal remains unchanged

I have integrated a modal using Iframe for user login/registration on my website. Here is the HTML code: <div class="modal" id="ays-popin-connexion" aria-hidden="true"> <div class="modal-body" aria-hidden="true"> <iframe src=" ...

Looking to adjust the positioning of text being inserted into a column - unable to utilize text-align or float properties

I'm struggling to position my text in a specific area within a column using CSS. Our app displays a price when a quantity is selected, but I can't seem to center the $14 under 'Item Total'. Even using float: center; hasn't worked f ...

Calculating the upcoming multiplier values for spacers in the Bootstrap 4 framework

Bootstrap is equipped with a range of 0-5 spacers and multiplier values between 0.25 to 3. If the goal is to incorporate spacers up to 15, what should the corresponding multiplier values be? Observing a sequence where each number is twice as much as the p ...

Issue with resizing keyboard/dropdown popup in Android hybrid app when navigating offscreen

Here is the offscreen navigation menu I have set up. When I open a keyboard or dropdown, the offscreen menu changes and exhibits a small gap on the left side after the popup (highlighted in red). It seems like the menu is shifting further away from the ed ...

How can I smoothly animate a DIV over a background in both directions?

Unfortunately, the full example I wanted to share is no longer available online. Let me try my best to describe it instead. The idea is to have a continuous looping video as the full-screen background. On top of that, there will be a DIV containing anothe ...

Is it achievable to establish both a maximum and minimum font size and maintain responsiveness when scaling?

Currently, I am using the following CSS class: .md-subheader-title { // 22 pixels font-size: calc(12px + 2vw); } While the font size is 22 pixels at max screen size, it reduces to less than 14px on mobile devices. Is there a way to make it respo ...

How can I arrange images vertically instead of placing them side by side?

I need help figuring out how to display multiple images in a vertical format instead of horizontally in the code below. The images are wide and take up too much space if shown side by side. My coding skills are basic, so specific guidance on where to mak ...

What is the best way to display breadcrumb text on a new line within a pop up when the width exceeds without resorting to a scroll bar

Presently, my breadcrumb has a scrollable wrap with text overflowhttps://i.sstatic.net/dhllv.png I want to make the overflowed text continue on the next line. How can I achieve this? The CSS code I am using for the image attached is as follows: .breadcrumb ...

How can we show pictures when a user clicks?

After creating a model for displaying images in a modal when clicked by the user, I encountered an issue. Whenever I try to access the images from the gallery within the modal content, it only displays a blank popup. I want the pictures from the image gall ...

What is the best way to create space between Bootstrap cards without causing flex-wrap to activate?

Looking for guidance on managing Bootstrap cards with a 2px margin between them. Encountering flex-wrap issues when applying margin classes or styles directly to the cards. Any suggestions on how to handle this behavior effectively? Should I consider ad ...

Issue with JS jQuery: The click event on an li element within an expanded ul does not function properly

I have built a basic webpage to explore jQuery. However, there is an issue that arises when I click on the "Button: another one." It seems to interfere with the event of clicking on the li element, causing it to glitch and not respond. Here is the code: JS ...

Is there a way for me to enable autoplay on the Slice Slider?

I'm struggling to enable autoplay on a slider I have here. Is it possible to quickly set this up in the var settings? I attempted to insert the setInterval(spin, 3000); command before and after the init lines, but it hasn't been successful so fa ...

Unable to locate Element in Selenium using Python because of multiple HTML tags present

Hello everyone! I'm excited to be here as a newbie in the world of programming. Currently, I am delving into automation tasks using Selenium. Up until now, I've had no issues utilizing the find_element(By.ID,'') function and navigatin ...