Rectangular clipping with CSS3 animation and transitions

I have been working on a JSFiddle code where I am trying to apply a clip to an image using only CSS, but unfortunately, I have not had any success so far.

Initially, I attempted to place the image within an external div and apply the clip to that div, but once again, my efforts did not yield the desired result.

If anyone has any insights or ideas as to why this may be happening, I would greatly appreciate it!

Below is the snippet of CSS code:

img{
  width:500px;
  height 100px;
  top:10px;
  left:10px;
  -webkit-box-shadow: 10px 10px 5px 0px rgba(0,0,0,1);
  -moz-box-shadow: 10px 10px 5px 0px rgba(0,0,0,1);
  box-shadow: 10px 10px 5px 0px rgba(0,0,0,1);

}

#image{
   clip: rect(20px, 20px, 20px 20px);
}

Here is the corresponding HTML code:

<div id="image">
  <img src="http://www.menucool.com/slider/jsImgSlider/images/image-slider-2.jpg">
</div>

Answer №1

Important Note: The CSS clip property is deprecated and should be avoided.

The CSS clip property is designed for absolutely positioned elements, meaning you must position either the #image or the img element absolutely in order to apply the clip property.

It's crucial to remember that when using the rect function with clip, the values provided represent offsets from the top-left border edge of the box, forming a proper rectangle.

img {
  position: absolute;
  width: 500px;
  height 100px;
  top: 10px;
  left: 10px;
  -webkit-box-shadow: 10px 10px 5px 0px rgba(0, 0, 0, 1);
  -moz-box-shadow: 10px 10px 5px 0px rgba(0, 0, 0, 1);
  box-shadow: 10px 10px 5px 0px rgba(0, 0, 0, 1);
  clip: rect(0px, 20px, 20px, 0px);
}

#image {
  position: relative;
}
<div id="image">
  <img src="http://www.menucool.com/slider/jsImgSlider/images/image-slider-2.jpg">
</div>


The clip property, specifically when used as a rect, can be transitioned/animate as shown below.

img {
  position: absolute;
  width: 500px;
  height 100px;
  top: 10px;
  left: 10px;
  -webkit-box-shadow: 10px 10px 5px 0px rgba(0, 0, 0, 1);
  -moz-box-shadow: 10px 10px 5px 0px rgba(0, 0, 0, 1);
  box-shadow: 10px 10px 5px 0px rgba(0, 0, 0, 1);
  clip: rect(0px, 20px, 20px, 0px);
  transition: all 1s;
}

#image {
  position: relative;
}
#image:hover img {
  clip: rect(0px, 50px, 50px, 0px);
}
<div id="image">
  <img src="http://www.menucool.com/slider/jsImgSlider/images/image-slider-2.jpg">
</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

Tips for resizing all HTML elements when adjusting browser window dimensions

I am working with the following HTML code snippet: <div id="sectionOne" class="container-fluid d-flex flex-column justify-content-center align-items-center" style="height: 80vh;"> <div class="row d-flex justify-content-center" style="color: #00 ...

Sorry, but we are experiencing difficulties processing your request at the moment. HTTP ERROR 500: We are unable to locate the

My code seems to be functioning correctly when connecting to the database, but I'm encountering an error. Here is the code snippet: <?php include 'header.php'; ?> <!DOCTYPE html> <html> <head> <t ...

When using Tailwind CSS with flexbox, elements do not expand horizontally as expected

Is there a way to prevent the label from expanding along with the description in tailwindcss? <script src="https://cdn.tailwindcss.com"></script> <div class="flex items-center"> <div>block</div> <div class="m ...

Hamburger Icon in Bootstrap Navbar-Brand is not aligned as expected

I'm currently working on a website project using Bootstrap and have encountered an issue with the navbar component. When the screen shrinks to the breakpoint I've defined (lg), the alignment of the hamburger icon gets disrupted, as shown below. ...

Tips for keeping a video background stationary while allowing the text to move smoothly

When I added the video for the background, it only appears at the top of the page. However, I want it to be visible as the rest of the page is scrolled. <div class="hero"> <video autoplay loop muted plays-inline class="back-video&qu ...

Angular CSS Animation not functioning as expected

I have developed a system that retrieves stored messages from the server dynamically. The view for this system is structured as follows: <div id= "messages" data-ng-controller="MessageController"> <div class="message" data-ng-repeat="message ...

Tips for ensuring that the lightbox scrolling feature only affects the lightbox and not the entire page

Currently, I am utilizing the lightbox_me jQuery plugin to trigger a lightbox when a user clicks on a product. The issue I am facing is that the lightbox content often extends below the visible area, causing the entire page to scroll when using the right s ...

How to eliminate a hyperlink from an HTML element with the help of JQuery

Recently, I was assigned to revamp a website for the company I work for. However, upon closer inspection, I realized that the website is quite messy and relies heavily on templates, resulting in certain elements being auto-generated as active links. The i ...

The challenge of aligning widgets in bootstrap panels across different browsers

Incorporating angularjs and bootstrap3, I'm in search of the best way to center widgets within a panel and achieve responsive behavior. Interestingly, the results vary across browsers, particularly in Firefox. Within my code, I have 4 column divs str ...

Prevent tooltip text from appearing when a button is disabled in an angular application

As a beginner in UI development, I have a requirement for my Angular application. I need to enable and disable a button based on certain conditions. The tricky part is that I want to display a tooltip only when the button is enabled. I have managed to chan ...

Leveraging the adjacent sibling CSS combinator (+) alongside HgClass and varying values

I'm currently exploring the feasibility of a specific idea, and I'm wondering if there's a better approach I should consider. Here's a basic representation of where I am My primary list is constantly being modified by other processes ...

Compel the browser to initiate a reflow by adjusting the CSS

I am currently in the process of building a responsive image slider without relying on jQuery, using CSS3 transitions. The setup is quite simple: there's a viewport with a UL inside, containing relatively positioned LIs that are left floated. Howeve ...

Thymeleaf: Automating the Adjustment of Content Fragments

In my project using Spring MVC and Thymeleaf, there is a default fragment that sets up the page structure. Here's what it looks like: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/ ...

Expanding the Width of Bootstrap 4 Dropdown Button

Currently, I am utilizing Bootstrap 4's dropdown feature. Is there a way to modify the code below so that the dropdown menu that appears upon clicking the button matches the same width as the button itself? It's important to note that the button ...

Preventing Scrollable Div Content from Displaying Behind a Fixed Div using CSS

Seeking help with a message_container division that contains a fixed scrollable message_header division and a scrollable message_body division. The issue I am facing is that the content of the message_body division is appearing behind the fixed div. How ca ...

Resetting text in an input field using CSS

Here is the fiddle link for reference: http://jsfiddle.net/a765q/1/. I noticed that when I click the reset button, the text color changes to grey. Any ideas on how to fix this? ...

Transition smoothly between HTML pages by using a script to fade in and out while changing images from an array

I am facing a small issue. I have successfully implemented a fadeIn and fadeOut effect on an entire page by clicking HTML links. However, I now want to dynamically change the images associated with these links using an array. Here is the code for transiti ...

What is the best way to make a linear gradient that spans the entire vertical space that is visible (or shifts to a solid color)?

I'm trying to figure out how to apply a linear gradient to the body element of my webpage. Here's the CSS code I've been using: body { background: linear-gradient(0deg, white, lightgray); } The issue I'm facing is that the gradien ...

Table with a fixed header and scrolling body in Bootstrap 4

https://i.sstatic.net/YasoT.pnghttps://i.sstatic.net/ubmHZ.png I'm new to the world of front-end development and I'm currently working on a table layout. Here's the code snippet of the table I've created: <div className="table ...

Froala text area is unexpectedly visible when I attempt to cover it with a partially see-through mask

The website I'm currently developing features a semi-transparent overlay that dims the screen with a light-colored message displayed on top. This overlay and message are shown whenever there is a background process running. Everything was running smoo ...