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

จัดตำแหน่งข้อความมุมซ้ายบนรูปภาพด้วย CSS

ใช้ position: absolute ร่วมกับ top และ left เพื่อจัดตำแหน่งข้อความบนรูปภาพ

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
    <style>
        .box {
            position: relative; /* จุดอ้างอิง */
        }
        img {
            width: 100%;
            height: auto;
            opacity: 0.6;
        }
        .direction {
            position: absolute;
            top: 10px;
            left: 19px;
            font-size: 13px;
        }
    </style>
</head>
<body>
    <h1>Heading One</h1>
    <p>Below image has text in the top left:</p>
    <div class="box">
        <img src="https://www.tutorialspoint.com/python/images/python_data_science.jpg" alt="Tutorial" width="800" height="400">
        <div class="direction">Top Left Corner</div>
    </div>
</body>
</html>

หลักการ: Parent ต้อง position: relative Child ใช้ position: absolute พร้อม top/left/bottom/right