Introduction
A background image adds a picture behind the content of a webpage to make it look nicer and more interesting.
Jump into concepts and practice - no test required
selector {
background-image: url('path/to/image.jpg');
}body {
background-image: url('background.jpg');
}.header { background-image: url('header-pattern.png'); background-repeat: no-repeat; }
.card { background-image: url('card-bg.svg'); background-size: cover; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Background Image Example</title> <style> body { margin: 0; font-family: Arial, sans-serif; background-image: url('https://images.unsplash.com/photo-1506744038136-46273834b3fb?auto=format&fit=crop&w=800&q=80'); background-size: cover; background-position: center; background-repeat: no-repeat; color: white; height: 100vh; display: flex; justify-content: center; align-items: center; text-shadow: 0 0 5px black; } h1 { font-size: 3rem; } </style> </head> <body> <h1>Welcome to My Website</h1> </body> </html>
background-image do on a webpage?background-imagebackground-image.photo.jpg?: to assign values, not an equals sign or parentheses.url('filename') with parentheses and quotes around the filename.<div>?
div {
background-image: url('tree.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
width: 200px;
height: 200px;
border: 1px solid black;
}body {
background-image: url(tree.png);
background-repeat: no-repeat
background-position: center;
}