Parallax enchantment with CSS clip masking

I'm trying to apply a clip mask to an image in order to prevent it from overlapping with its background, but I'm not exactly sure how to achieve this. I attempted using the background-clip property with box/padding values, however, it did not produce the desired effect.

Furthermore, my attempt at utilizing the clip: rect(); function seemed ineffective, leading me to wonder if I am executing it incorrectly.

Here is a link to the code snippet on JSFiddle

 <div class="paralax">
        <div class="fast"></div>
        <div class="medium"></div>
        <div class="slow"></div>
    </div>

.paralax{
width: 100%;
height: 306px;
background: #ccc;
position: relative;
display: block;
clear: both;
}

.fast{
    background: url(http://s13.postimg.org/v7rm6cidj/fast.png);
    width: 100%;
   height: 193px;
}
.medium{
    background: url(http://s7.postimg.org/ae6yxfd4b/medium.png);
    width: 100%;
    height: 510px;
    clip: rect(0px, 0px, 100px, 0px);
}

Answer №1

To achieve the desired effect, you should apply either absolute or fixed positioning to the object. Then, expand the left side of the rectangle by adjusting its second position as shown below:

.parallax {
  width: 100%;
  height: 306px;
  background: #ccc;
  position: relative;
  display: block;
  clear: both;
}
.medium {
  background: url(http://s7.postimg.org/ae6yxfd4b/medium.png);
  position: absolute;
  width: 100%;
  height: 510px;
  clip: rect(0px, 1025px, 100px, 0px);
}
<div class="parallax">
  <div class="medium"></div>
</div>

It's worth noting that although clip is becoming outdated, it still functions in many browsers. Its successor, clip-path, may not be supported universally.

Furthermore, when specifying parameters for rect, remember that they represent distances from the Top-Left corner, with values listed in the order of Top, Right, Bottom, Left. This can be confusing, especially since some resources fail to explain this aspect accurately.

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

The JSON Stringify function is absent in the QA environment, however, it is present

Linked (yet distinct): Is JSON.stringify() supported by IE 8? Also connected (but with a different IE version): JSON object undefined in Internet Explorer 8 Recently, while collaborating with our QA team to test a feature, we encountered an issue. The fe ...

Troubleshooting problem with Kendo UI Chart Tooltips

Our Kendo UI DataViz chart has a feature that displays a tooltip when hovering over a bar on the chart. When testing this on mobile, the tooltip appears on touch, which is expected. However, after multiple touches, the application freezes. Is there a solu ...

The openlayers 3 function ol.proj.transform seems to be mysteriously turning live geolocation data into a string

I came across an interesting solution on stackoverflow for dynamically inserting data into a map using OpenLayers 3.*. Everything seems to be working fine when I directly input the numbers like this: geometry: new ol.geom.Point(ol.proj.transform([-72.07 ...

What kind of mischief can be wreaked by a malicious individual using JavaScript?

My mind has been consumed by thoughts about the safety of my projects, especially when it comes to password recovery. On the password recovery page, users must fill out a form with valid data and complete a recaptcha test for security. To enhance user ex ...

I'm currently working with ReactJS and attempting to retrieve JSON data from a REST API in JIRA, but I'm facing challenges in achieving this

I've been struggling for hours trying to understand why I am unable to access and transfer data in my array from the JSON data in JIRA using the REST API. Basically, I am attempting to retrieve the JSON data from the JIRA website via URL with Basic Au ...

The setInterval function is malfunctioning

If I have the following function: function check(){ alert("Welcome"); } window.onload = check(); setInterval("check();", 5000); However, it is not working properly. Oddly enough, when I refresh the page, it works as intended. How can I resolve this i ...

Error occurred in ajax syntax

I found this code online and modified it to suit my needs, but I keep getting a syntax error $(document).ready(function(){ $address = localStorage.getItem('email_address'); $email_field = $('#email_field') if ($address !== null) ...

Display added images upon page refresh

I am adding images to a webpage and have included checkboxes to show or hide the added items. The data is stored in a JSON array, but I need the appended images to remain visible even after reloading the page tab. ...

updating the name of an HTML element with jQuery

Encountering a problem with setting the class for each li in a group of li tags: function pathContents(fileList) { var $list = $('<ul/>'); $.each(fileList, function (_, file) { $('<li/>').prop('class', &apos ...

Troubleshooting Display Problems when Switching Bootstrap Themes in ASP.NET MVC

I recently made changes to my MVC project by switching the default Bootstrap theme to Bootswatch - Cosmos. However, I am facing an issue with the Navbar as shown in the image below: View Header Display Issue Image Initially, I was using Bootstrap 3.0 and ...

Transform a personalized Google map into a visual representation

I am facing a challenge with the following issue. I have implemented a custom overlay on a Google map and it works perfectly in the browser. However, my dilemma arises when I need to display an image with a marker for each point of interest on a different ...

Tips for ensuring browsers maintain the aspect ratio specified by width and height HTML attributes when CSS is applied

I am seeking a method to reserve space in my layout for images before they are loaded by adding width and height properties to the img element. This approach functions as anticipated: img { display: block; border: 1px solid tomato; } <img src="" ...

Encountering issues with reading undefined properties within azure-maps-animations

Attempting to reimplement the Azure Maps Animations example with TypeScript has been a challenging task for me. Here's the link to the sample: I'm encountering several issues and could really use some assistance. I'll describe my problems a ...

How can I deactivate the today button in the angular-ui bootstrap datepicker popup?

Currently, I am working with two dates: StartDate and EndDate which are being managed using the angular-ui-bootstrap datepicker. When a user selects a StartDate (which must be greater than today's date), I want to ensure that the min-date of the EndD ...

Issue with jQuery: Function not triggered by value selection in dynamically created select boxes

I am in need of some assistance with my code that dynamically generates select drop down menus. I want each menu to trigger a different function when an option is selected using the "onchange" event, but I am struggling to implement this feature. Any hel ...

Socket connection in Vue not activating

I've been experimenting with incorporating Vue.js and sockets together, but I'm encountering an issue where my Vue application isn't receiving socket events. Despite following online tutorials, the example provided doesn't seem to work ...

CSS Only flyout navigation - reveal/hide with a tap or click

I want to make adjustments to a CSS-only menu that has a horizontal flyout effect triggered by hovering. I am looking to achieve the same effect on touch or tap - meaning, one tap to open the menu and another tap to close it. Is it possible to accomplish ...

Issue with Vue-Google-Charts rendering calendar chart in Vue framework

I am trying to utilize the vue-google-charts package for displaying Google charts, but I'm encountering difficulties when attempting to incorporate the Calendar chart. This is how my HTML template looks: <GChart type="CalendarChart" :data="ch ...

Collapse the stacked navigation menu upon clicking a new item, utilizing Bootstrap 4 features

One issue I am facing is with my layered navigation setup. https://i.sstatic.net/xyz123.png Whenever I click on two items that have children, both of them remain open causing the menu to get a scrollbar. However, I do not want to hide the scrollbar. htt ...

Having trouble getting my image and text to align properly in two columns

<img id="story_image" src="images/builder_story_side.png" alt="photo"/> <div id="story_right"> <h2 class="story_header">fdgdfgdfgdfgdfgfdgdfgfdgdfgdfgdfgdfgdfgdfgdfgdfgdfgfdgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfg</h2> <p ...