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

  • SEARCH
  • Home
  • 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 8815091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:17:21+00:00 2026-06-14T04:17:21+00:00

I have an SVG <rect> inside a <div> and want to drag and resize

  • 0

I have an SVG <rect> inside a <div> and want to drag and resize it. When I create the elements statically in the HTML like so:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {  

        $('div').draggable({
            handle: 'rect'
        }).resizable({
            aspectRatio: 1.0
        });
    });
</script>
</head>
<body>    
    <div style="width:400px; height:400px; border:solid thin #888; padding:10px; border-radius:4px; background-color:#ccc;">
        <svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="0 0 400 400">
            <rect x="0" y="0" width="200" height="200" style="fill:#FF0000" />
        </svg>
    </div>
</body>

everything works. The <rect> is dragged and resized together with the <div>, but when I generate the elements dynamically like so:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {

        $('body').append('<div style="width:400px; height:400px; border:solid thin #888; padding:10px; border-radius:4px; background-color:#ccc;">');

        var svg = document.createElementNS('http://www.w3.org/2000/svg', "svg");
        svg.setAttributeNS(null, "viewbox", "0 0 400 400");
        $('div').append(svg);

        var square = document.createElementNS('http://www.w3.org/2000/svg', "rect");
        square.setAttributeNS(null, "width", "200");
        square.setAttributeNS(null, "height", "200");
        square.setAttributeNS(null, "x", "0");
        square.setAttributeNS(null, "y", "0");
        square.setAttributeNS(null, "style", "fill:#FF0000");
        $('svg').append(square);

        $('div').draggable({
            handle: 'rect'
        }).resizable({
            aspectRatio: 1.0
        });
    });
</script>
</head>
<body>    
</body>
</html>

only dragging continues to work for both the <div> and the <rect>. Resizing works only for the <div>, the <rect> doesn’t change size at all.

What’s wrong?

  • 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-06-14T04:17:22+00:00Added an answer on June 14, 2026 at 4:17 am

    SVG is case sensitive in the static case you use the correct “viewBox” case. In the dynamic case you don’t.

        svg.setAttributeNS(null, "viewbox", "0 0 400 400");
    

    should be

        svg.setAttributeNS(null, "viewBox", "0 0 400 400");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have problems aligning inline SVG correctly inside a bounding DIV, like in
I have some SVG files that I'd like to create a PDF with. For
I have an SVG file inside HTML, and one of the things I've heard
I would like to change the fill color of all elements inside an SVG.
I have an svg group which contains some elements, I want to clone the
right now I have a group of svg elements (circle, rect, path, etc.).The actual
I am attempting to create an arrow that looks like this using SVG elements:
I have an SVG figure and want to apply :hover property on one <rect>
I have a SVG file that I want to extend by adding onclick handlers
I have an SVG map of the world, and I want to colour each

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.