
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to set div position relative to another div in JavaScript?
For this, you need to use “flex-direction” concept of CSS. Let’s say the following is our CSS style −
<style> .demo{ display: flex; flex-direction: column-reverse; } </style>
Now, set the div position relative to another div −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <style> .demo{ display: flex; flex-direction: column-reverse; } </style> </head> <body> <div class="demo"> <div class="demo1">DIV_DEMO1</div> <div class="demo2">DIV_DEMO2</div> </div> <script> </script> </body> </html>
To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
Output
This will produce the following output −
- Related Articles
- How to center a div within another div?
- How to copy the content of a div into another div using jQuery?
- How to position horizontal scroll bar at center of DIV?
- How to create editable div using JavaScript?
- How to set div width to fit content using CSS?
- How to create a moving div using JavaScript?
- How to set background color of a particular div in jQuery?
- How can you set the height of an outer div to always be equal to a particular inner div?
- How to hide a div in JavaScript on button click?
- How to add a tooltip to a div using JavaScript?
- How to append HTML code to a div using JavaScript?
- How to take screenshot of a div with JavaScript
- How to clear the content of a div using JavaScript?
- How to set position relative to top of a canvas circle using Fabric.js?
- How to clear all div’s content inside a parent div in JavaScript?

Advertisements