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

วิธีเพิ่มฟอร์มลงบนรูปภาพเต็มหน้าจอ (Full-width Image Form) ด้วย CSS

วาง Form ลงบนรูปภาพพื้นหลังเต็มหน้าจอ (Full-screen Background) ใช้ Position Absolute จัดตำแหน่ง Form ด้านขวา

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

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body, html {    height: 100%;    margin: 0;    padding: 0;    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } * {box-sizing: border-box;} .backgroundImage {    height: 100%;    background-image: url("https://images.pexels.com/photos/1424246/pexels-photo-1424246.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=1000");    background-position: center;    background-repeat: no-repeat;    background-size: cover;    position: relative; } .formContainer {    position: absolute;    right: 40%;    max-width: 400px;    margin: 20px;    padding: 16px;    background-color: white; } label{    font-size: 20px;    font-weight: bolder; } input[type=text], input[type=password] {    width: 100%;    font-size: 18px;    padding: 15px;    margin: 5px 0 22px 0;    border: none;    background: #e3ff95; } input[type=text]:focus, input[type=password]:focus {    background-color: #ddd;    outline: none; } .btn-login {    background-color: #4CAF50;    color: white;    padding: 16px 20px;    border: none;    cursor: pointer;    width: 100%;    font-size: 20px; } </style> </head> <body> <div class="backgroundImage"> <form class="formContainer"> <h1>Register Here</h1> <label for="eMail">Email</label> <input type="text" placeholder="Enter your Email ID" name="eMail" required> <label for="pass">Password</label> <input type="password" placeholder="Enter your Password" name="pass" required> <label for="Address">Address</label> <input type="text" placeholder="Enter your Address" name="Address" required> <button class="btn-login">Login</button> </form> </div> </body> </html>

โครงสร้าง

  • .backgroundImage { height: 100%; position: relative; } - พื้นหลังเต็มหน้าจอ จุดอ้างอิง
  • .formContainer { position: absolute; right: 40%; max-width: 400px; } - ฟอร์มลอยด้านขวา
  • Input Styling - พื้นหลังสีเหลืองอ่อน Focus -> สีเทา
  • Button - สีเขียว กว้างเต็มฟอร์ม

Responsive Adjustment

@media (max-width: 768px) {
  .formContainer {
    position: static; /* ไม่ Absolute */
    margin: 0 auto; padding: 20px;
    max-width: 90%;
    transform: none;
  }
  .backgroundImage { height: auto; min-height: 100vh; }
}

ผลลัพธ์

รูปทิวทัศน์เต็มหน้าจอ ฟอร์มสีขาวลอยด้านขวา 4 ฟิลด์ (Email, Password, Address, Login Button)