<div> Make the background image transparent, but not the text

I am currently designing a <div> with a gradual panning transparent background as part of an HTML module in opencart. Since it is a module, all styling must be self-contained.

Here is what I have achieved so far:

<style>
@keyframes backgroundScroll {
0% { background-position: 50% 50%; }
33% { background-position: 1% 50%; }
40% { background-position: 1% 50%; }
66% { background-position: 99% 50%; }
75% { background-position: 99% 50%; }
100% { background-position: 50% 50%; }}
#mtn-scroll {
width: 800px;
height: 100%;
background: url(coolpanoramapicture.jpg) no-repeat;
opacity: 0.1;
background-size: 250%;
background-position: 50% 50%;
animation: backgroundScroll 60s ease-in-out 1s infinite; }
</style>

<div id="mtn-scroll">
  <div>
    Content text goes here!
  </div>
</div>

I want the opacity to only affect the background image and not the text. I have heard suggestions about using ::before, but I am unsure how to implement it. Any ideas?

Is there a way to make only the background image transparent without needing a transparent PNG file?

Answer №1

It seems you were heading in the right direction by using the ::before pseudo-element. This is how you can go about implementing it:

html,
body {
  height: 100%;
}

#mtn-scroll {
  width: 800px;
  height: 100%;
  position: relative;
}

#mtn-scroll::before {
  background: url('https://i.imgur.com/4HLnakJ.jpg') no-repeat;
  opacity: 0.1;
  background-size: 250%;
  background-position: 50% 50%;
  animation: backgroundScroll 60s ease-in-out 1s infinite;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
  content: '';
}

#mtn-scroll div {
  position: relative;
}

@keyframes backgroundScroll {
  0% {
    background-position: 50% 50%;
  }
  33% {
    background-position: 1% 50%;
  }
  40% {
    background-position: 1% 50%;
  }
  66% {
    background-position: 99% 50%;
  }
  75% {
    background-position: 99% 50%;
  }
  100% {
    background-position: 50% 50%;
  }
}
<div id="mtn-scroll">
  <div>
    Insert your content text here!
  </div>
</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

Troubleshooting: CSS causing images to not show up on

Struggling with a CSS issue, I can't seem to solve it. My images are not showing up on the webpage even though the URLs are correct and the HTML is properly written. There is no specific styling applied to the images, and the only styling for the ima ...

Instead of using <div>, try substituting it with a different HTML page

Seeking assistance on an HTML page project that involves jQuery, CSS, and HTML without server-side integration. Encountering an issue when attempting to replace a <div> with content from another HTML file saved on my computer. The main page code sni ...

Having issues with React Nivo tooltip functionality not functioning properly on specific graphs

While using Nivo ResponsivePie to visualize some data, everything is functioning properly except for the tooltip. For some reason, the tooltip isn't appearing as it should. Interestingly, I have a heatmap and a bar graph with tooltips that are working ...

When a user hovers over the image, a button is revealed

I have a test page on my website: If you'd like to check it out, feel free to visit: I am trying to create a feature where when you hover over an image, a black button with text and links will appear. When you move your mouse away from the image, ...

Troubleshooting Challenges in JavaScript/jQuery Hangman Game

Having trouble with my hangman game's game loop. Attempting to replace correct letters as the game runs through the word, but it's currently: looping through the word checking if the guessed letter is in the word returns index[0] Tried splitti ...

Do not incorporate a div in the overlay

I have an image inside a container. When hovering over the image/container, overlay information is displayed (which is currently working correctly). The overlay should cover the entire container (the grey part) where the image is located. The product-tit ...

Arrange the position of a div based on the location of another div

I'm working on a webpage that has 3 divs. The first one, labeled as "A," is positioned at the top of the screen, while the second one, labeled as "B," is centered on the screen. My goal is to always position the third div, labeled as "C," according t ...

What could be causing this table to exceed its dimensions by 2 pixels?

Why is the height of this table 102px instead of 100px? Is there another CSS attribute that needs to be set besides: table { border-spacing:0; border-collapse:collapse; } For example, check out this example link. ...

What is the best way to create a responsive-friendly centered navbar?

I have successfully centered my navbar, but it doesn't appear to be very responsive-friendly. Additionally, the hamburger menu is not showing up when I collapse the menu. Any tips or suggestions would be greatly appreciated! .navbar-header { heig ...

Is it possible to create pulsating text using only CSS3?

Would it be feasible to create a dynamic text effect that pulsates between green and blue colors in intervals without using any JavaScript? Perhaps utilizing SCSS or SASS shortcuts for efficiency? ...

Customizing the appearance of the browser's autocomplete feature as we input information into the form field

https://i.sstatic.net/p7PvH.png The image illustrates the issue of the background turning white when the browser autofills. However, I am seeking a solution similar to the transparent input below. ...

What causes the discrepancy in pixel size between these two web pages on the same device?

I am currently putting together a portfolio showcasing the projects I have worked on while learning to code. Each project page has a banner at the top, but I'm encountering an issue with one site (Jubilee Austen page) that is unresponsive. After usin ...

How to properly adjust image size in a flex-direction column layout - tips and solutions

My mission is to place an <img> with text blocks above and below it inside a container of specified height. The exact heights of the text blocks and image are unknown. After extensive searching, my colleague provided a useful solution by using the ...

Place an image in the middle of a div

Here are a few images for you to consider: <div class="builder-item-content row"> <div class="twelve columns b0" style="text-align:center;"> <ul class="upcoming-events"> <li> <a href="http://www.padi.com/scub ...

Issue with setting Y Axis intervals using ticks in d3 library

I am trying to set the Y Axis from 0 to 100 percent with intervals of 0-25-50-75-100. Despite setting ticks to 4, I am seeing Y axis intervals of 0-20-40-60-80-100 How can I adjust the Y axis to display intervals of 0-25-50-75-100? Here is the code I am ...

How to Dynamically Change the Background Color of an h1 Element using C# ASP

Is there a way to dynamically change the background color of my h1 header line based on the environment? For example, if the webpage is running in production or development mode, I want the background color of the H1 header tag to be adjusted accordingly. ...

Issues with Wamp server's CSS display are frustrating and need to

I have created a pair of divs in a .css file and then I connected that .css file in a php header file as shown below: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css/headercss.css" type="text/css" media="screen" title ...

Incorporated video content directly onto my website

Can anyone provide guidance on how to embed a video from the following site? I attempted to copy the link in view source mode, but the video address seems to change after a certain period of time. Would greatly appreciate any assistance with this issue. ...

Distributing information to modules made of Polymer

Is there a way to create a single source for all data that my elements can utilize but not change (one-way data-binding)? How can I achieve this without using a behavior? I attempted the following in my code: <script type="text/javascript" src="/data. ...

Is there a way to align both the horizontal and rotated lines to intersect at a common point in HTML and CSS?

As a beginner, I'm looking to create a structure resembling a thigh, leg, and ankle using thick lines. My aim is to rotate them with animation, ensuring that the joints between the lines mimic those of a knee joint and hip joint. Below is the code sn ...