Include an additional feature to an already established design

I have been attempting to move an element to the left using the transform property. However, there is already a pre-existing style with transform, which appears as:

transform: translate3d(tx, ty, tz)

What I am aiming for is to incorporate another property alongside translate3d(tx, ty, tz), known as translateX(tx). Therefore, the complete style would be:

transform: translate3d(tx, ty, tz) translateX(tx)

Is it possible to achieve this in CSS without overwriting the current style? My project uses Angular, so should I make changes in the component code? Any guidance on this matter is highly appreciated!

EDIT: I would like to clarify that the translate3d style is already applied to the element by default (not in my own css file, but set automatically by a library). What I am trying to understand is how to retrieve the current style, which is transform: translate3d, and add the translateX style to it as well, resulting in:

transform: translate3d(tx, ty, tz) translateX(tx)

Once again, I want to emphasize that I did not define the translate3d in my css file; it is a style that is automatically set for that specific element due to the library being used. For reference, the library in question is Leaflet for Angular

Answer №1

To create a unique effect, you can utilize the power of relative positioning in CSS by using these properties:

  • position: relative
  • left: [tx]

Here is an example to demonstrate how this works:

.my-heading-a,
.my-heading-b {
  position: relative;
  transform: translate3d(40px, 20px, 10px);
}
.my-heading-a {
  left: 0;
}
.my-heading-b {
  left: 80px;
}
<h2 class="my-heading-a">My Heading A</h2>
<h2 class="my-heading-b">My Heading B</h2>

Answer №2

Update 2

This example now directly modifies the x value of the translate3d property, removing the need for custom variables. It should be noted that the getTransform function used here was mostly copied from a different post on Stack Overflow.

const myEl = document.querySelector(".myElement");
const MOD = 10;

const getTransform = el => {
  var transform = window.getComputedStyle(el, null).getPropertyValue('-webkit-transform');
  var results = transform.match(/matrix(?:(3d)\(-{0,1}\d+(?:, -{0,1}\d+)*(?:, (-{0,1}\d+))(?:, (-{0,1}\d+))(?:, (-{0,1}\d+)), -{0,1}\d+\)|\(-{0,1}\d+(?:, -{0,1}\d+)*(?:, (-{0,1}\d+))(?:, (-{0,1}\d+))\))/);

  if (!results) return [0, 0, 0];
  if (results[1] == '3d') return results.slice(2, 5);

  results.push(0);
  return results.slice(5, 8);
}

document.querySelector(".button").addEventListener("click", () => {
  const [x, y, z] = getTransform(myEl);
  const newX = parseInt(x, 10) + MOD;
  myEl.style.transform = `translate3d(${newX}px, ${y}, ${z})`;
});
.myElement {
  transform: translate3d(100px, 0, 0);
  display: inline-block;
  padding: 1rem;
  background-color: tomato;
  color: white;
  transition: 0.3s transform;
}

.button {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: gray;
  padding: .5rem 0;
  color: white;
  font-size: 1.4rem;
}
<div class="myElement">Test</div>
<button class="button">Move it</button>


Update

If you want to add 10px to the original x translation in the translate3d, this code snippet will achieve that:

/* 200px + 10px = 210px x translation */
transform: translate3d(200px, 0, 0) translateX(10px);

div {
  background-color: red;
  padding: 1rem 2rem;
  display: inline-block;
  transform: translate3d(200px, 0, 0) translateX(10px);
}
<div></div>


Original Answer

Instead of using the pattern below:

transform: translate3d(tx, ty, tz) translateX(tx);

You can set up the translate3d properties with custom properties at the beginning and then update each individual value as needed:

transform: translate3d(var(--tx), var(--ty), var(--tz));

In the provided example, custom properties are updated to move the box downwards and to the right. This approach provides full control over the changes made without overwriting existing styles.

document.querySelector(".button").addEventListener("click", () => {
  const style = document.documentElement.style;
  style.setProperty("--tx", "120%");
  style.setProperty("--ty", "80px");
});
:root {
  --tx: 100%;
  --ty: 40px;
  --tz: 0;
}

.myElement {
  transform: translate3d(var(--tx), var(--ty), var(--tz));
  display: inline-block;
  padding: 1rem;
  background-color: tomato;
  color: white;
  transition: 0.3s transform;
}

.button {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: gray;
  padding: .5rem 0;
  color: white;
  font-size: 1.4rem;
}
<div class="myElement">Test</div>
<button class="button">Move it</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

A step-by-step guide on showcasing database data on an HTML page using jQuery

I am currently using jQuery to make an HTTP GET request and fetch JSON data from a database. After executing the request, I received the JSON array successfully. Now, I am attempting to display this JSON data on an HTML page by using the jQuery $(newData ...

Ways to pass scope between the same controller multiple times

There is a unique scenario in which I have a controller appearing in 2 different locations on a page. This arrangement is necessary for specific reasons. To illustrate, the simplified HTML structure is as follows: <aside ng-if="aside.on" ng-controller ...

I am finding that the text I am creating using context.fillText keeps getting distorted within the canvas

I am attempting to place text inside a canvas element. The HTML markup for the canvas is as follows: <canvas id="leaderboard2" style="position: fixed; right: 1250px; top: 140px; width: 230px; height: 330px;"></canvas>. Des ...

Enhancing images by creating clickable sections in a more organized and efficient manner

Are there any other methods to make parts of an image clickable in a more organized way, aside from using the coordinates method or directly embedding images from Photoshop? <script> new el_teacher = ["candice","john"]; $(".teacher1").mouseenter(fu ...

What is the best method for positioning two forms in a vertical arrangement?

I'm looking to integrate a Login form in HTML within my C# project. Here's what I have so far: However, as you can see, the layout is not very aesthetically pleasing. I'm struggling to align the elements in a more visually appealing way. My ...

Displaying PHP output within a JavaScript expression

Let's dive into a scenario involving a basic HTML document and some JavaScript that's loaded within it: <!-- doc.html --> <!doctype html> <html lang="en"> <head> <script type="text/javascript" src=" ...

Avoiding scrolling when a button (enveloped in <Link> from react-scroll) is pressed in React

Currently, I am implementing the smooth scroll effect on a component using react-scroll. However, adding a button inside the component to create a favorite list has caused an issue where the smooth scroll effect is triggered upon clicking the button. Is ...

Scaling boxes responsively according to screen size utilizing Bootstrap

Creating a portfolio website with a carousel section to display completed projects in a 2x2 grid layout, transitioning to a 1x4 on smaller screens is proving to be quite challenging. After trying various methods from Bootstrap documentation and YouTube tut ...

What is the function of table widths in accommodating wider details during insertion?

What happens when a detail width is set to <td width="10"...> and something wider, like a picture that is 20 pixels wide, is placed in that detail? ...

Discrepancy in the vertical pattern repetition of the SVG background

When I use a simple div with an SVG set as a background image with vertical repeat, I noticed a gap of varying sizes on Chrome and Firefox depending on the screen size (please resize the window). Check out the issue here .bg { width: 50%; height: 2 ...

Issues with Jquery Div sliding transitions from right to left are not functioning correctly

Creating page transitions in jQuery similar to "http://support.microsoft.com/." Encountering an issue where after the page transitions are completed, they start from the left instead of the expected right direction. Referencing this fiddle (Working Code) ...

Tips for inserting HTML content into a Flex TextArea

There was once a panel where I had added a fadeIn show effect. Initially, the effect only applied to the panel itself, with text appearing without the desired fadeIn effect. Fortunately, a colleague suggested embedding the fonts, which miraculously solved ...

Issues with inconsistent behavior of the select option in a form

Having some trouble with my form code. When I checked the web page, there seems to be an extra element that shouldn't be there. You can view the webpage https://i.sstatic.net/pu4ef.jpg. Inspecting it in browser developer mode, I found this extra eleme ...

Display the GIF when the mouse hovers over it, but ensure it plays just a single time

Is it possible to make a GIF only play once when you hover over it on a website? I want to avoid the animation repeating if the user hovers over it again without refreshing the page. Currently, I have a static image that changes to a gif on mouseover, but ...

Removing a outside div element based on a dropdown change in ASP.NET jQuery

this is the structure of my unique div <div class="row-fluid"> <div class="span12"> <div style="overflow: auto; width: 90%;" class="goleft"> <div style="display: inline-block; width: 200px;"> ...

Styling using CSS is effective only when applied locally, as it does not function properly on GitHub Pages

Although I have come across similar questions to the one I am facing, none of the solutions provided seem to work for me at the moment. My CSS file loads and functions as expected when tested locally, but once I upload it to GitHub, it stops working. In my ...

A tutorial on how to create the appearance of disabled buttons that look the same as enabled buttons through the use

My button includes a text field that is constantly disabled. I would like for the text field to appear as bright as it does when the button is enabled. After disabling the button, they both appear dimmer compared to others and the text field follows suit ...

What is the method for showcasing an image stored in a Mysql database as a Medium Blob on my webpage?

After following instructions from the internet, I have implemented the following code in my HTML: <img src="imagescript.php?id=1"> Additionally, the imagescript.php file contains the following PHP code: <?php $servername="loc ...

Ways to eliminate unused classes from JQuery UI

We have successfully implemented JQuery UI library for enhancing our interface. However, since we no longer need to support outdated browsers like IE6, classes such as .ui-corner-all, .ui-corner-top, .ui-corner-left, .ui-corner-tl are unnecessary and clu ...

Clicking does not trigger scrollIntoView to work properly

I'm facing an issue with a button that is supposed to scroll the page down to a specific div when clicked. I implemented a scrollIntoView function in JavaScript and attached it to the button using onClick. Although the onClick event is functioning as ...