How to set div position relative to another div in JavaScript?

Setting div position relative to another div in JavaScript can be achieved through several methods including CSS positioning, Flexbox, and dynamic JavaScript positioning. Here are the most effective approaches.

Method 1: Using CSS Positioning

The most common approach uses CSS position: relative and position: absolute to position elements relative to each other.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Relative Positioning</title>
    <style>
        .parent-div {
            position: relative;
            width: 300px;
            height: 200px;
            background-color: lightblue;
            border: 2px solid blue;
        }
        .child-div {
            position: absolute;
            top: 20px;
            left: 20px;
            width: 100px;
            height: 50px;
            background-color: lightcoral;
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <div class="parent-div">
        Parent Div
        <div class="child-div">Child Div</div>
    </div>
</body>
</html>

Method 2: Using Flexbox Layout

Flexbox provides flexible positioning options with flex-direction and alignment properties.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flexbox Positioning</title>
    <style>
        .flex-container {
            display: flex;
            flex-direction: column-reverse;
            width: 300px;
            height: 200px;
            background-color: lightgreen;
            border: 2px solid green;
        }
        .flex-item1, .flex-item2 {
            padding: 10px;
            margin: 5px;
            border: 1px solid darkgreen;
        }
    </style>
</head>
<body>
    <div class="flex-container">
        <div class="flex-item1">First Item (appears second)</div>
        <div class="flex-item2">Second Item (appears first)</div>
    </div>
</body>
</html>

Method 3: Dynamic Positioning with JavaScript

JavaScript allows dynamic positioning based on another element's coordinates using getBoundingClientRect().

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic Positioning</title>
    <style>
        .reference-div {
            width: 150px;
            height: 100px;
            background-color: yellow;
            border: 2px solid orange;
            margin: 50px;
            position: relative;
        }
        .positioned-div {
            width: 80px;
            height: 60px;
            background-color: pink;
            border: 1px solid purple;
            position: absolute;
        }
    </style>
</head>
<body>
    <div id="reference" class="reference-div">Reference Div</div>
    <div id="positioned" class="positioned-div">Positioned Div</div>
    
    <script>
        function positionRelative() {
            const reference = document.getElementById('reference');
            const positioned = document.getElementById('positioned');
            
            // Get reference element's position
            const rect = reference.getBoundingClientRect();
            
            // Position the div 10px below and 20px to the right
            positioned.style.top = (rect.bottom + 10) + 'px';
            positioned.style.left = (rect.left + 20) + 'px';
        }
        
        // Position on page load
        window.onload = positionRelative;
    </script>
</body>
</html>

Comparison of Methods

Method Use Case Flexibility Performance
CSS Positioning Static layouts Limited Best
Flexbox Responsive layouts High Good
JavaScript Dynamic Interactive positioning Highest Moderate

Key Points

  • CSS Positioning: Parent needs position: relative, child uses position: absolute
  • Flexbox: Use flex-direction to control element order and alignment
  • JavaScript: Use getBoundingClientRect() for dynamic positioning based on element coordinates

Conclusion

Choose CSS positioning for static layouts, Flexbox for responsive designs, and JavaScript for dynamic positioning. Each method offers different levels of control and performance characteristics depending on your specific requirements.

Updated on: 2026-03-15T23:18:59+05:30

763 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements