Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jQuery UI - Draggable Containment</title> <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script> <style> body{font-family:Arial, sans-serif; padding:20px;} #parent{position:relative;width:400px;height:200px;border:2px dashed #999;border-radius:6px;background:#fafafa;} #box{position:absolute;top:10px;left:10px;width:120px;height:60px;background:#04AA6D;color:#fff;display:flex;align-items:center;justify-content:center;border-radius:6px;cursor:move;} </style> </head> <body> <h3>containment: '#parent'</h3> <div id="parent"> <div id="box">Confined</div> </div> <script> $(document).ready(function(){ $("#box").draggable({ containment: "#parent" }); }); </script> </body> </html>
Output