Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Cách tạo hình ảnh nền toàn trang (full-page background image) bằng CSS

Dưới đây là mã nguồn để tạo hình ảnh nền phủ toàn bộ trang web bằng CSS:

Ví dụ

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body, html { height: 100%; margin: 0; } .background { background-image: url("https://images.pexels.com/photos/1424246/pexels-photo-1424246.jpeg? auto=compress&cs=tinysrgb&dpr=1&w=1000"); height: 100%; background-position: center; background-repeat: no-repeat; background-size: cover; } </style> </head> <body> <div class="background"></div> </body> </html>

Kết quả

Mã trên sẽ cho ra kết quả như sau:

Cách tạo hình ảnh nền toàn trang (full-page background image) bằng CSS