CSS Colors Get Certified
What are CSS Colors?
CSS colors are used to set the color of text, backgrounds, borders, and other elements on a webpage. Colors improve the visual appearance of a website and create a better user experience. They help organize content, draw attention to important information, establish brand identity, and make websites easier to read and navigate.
Why Use CSS Colors?
Colors improve the visual appearance of a website and create a better user experience. They help organize content, draw attention to important information, establish brand identity, and make websites easier to read and navigate.
Syntax of CSS Colors
What this code does: Applies a blue color to the main heading using the color property inside a simple internal stylesheet.
<!DOCTYPE html>
<html>
<head>
<style>
h1{
color: blue;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Step 1: Change the Text Color
Use the color property to change the color of text. You can use color names, hexadecimal values, RGB values, or HSL values.
What this code does: Makes the heading crimson and the paragraph green using simple color names.
<!DOCTYPE html>
<html>
<head>
<style>
h1{
color: crimson;
}
p{
color: green;
}
</style>
</head>
<body>
<h1>Welcome to Academic Block</h1>
<p>This paragraph is green.</p>
</body>
</html>
Step 2: Change the Background Color
Use the background-color property to change the background color of an element.
What this code does: Sets a light blue page background and gives the heading a solid blue background with white text.
<!DOCTYPE html>
<html>
<head>
<style>
body{
background-color:#eef7ff;
}
h1{
background-color:#007BFF;
color:white;
padding:15px;
}
</style>
</head>
<body>
<h1>CSS Background Color</h1>
</body>
</html>
Different Ways to Specify Colors
CSS provides multiple ways to define colors. Each method produces the same result but uses a different format depending on your preference.
What this code does: Demonstrates five different color formats (name, hex, RGB, RGBA, HSL) applied to separate headings so you can see them side by side.
<!DOCTYPE html>
<html>
<head>
<style>
.name{
color:red;
}
.hex{
color:#0066cc;
}
.rgb{
color:rgb(0,128,0);
}
.rgba{
color:rgba(255,0,0,0.6);
}
.hsl{
color:hsl(280,100%,40%);
}
</style>
</head>
<body>
<h2 class="name">Color Name</h2>
<h2 class="hex">Hexadecimal</h2>
<h2 class="rgb">RGB Color</h2>
<h2 class="rgba">RGBA Color</h2>
<h2 class="hsl">HSL Color</h2>
</body>
</html>
Example of CSS Colors
What this code does: Builds a complete colorful page with a light background, blue heading, dark paragraph, orange box, and a green button that darkens on hover.
<!DOCTYPE html>
<html>
<head>
<style>
body{
font-family:Arial,sans-serif;
background:#f5f9ff;
}
h1{
color:#0056b3;
}
p{
color:#444;
}
button{
background:#28a745;
color:white;
border:none;
padding:12px 24px;
font-size:16px;
cursor:pointer;
border-radius:5px;
}
button:hover{
background:#1e7e34;
}
.box{
width:200px;
margin:20px auto;
padding:20px;
background:orange;
color:white;
border-radius:8px;
}
</style>
</head>
<body>
<h1>Learning CSS Colors</h1>
<p>Colors make webpages beautiful.</p>
<div class="box">Orange Box</div>
<button>Click Me</button>
</body>
</html>
Advantages of CSS Colors
Common Mistakes
Beginners often misspell color names, forget the # in hexadecimal values, use invalid RGB values, or choose text and background colors with poor contrast, making content difficult to read.
What this code does: Shows a common spelling mistake (blu) versus the correct color name blue.
<!DOCTYPE html>
<html>
<head>
<style>
/* Incorrect */
h1{
color:blu;
}
/* Correct */
h1{
color:blue;
}
</style>
</head>
<body>
<h1>CSS Colors</h1>
</body>
</html>
Best Practices
- Choose colors with sufficient contrast for better readability.
- Use a consistent color scheme throughout your website.
- Prefer hexadecimal or RGB values for precise color control.
- Avoid using too many bright colors on one page.
- Test colors on different screen sizes and devices.
- Use meaningful colors to indicate actions or status.
- Keep accessibility in mind when selecting text and background colors.
- Use CSS variables for reusable color values in large projects.
Web Resources on CSS Colors
1. MDN Web Docs : <named-color> CSS type.
2. Developer Chrome.com : High definition CSS color guide.
3. HTML Color Codes.com : HTML color codes, Hex color codes, RGB, HSL and OKLCH values.
4. Elementor.com : CSS Colors Properties, Codes, Keywords and Names.
Click “Run Code” to see the result here.
CSS Syntax Example
The CSS syntax consists of a selector and a declaration block.
h1 { color: blue; font-size: 36px;}Selector: h1 – targets all <h1> elements
Property: color and font-size
Value: blue and 36px
Your CSS Applied Below:
This is an H1 heading with your CSS
This is an H2 heading (not affected)
This paragraph is not affected by the h1 selector.
Welcome to CSS
This page is styled using CSS.
The heading and paragraph use your CSS rules.
- List item with your styles
- Another list item
🧪 Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.