How to Set Background Color and Image in HTML
To set a background color or image in HTML, you can use the 'style' attribute on the 'body' element. Here are two examples:
- Setting a background color:
<!DOCTYPE html>
<html>
<head>
<title>Background Color Example</title>
</head>
<body style='background-color: #f0f0f0;'>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
In this example, the background color is set to '#f0f0f0', which is a light gray color.
- Setting a background image:
<!DOCTYPE html>
<html>
<head>
<title>Background Image Example</title>
<style>
body {
background-image: url('image.jpg');
background-size: cover;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
In this example, the background image is set to 'image.jpg', and the 'background-size' property is set to 'cover' to ensure the image covers the entire background.
You can replace 'image.jpg' with the path to your desired image file.
原文地址: https://www.cveoy.top/t/topic/eFKY 著作权归作者所有。请勿转载和采集!