如何在 JavaScript 中將 div 定位相對於另一個 div?
為此,你需要使用 CSS 的“flex-direction”概念。比如以下內容就是我們的 CSS 樣式 −
<style>
.demo{
display: flex;
flex-direction: column-reverse;
}
</style>現在,將 div 定位相對於另一個 div −
示例
<!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>要執行上面的程式,儲存檔名“anyName.html(index.html)”,然後右鍵單擊該檔案。在 VS Code 編輯器中選擇“使用 Live Server 開啟”選項。
輸出
這將產生以下輸出 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP