To change the background color of an HTML website, you primarily use the CSS background-color
property, which can be applied in several ways depending on the scope and maintainability desired.
Understanding the Basics: CSS Background Color Property
The background-color
CSS property is fundamental for setting the background color of any HTML element. You can specify colors using various formats like color names (e.g., blue
), hexadecimal codes (e.g., #0000FF
), RGB values (e.g., rgb(0, 0, 255)
), or HSL values (e.g., hsl(240, 100%, 50%)
).
Here are the main methods to apply a background color:
1. Inline CSS (Directly on an HTML Element)
This method involves applying the background-color
property directly within the style
attribute of an HTML element. It's useful for quick, localized changes to a specific element, but it's generally not recommended for large-scale styling as it mixes presentation with structure.
To apply a background color using inline CSS:
- Add a
style
attribute to the desired HTML element (like<body>
,<div>
,<h1>
,<span>
, or<table>
). - Set the value of the
style
attribute tobackground-color: desired_color;
.
Example for the Entire Page:
To change the background color of the entire website, you would typically apply it to the <body>
tag:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Website with Light Blue Background</title>
</head>
<body style="background-color: lightblue;">
<h1>Welcome to My Website!</h1>
<p>This is a paragraph with a light blue background.</p>
</body>
</html>
Example for a Specific Section or Element:
You can also use this method for elements like a heading, a div
, or a span
tag:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Section with Custom Background</title>
</head>
<body>
<h2 style="background-color: #f0f0f0; padding: 10px;">A Section Title</h2>
<div style="background-color: #add8e6; padding: 20px;">
<p>This div has a distinct background color.</p>
<p><span style="background-color: yellow;">Highlight this text</span> within the div.</p>
</div>
</body>
</html>
2. Internal CSS (Within the HTML Document)
For styling an entire page or multiple elements within it, internal CSS is a more organized approach than inline styling. You place CSS rules within a <style>
tag located in the <head>
section of your HTML document.
How to Use Internal CSS:
- Within the
<head>
section of your HTML file, add a<style>
tag. - Inside the
<style>
tag, write your CSS rules, targeting elements using their tag names, classes, or IDs.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Website with Internal CSS Background</title>
<style>
body {
background-color: #E6E6FA; /* Lavender */
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
h1 {
background-color: #FFFACD; /* Lemon Chiffon */
padding: 20px;
text-align: center;
}
.content-section {
background-color: #F0FFFF; /* Azure */
padding: 15px;
margin: 20px;
border-radius: 8px;
}
</style>
</head>
<body>
<h1>My Beautiful Website</h1>
<div class="content-section">
<p>This section has a different background color.</p>
</div>
</body>
</html>
3. External CSS (Recommended for Best Practice)
For larger websites, maintaining consistency, and separating content from presentation, external CSS is the standard and most recommended approach. This involves creating a separate .css
file and linking it to your HTML document.
Steps for External CSS:
-
Create a CSS File: Create a new file (e.g.,
styles.css
) in your project folder. -
Add CSS Rules: In
styles.css
, define your background color rules:/* styles.css */ body { background-color: #F0F8FF; /* AliceBlue */ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 0; padding: 0; } h1 { background-color: #ADD8E6; /* LightBlue */ padding: 25px; text-align: center; color: #333; } .card { background-color: white; padding: 20px; margin: 20px auto; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); max-width: 600px; }
-
Link to HTML: In your HTML file, within the
<head>
section, add a<link>
tag that references yourstyles.css
file:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Website with External CSS</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>A Title from External CSS</h1> <div class="card"> <p>This content is styled by the external stylesheet.</p> <p>The entire page has an AliceBlue background set in <code>styles.css</code>.</p> </div> </body> </html>
Choosing and Specifying Colors
CSS offers several ways to define colors. Here's a quick overview:
Method | Description | Example |
---|---|---|
Named Colors | Simple, pre-defined color names. Limited choices but easy to remember. | red , blue , lightblue |
Hexadecimal | A 6-digit code representing RGB values. Very common and precise. | #FF0000 (red), #00FF00 (green), #663399 (RebeccaPurple) |
RGB | Specifies red, green, and blue intensity (0-255). | rgb(255, 0, 0) (red), rgb(102, 51, 153) |
RGBA | RGB with an added alpha (opacity) channel (0-1). | rgba(255, 0, 0, 0.5) (50% opaque red) |
HSL | Defines color using Hue (0-360°), Saturation (0-100%), Lightness (0-100%). | hsl(0, 100%, 50%) (red), hsl(270, 60%, 40%) |
HSLA | HSL with an added alpha (opacity) channel (0-1). | hsla(0, 100%, 50%, 0.7) (70% opaque red) |
You can find a comprehensive list of color names and tools for picking color codes on resources like MDN Web Docs: CSS Colors.
Practical Tips for Background Colors
- Page Background: For the background of the entire website, always apply
background-color
to the<body>
element. - Section Backgrounds: Use
div
elements or semantic HTML5 tags like<header>
,<main>
,<section>
, or<footer>
with specific classes or IDs for distinct background colors. - Readability: Ensure there is sufficient contrast between your background color and the text color for optimal readability. You can use online contrast checkers to verify.
- Consistency: For complex designs, plan your color palette in advance and use external CSS to maintain consistency across your site.