Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ตั้งตำแหน่ง div 相对กับอีก div ด้วย CSS Flexbox

ใช้ CSS flex-direction: column-reverse สลับลำดับ div

ตัวอย่างโค้ด

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <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>
</body>
</html>

วิธีรันโปรแกรม

  1. บันทึกไฟล์ชื่อ "anyName.html" หรือ "index.html"
  2. คลิกขวาที่ไฟล์ เลือก "Open with Live Server" ใน VS Code Editor

ผลลัพธ์

DIV_DEMO2 จะแสดงด้านบน DIV_DEMO1 (ลำดับกลับกัน)

หมายเหตุ: นี่เป็นเทคนิค CSS ไม่ใช่ JavaScript โดยตรง