Determine the location of an object that has been both translated and reduced in size

I am currently in the process of creating a unique "3D carousel" from scratch.

This carousel is designed to hold 3 slides that are stacked on top of each other using CSS Grid, all with the same grid-area: 1/1 property.

The first slide remains positioned at the center of the carousel.

To achieve a basic 3D effect, I utilize a translate function to shift slides 2 (to the left) and 3 (to the right).

My goal is to have the container of the carousel perfectly fit the content (the 3 slides), hence I establish padding on the left and right sides of the carousel to compensate for any overflow caused by slides 2 and 3.

Currently, the amount of padding matches the offset of slides 2 and 3. (For example, if I translateX(-100px) slide 2, then a padding-left: 100px works effectively.

However, things become more challenging when I apply a scale(0.5) transformation to slides 2 and 3: the padding value becomes inaccurate. Although I can manually adjust the padding to find a suitable value, I cannot seem to discover a formula that consistently accommodates changes in the main slide width, offset, or scaling factor.

I have provided a Codepen demonstration to highlight this issue, especially occurring at resolutions below 1024px (where you need to resize the window to observe the problem).

Visit the Codepen demo here

window.onload = function (event) {
  document.querySelector("span").innerHTML = window.innerWidth + 'px'
};

window.onresize = function (event) {
  document.querySelector("span").innerHTML = window.innerWidth + 'px';
};
* {
  box-sizing: border-box;
}

div {
  --offset: 100px;
  --scaling: 0.75;
  --width: 200px;
  --padding: calc(
    var(--width) / 2 * var(--scaling)
  );
  border: 1px dashed black;
  background: lightgrey;
  display: inline-flex;
  width: var(--width);
  padding: 0 var(--padding);
  box-sizing: content-box;
}
div ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
}
@media screen and (max-width: 1025px) {
  div ul {
    --offset: 75px;
    --scaling: 0.75;
    --width: 150px;
  }
}
div ul li {
  grid-area: 1/1;
  border: 1px solid black;
  width: 200px;
  height: 200px;
}
div ul li:nth-child(1) {
  background-color: red;
  z-index: 1;
}
div ul li:nth-child(2) {
  background-color: blue;
  transform: translateX(var(--offset)) scale(var(--scaling));
}
div ul li:nth-child(3) {
  background-color: green;
  transform: translateX(calc(var(--offset) * -1)) scale(var(--scaling));
}
<div>
  <ul>
    <li>Card 1</li>
    <li>Card 2</li>
    <li>Card 3</li>
  </ul>
</div>
<p>At less than 1024px resolution wide, padding is not correctly calculated (width of the Card 1 changes, offset of Cards 2 and 3 too.)</p>
<p>The padding (lightgrey zone) should <strong>NOT</strong> be visible at the left / right side under 1024 pixels </p>
<p> Current viewport width: <span></span> </p>

Here is a list of manual padding values that align perfectly with corresponding width, offset, and scaling variables. Unfortunately, I have been unable to identify a universal formula connecting these values. Please note that the values may be rounded (e.g., 68px = 67.5px).

//     width(px)  offset(px)  scaling        padding(px)
//        150       50          0.50        =    12
//        300      100          0.50        =    25
//        200      100          0.50        =    50
//        150      100          0.50        =    62
//        350      150          0.50        =    62
//        250      100          0.75        =    68
//        300      175          0.50        =   100
//        300      200          0.50        =   125
//        300      200          0.75        =   162

If further details are required, please do not hesitate to reach out. Enjoy experimenting!

Answer №1

The equation must read as follows:

--padding: calc(var(--offset) - var(--width)*(1 - var(--scaling))/2)

Start by subtracting the difference between the unscaled element and the scaled one (1 - var(--scaling)), then divide by 2 since we are only adjusting one side.

Remember to update the variables on the div, not the ul, because the padding and width are set in the div.

window.onload = function (event) {
  document.querySelector("span").innerHTML = window.innerWidth + 'px'
};

window.onresize = function (event) {
  document.querySelector("span").innerHTML = window.innerWidth + 'px';
};
* {
  box-sizing: border-box;
}

div {
  --offset: 100px;
  --scaling: 0.75;
  --width: 200px;
  --padding: calc(var(--offset) - var(--width)*(1 - var(--scaling))/2);
  border: 1px dashed black;
  background: lightgrey;
  display: inline-flex;
  width: var(--width);
  padding: 0 var(--padding);
  box-sizing: content-box;
}
div ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  width: 100%;
}
@media screen and (max-width: 1025px) {
  div {
    --offset: 75px;
    --scaling: 0.75;
    --width: 150px;
  }
}
div ul li {
  grid-area: 1/1;
  border: 1px solid black;
  aspect-ratio:1;
}
div ul li:nth-child(1) {
  background-color: red;
  z-index: 1;
}
div ul li:nth-child(2) {
  background-color: blue;
  transform: translateX(var(--offset)) scale(var(--scaling));
}
div ul li:nth-child(3) {
  background-color: green;
  transform: translateX(calc(var(--offset) * -1)) scale(var(--scaling));
}
<div>
  <ul>
    <li>Card 1</li>
    <li>Card 2</li>
    <li>Card 3</li>
  </ul>
</div>
<p>At less than 1024px resolution wide, padding is not correctly calculated (width of the Card 1 changes, offset of Cards 2 and 3 too.)</p>
<p>The padding (lightgrey zone) should <strong>NOT</strong> be visible at the left / right side under 1024 pixels </p>
<p> Current viewport width: <span></span> </p>

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

Unable to retrieve background position using jQuery .css() without needing !important in CSS

Could someone please explain why the jQuery .css() method is returning 0% 0% for elements with a background position defined in the inspector and style sheet? I'm puzzled as to why adding !important to the rule makes it work, but not without. The jQ ...

Implementing full-height list elements using CSS

I'm still facing challenges with my lists... After receiving some assistance, I was able to create a horizontal list with perfect dimensions for my needs: each element taking up 33.33% width and adjusting its height based on the tallest element. I ac ...

Can the navbar hamburger in Bootstrap be displayed at a screen size of 992px?

How can I display the bootstrap navbar Hamburger at md(992px) Screen? Here is the code snippet I am currently using: https://i.sstatic.net/OqBTQ.png Any suggestions on what steps I should take? ...

What is the best way to inform the DOM about newly generated elements dynamically?

When making an AJAX call and generating HTML on the backend side, the result may not display with the desired properties such as cursor styling. For example, using jQuery to render JSON data: $(data.message).each(function (index, value) { $('#sta ...

Bring your images to life with a captivating 3D hover effect

I am looking to achieve a similar effect using JavaScript instead of just pure CSS like the example provided. I'd prefer not to use SCSS either, just sticking to CSS would be great. Please check out this CodePen for reference. .picture-container ...

What is the best way to align HTML elements in a single row?

I have the following code snippet... <div class="header"> <div class="mainh"> <div class="table"> <ul> <li><a>smth</a></li> ...

Creating a personalized button using Bootstrap

Struggling to create a unique custom button in Twitter Bootstrap 3. I am trying to design a button with a triangle in the right bottom corner like this: https://i.sstatic.net/IZAv0.jpg I have considered using the background of the button and adding text, ...

Leveraging a Keyframes Definition from an External Source

Font Awesome has a special spin animation for elements that have the class fa-spin. Taking a closer look at the CSS code, you can see that the spinning effect is achieved through keyframes under the same name, fa-spin. Instead of creating a new animation ...

The border in my HTML table is not displaying when I run my flask application

My flask application was running smoothly in a virtual environment until I encountered an issue with the html borders not showing up. Even after seeking help from a friend who knows some html, the problem still persists. app.py: from flask import Flask, r ...

Creating a dynamic animation for a navigation bar's underline

I'm attempting to add a smooth underline effect to the links in my navbar when they are hovered over. Unfortunately, the entire ul element is being underlined instead of just the selected link. Is there a way to resolve this issue? a { text-deco ...

Maintaining the original size of the fill texture when resizing a path in SVG

I have created an SVG pattern using the following code: <svg height="10" width="10" xmlns="http://www.w3.org/2000/svg" version="1.1"> <defs> <pattern id="circles-1_4" patternUnits="userSpaceOnUse" width="10" height="10"> & ...

Is there a way to change OTF/TTF file formats into EOT?

Is there a simpler way to convert my OTF/TTF fonts to EOT format for Microsoft browsers' @font-face feature? I attempted using the WEFT tool but without success. Any alternative methods available? ...

Unexpected behavior with CSS background-image transitions effects

Currently, I am experimenting with a hover swipe effect using div and background image. The background image utilizes the outer div's max-width and an inner div with a padding-bottom percentage to maintain the aspect ratio. However, there are several ...

Is JavaScript generating a random sequence by dynamically adding fields?

I'm encountering an issue with my button that adds a set of text fields every time I click on the Add More button. The problem is that when I add new text fields, they are appended above the Add More button. Then, pressing the button again adds more t ...

The most effective method for verifying the symmetry of brackets in an arithmetic string

Can someone help me verify the validity of a given arithmetic string like this: (a[i]+{-1}*(8-9)) In this context, "valid" means a string that contains properly matched brackets, such as: ([]{}()) An example of an invalid string would be: {([[})) ...

What might be preventing me from achieving a full-length page using height 100% or height 100vh?

I am currently working on a web application that has a similar layout to Slack. I am facing an issue where the page doesn't take up the full width and height as intended. The problem seems to be that it sometimes only occupies half of the screen while ...

Using a variable with the :contains selector

function checkAndUpdateStatus(newName) { $('#myTable').children('tr').remove(":contains('" + newName + "')"); }; The function above is attempting to remove table rows in 'myTable' containing a string matching the ...

Display issue with positioning user name in navbar on Internet Explorer

The user name appears correctly in the navbar on Firefox and Chrome, but in IE it drops down due to a break tag issue. Is there a way to resolve this in IE so that it displays like in Firefox and Chrome? body { padding-top: 102px; background-colo ...

Can a submit button be created that integrates both a radio button and submission functionality?

Is there a way to combine a radio button and submit button into one element for submitting the value just with a click? Currently, I have a radio button and a separate submit button in my code, but I want to streamline it. This is the current code snippet ...

Is it possible to swap two classes with each other using jQuery?

I am trying to implement a tree view with the following code snippet. Within the tree view, there are spans that have either the CollOpen or CollClosed class assigned to them. How can I toggle between these two classes on click for each span? .tree ...