June 3, 2024
Color Notations: Hex, RGB, HSL
CSS supports several ways to define colors, including hexadecimal (hex), RGB, and HSL notations. Each method offers different ways to specify color values.
color: #ff5733;
color: rgb(255, 87, 51);
color: hsl(10, 100%, 60%);
Background Colors and Images
The background-color
property sets the background color of an element, while the background-image
property sets an image as the background.
Example:
body {
background-color: #f0f0f0;
background-image: url('background.jpg');
background-repeat: no-repeat;
background-size: cover;
}
Gradients: Linear, Radial, and Conic
CSS gradients allow you to create smooth transitions between two or more colors. There are three types of gradients: linear, radial, and conic.
background: linear-gradient(to right, #ff5733, #33c1ff);
background: radial-gradient(circle, #ff5733, #33c1ff);
background: conic-gradient(from 0deg, #ff5733, #33c1ff);
Exercise: Building a Stylish Header Section
Let's apply these concepts to create a stylish header section with a gradient background and custom colors.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stylish Header</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Welcome to My Stylish Website</h1>
</header>
</body>
</html>
body {
margin: 0;
font-family: Arial, sans-serif;
}
header {
background: linear-gradient(to right, #ff5733, #33c1ff);
color: white;
padding: 50px 0;
text-align: center;
}
header h1 {
margin: 0;
font-size: 2.5em;
}
Open the HTML file in a browser to see your stylish header section. Experiment with different gradients, colors, and background images to see how they affect the appearance of your header.
Coming Up Next
In Chapter 6, we'll explore CSS positioning, including static, relative, absolute, fixed, and sticky positioning, to control the layout of your web elements.
Please Sign In to post a comment.