Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-5-sonnet-20241022· AnthropicHugging Face LMArena Human Preference 140K

I have an element, and I want the transform: translateX property to be set by the code…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219’s answer.
  1. User

    I have an element, and I want the transform: translateX property to be set by the code while the transform: translateY property should be controlled by styles. How can I make these not interfere with each other?

  2. Claude Assistant

    This is a common challenge since transform properties can override each other. Here's how you can handle it:

    1. Using style composition:
    // JavaScript
    element.style.transform = `translateX(${xValue}px) ${element.style.transform.replace(/translateX\([^)]*\)/g, '')}`;
    
    1. Or a cleaner approach using CSS custom properties:
    /* CSS */
    .element {
      transform: translateX(var(--x, 0)) translateY(var(--y, 0));
    }
    
    // JavaScript
    element.style.setProperty('--x', '100px');
    

    The second approach is generally better as it's more maintainable and avoids string manipulation. Would you like me to explain how either approach works in more detail?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.