Issue with cutting of rotating pseudo element

Trying to rotate a pseudo element with a background-image is posing a challenge. The background, positioned at the center of the main element for visual effect, ends up being cut off when rotated.

An illustrative example has been created using random images to demonstrate the issue at hand. Is there a way to rotate the background without it being cut off? Any insight on why this approach is causing the cut-off would be greatly appreciated.

Thank you.

$(document).ready(function () {
'use strict';
$('button').on('click', () => {
$("li").addClass('issue-simulation');
})
});
ul {
  display: flex;
  list-style: none;
  padding: 0;
}
ul li {
  position: relative;
  display: flex;
  width: 200px;
  height: 200px;
}
ul li:after {
  content: '';
  position: absolute;
  width: 200px;
  height: 200px;
  background-image: url(https://picsum.photos/180/180);
  background-size: 80%;
  background-repeat: no-repeat;
  background-position: center;
  transition: 650ms cubic-bezier(0.5, 0, 0.1, 1);
}
ul li:hover:after {
  transform: rotate(-10deg);
}
ul li.issue-simulation:after {
  background-position: calc(50% + 50px) center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>
    <img src="https://picsum.photos/200/200" alt="">
  </li>
</ul>
<button>Simulate issue</button>

Answer №1

The core of your problem lies in the manipulation of the background-position. Attempting to make a background image overflow its container is not a feasible task and cannot be bypassed.

It seems like you might be facing an XY issue, possibly due to complications with compositing or combining transforms. Trying to achieve something like this will not yield the desired results:

ul li:hover:after {
  transform: rotate(-10deg);
}
ul li.issue-simulation:after {
  transform: translateX(50px);
}

This is because the transform property does not support additive behavior, meaning you cannot combine multiple transform properties.

The recommended approach is to utilize CSS variables:

  1. Define the base appearance using CSS variables:

    :root {
      --rotate: 0deg;
      --translateX: 0;
    }
    
  2. Implement these variables in the styling of the pseudo-element:

    ul li:after {
      transform: translateX(var(--translateX)) rotate(var(--rotate));
    }
    
  3. Subsequently, manipulate these CSS variables using JavaScript:

    const root = document.documentElement;
    
     $('ul li')
       .on('mouseover', () => root.style.setProperty('--rotate', '-10deg'))
       .on('mouseout', () => root.style.setProperty('--rotate', '0deg'));
    
     $('button').on('click', () => root.style.setProperty('--translateX', '50px'))
    

$(document).ready(function() {
  const root = document.documentElement;

  $('ul li')
    .on('mouseover', () => root.style.setProperty('--rotate', '-10deg'))
    .on('mouseout', () => root.style.setProperty('--rotate', '0deg'));

  $('button').on('click', () => root.style.setProperty('--translateX', '50px'))
});
/* CSS variables */

:root {
  --rotate: 0deg;
  --translateX: 0;
}

ul {
  display: flex;
  list-style: none;
  padding: 0;
}

ul li {
  position: relative;
  display: flex;
  width: 200px;
  height: 200px;
}

ul li::after {
  content: '';
  position: absolute;
  width: 200px;
  height: 200px;
  background-image: url(https://picsum.photos/180/180);
  background-size: 80%;
  background-repeat: no-repeat;
  background-position: center;
  transition: 650ms cubic-bezier(0.5, 0, 0.1, 1);
  /* Apply `transform` to the actual pseudo-element */
  transform: translateX(var(--translateX)) rotate(var(--rotate));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>
    <img src="https://picsum.photos/200/200" alt="">
  </li>
</ul>
<button>Simulate issue</button>

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

Maintaining active navigation state in JQuery/JavaScript after clicking a link: tips and tricks

While many resources discuss adding the active class to a nav link using jquery, there is less information on maintaining the active state after the nav link has been clicked. After experimenting with code from various sources, I have attempted to set ses ...

Elements vanish when SHAKE effect is in use

I've been experimenting with this framework and I'm struggling to get the shaking effect to work properly. Whenever I hover over an element, other divs seem to disappear. I tried using different versions of JQuery and JQuery UI on JSFiddle, and i ...

Is there a way to display a delivery estimate underneath the product title on a WooCommerce website?

How can I display Estimated delivery days below the product title to inform my customers about their order's arrival date, similar to the example on the page included in the link? I would like it to show varying days depending on the shipping class. ...

Picture in the form of a radio button

Is there a way to use an image instead of a radio button in my AngularJS form? I have managed to hide the radio button using CSS, but it disables the checked event. How can I make the image clickable? position: absolute; left: -9999px; Here is the code s ...

What's causing the text not to wrap when using float?

I've tried setting float:left for the image and float:right for the text, but the image is still overlapping the text. What I really want is for the text to wrap around the image - to be on the right side of the image at the beginning and below the i ...

Issues arising from the arrangement of the menu bar

I created a navigation bar using an unordered list (ul) with list items (li). However, I encountered an issue where the items were displaying in reverse order until I applied the following CSS: .nav-bar .btn{ float: left; } .nav-bar u ...

HTML: arranged <pre> with fixed positioning

I currently have a centered column of text with a fixed width. However, I am looking to break that fixed width for certain tags like <pre> so that they can fill the full screen width while maintaining flow with the rest of the text. My CSS snippet s ...

The powers of jQuery and CSS selectors

Previously, I utilized the following code to extract text from the data-placeholder attribute and apply it as a placeholder for my div. [contentEditable=true]:empty:not(:focus):before { content:attr(data-placeholder) } While this method worked well b ...

Setting the maximum height for a tr or td element that is compatible with Chrome

Is there a way to set the max-height for a tr or td element that is specifically supported in Chrome? Previously, an answer suggested at this link did not work for Chrome: How to fix height of TR? ...

Guide on transferring href parameter from the parent Vue component to the child component

Hey there! I've been working on creating a Vue page, breaking it down into components, and populating it with content from json. It all seems pretty straightforward, but I've hit a snag. Why is the href parameter not showing up in the right place ...

Using JQuery to create animations with fadeIn and fadeOut effects is a great way to

As a complete newbie taking my first steps in front-end development, I spent most of my day trying to work out an animation function but couldn't quite get it right. Below are the snippets of HTML, CSS, and JavaScript codes: <!DOCTYPE html> < ...

Attempting to create a multi-page slider using a combination of CSS and JavaScript

Looking for help with creating a slider effect that is triggered when navigating to page 2. Ideally, the div should expand with a width of 200% or similar. Any assistance would be greatly appreciated! http://jsfiddle.net/juxzg6fn/ <html> <head& ...

Utilizing scrapy and python to gather information on attractions from Tripadvisor

Struggling to extract the names and addresses of attractions from TripAdvisor using web scraping. The suspicion is that there might be an issue with how product.css(...) is written (possibly related to jsons?). Seeking guidance on correcting the code in o ...

Failure to respond to dual part form by RemoveClass

Currently, I am utilizing jQuery to visually control a form. The issue arises when trying to remove classes from the first part of the form using removeClass. While this works initially, upon clicking the button to advance to the second part of the form, t ...

Incorporate a class into a link within the wp_list_pages function

I've been experimenting with creating a hover effect on list items using the wp_list_pages() function in my Wordpress theme. Despite my efforts, I've hit a roadblock in getting the hover effect to work properly. As a beginner in web development, ...

"Utilizing images within an iframe: A step-by-step guide

I'm trying to integrate an iframe into a phone image with a transparent background. However, I am unsure how to effectively mask the iframe so that it only appears within the boundaries of the phone image. .phone { display: block; position: r ...

The placement of my footer is off and not aligned with the bottom of the page

I am struggling to make my footer align with the bottom of the page rather than sticking to the bottom of the browser window and moving along with the scroll. How can I fix this issue? The goal is to have the footer remain out of sight at the bottom of th ...

Animate a div once the parent section becomes visible while scrolling

I previously had this function working, but it seems that I have somehow messed it up in the process. My current setup includes a scroll hijack feature that navigates users to a new section or card each time they scroll. The script adds a visible class w ...

ngAnimate - Animation not activating on recurring custom directive

I am trying to implement ngAnimate on a custom directive that is repeated using ng-repeat in my AngularJS application. The animations function correctly when I use ng-repeat on a simple HTML list-item. However, when I replace the list-item with a custom di ...

Is there a way to exclusively utilize CSS in order to achieve bottom alignment for this btn-group?

I currently have a series of div elements that I transformed into two columns. https://i.stack.imgur.com/xG7zT.png My goal is to align the PDF/XML button group at the bottom, creating a layout like this: https://i.stack.imgur.com/ijtuH.png This is an e ...