Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6471589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:14:30+00:00 2026-05-25T06:14:30+00:00

Following on from Changing width/height to a CSS rotated div triggers top/left reposition I

  • 0

Following on from Changing width/height to a CSS rotated div triggers top/left reposition I need some help to solve a CSS rotation and dynamic width/height.

I understand transform-origin and think I need to dynamically update it at the same time the width or height of the element is updated. I’m only interested in the technical solution, rather than any cross-browser cleverness and hence the demo only uses the -webkit prefix.

HTML

<div id="wrap">
    <div id="rotated">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
    Width: <input id="width" type="range" min="20" max="400" step="1" value="200">
    Height: <input id="height" type="range" min="20" max="400" step="1" value="100">
    Angle: <input id="angle" type="range" min="0" max="360" step="1" value="0">
</div>

CSS

#rotated {
    background:lightblue;
    border:1px dotted #000;
    height:100px;
    width:200px;
    position:absolute;
    top:300px;
    left:100px;
    overflow:hidden;
}

#width, #height, #angle {
    display:block;
    margin-bottom:10px;
    width:200px;
}

JavaScript

$('#width').change(function() {
    $('#rotated').css({width:this.value + 'px'});
});

$('#height').change(function() {
    $('#rotated').css({height:this.value + 'px'});
});

$('#angle').change(function() {
    $('#rotated').css({'-webkit-transform': 'rotate(' + this.value + 'deg)'});
});

In the second demo, adding the CSS

#rotated {
    -webkit-transform-origin: 100px 50px;
    -webkit-transform: rotate(60deg);
}

and updating the value of the angle slider to 60

Angle: <input id="angle" type="range" min="0" max="360" step="1" value="60">

produces the correct result when modifying the width/height via the sliders, in that the element grows and shrinks in the x, y dimensions without moving position. However, rotating the element now no longer around the desired (centre point) origin.

I have tried some variations (mousedown, mousemove, change) on this which I thought would set the origin before the width/height is modified but the <div> is still shifting position.

$('#width, #height, #angle').change(function() {
    $('#rotated').css({'-webkit-transform-origin': $('#width').val()/2 + 'px' + $('#height').val()/2 + 'px'});
});

I assume jQuery is applying the CSS changes at the same time, whereas I think that the origin needs updating before the width/height change.

Basically I want the shape to always rotate about the center point and to not move when modifying the width/height if the shape is already rotated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:14:30+00:00Added an answer on May 25, 2026 at 6:14 am

    FIDDLE DEMO!!!

    I’m first ganna tell you WHY this is happening. As you can see when your div isn’t rotated there is no problem. So why is this strange behaviour happening when it is?

    Answer: Because you’re asking it to… As you change the height or width, the div’s 50%, or middle point is changing. and thus, the browser places your div in a way where the new 50% of the unrotated DIV would be, and then rotates it.

    The solution is to wrap the rotated div in another div (lets say “#wrapper”) with a fixed width and height and overflow:visible.

    Then you place the #rotated inside the #wrapper and on width or height change, calculate the change and translate the #rotated div in a way where it is always in the center of #wrapper ( for example, for width,the translation is -(wrapWidth-newWidth)/2 ).

    Then you rotate the wrapper and voila.

    here’s the code based on your setup:

    HTML:

    <div id="wrap">
        Width: <input id="width" type="range" min="20" max="400" step="1" value="200">
        Height: <input id="height" type="range" min="20" max="400" step="1" value="100">
        Angle: <input id="angle" type="range" min="0" max="360" step="1" value="60">
    </div>
    <div id="rotateWrap">
        <div id="rotated">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
    </div>
    

    CSS:

    #width, #height, #angle {
        position:relative;
        display:block;
        margin-bottom:10px;
        width:200px;
    }
    
    #rotateWrap{
        position:absolute;
        overflow:visible;
        height:100px;
        width:200px;
        top:200px;
        left:100px;
        outline:2px solid red;
        -webkit-transform-origin: 50% 50%;
    
    }
    
    #rotated {
        background:lightblue;
        position:relative;
        overflow:hidden;
        height:100px;
    }
    
    #wrap{
        position:absolute;
    }
    

    JAVASCRIPT

    wPrev=$("#rotateWrap").width();
    hPrev=$("#rotateWrap").height();
    
    $('#width').mousedown(function(){
    }).change(function() {
        $("#rotated").width(newW=$(this).val())
            .css({left: -(newW-wPrev)/2 + "px"});
    });
    
    $('#height').mousedown(function(){
    }).change(function() {
        $("#rotated").height(newH=$(this).val())
            .css({top: -(newH-hPrev)/2 + "px"});
    });
    
    
    $('#angle').change(function() {
        $("#rotateWrap").css({"-webkit-transform":"rotate("+$(this).val()+"deg)"});
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML snippet: Block 1: <div style="position: absolute; top: 105px; left:
I've got the following code from another question on SO to track the changing
Following from my last question which @Jon Skeet gave me a lot of help
I need to port the following from the ASP.NET MVC 2 sourcecode from C#
The documentation provides the following syntax: {% video url/to/video [width height] [url/to/poster] %} I
While browsing about changing identity with tor I have the following script: from TorCtl
Is the following From header incorect? // To send HTML mail, the Content-type header
I'm getting the following from a third party library (one example): @%SystemRoot%\SomePath\SomeFile.Dll,-203 I know
Sometimes I get the following from SQL Server 2005 when executing a stored procedure:
I have a problem following from my previous problem . I also have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.