如何在 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 開啟”選項。

輸出

這將產生以下輸出 −

更新於: 2020-09-07

584 次瀏覽

助力你的 職業生涯

透過完成課程獲取認證

開始
廣告