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

Cách tạo hình ảnh nền mờ (blurry background) bằng CSS

Dưới đây là mã nguồn để tạo hình ảnh nền mờ (blur) với văn bản trong suốt đè lên trên bằng CSS:

Ví dụ

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> body, html { height: 100vh; margin: 0; font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } * { box-sizing: border-box; } .backgroundImage { background-image: url("https://images.pexels.com/photos/1172207/pexels-photo-1172207.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"); filter: blur(10px); height: 100%; background-position: center; background-repeat: no-repeat; background-size: cover; } .transparentText { background-color: rgba(0, 0, 0, 0.4); color: white; border: 3px solid #f1f1f1; position: absolute; top: 40%; left: 30%; width: 50%; padding: 20px; text-align: center; } </style> </head> <body> <div class="backgroundImage"></div> <div class="transparentText"> <h1>I am Shawn</h1> <h1>I am a web developer</h1> </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 mờ (blurry background) bằng CSS