Master color theory and background properties to create stunning visual designs
Colors and backgrounds are essential elements of web design. They create visual hierarchy, establish brand identity, evoke emotions, and improve user experience. Understanding how to use CSS color and background properties effectively is crucial for modern web development.
CSS provides multiple ways to specify colors. Each method has its use cases and benefits.
/* 147 named colors available */
h1 { color: red; }
p { color: blue; }
div { color: navy; }
.box { background-color: lightblue; }
/* #RRGGBB format */
h1 { color: #FF0000; } /* Red */
p { color: #0000FF; } /* Blue */
div { color: #667eea; } /* Custom color */
/* Short hex (#RGB) */
.box { color: #F00; } /* Same as #FF0000 */
Hex Color Breakdown:
#RRGGBB
/* rgb(red, green, blue) - values 0-255 */
h1 { color: rgb(255, 0, 0); } /* Red */
p { color: rgb(0, 128, 255); } /* Blue */
div { color: rgb(102, 126, 234); } /* Custom */
/* rgba(red, green, blue, alpha) */
/* alpha: 0 (transparent) to 1 (opaque) */
.overlay {
background-color: rgba(0, 0, 0, 0.5); /* 50% transparent black */
}
.box {
background-color: rgba(102, 126, 234, 0.8); /* 80% opaque */
}
/* hsl(hue, saturation, lightness) */
/* Hue: 0-360 degrees, Saturation: 0-100%, Lightness: 0-100% */
h1 { color: hsl(0, 100%, 50%); } /* Red */
p { color: hsl(240, 100%, 50%); } /* Blue */
div { color: hsl(230, 77%, 66%); } /* Custom */
/* hsla(hue, saturation, lightness, alpha) */
.box {
background-color: hsla(230, 77%, 66%, 0.7);
}
The background-color property sets the background color
of an element.
body {
background-color: #f0f0f0;
}
.header {
background-color: rgb(102, 126, 234);
}
.card {
background-color: rgba(255, 255, 255, 0.9);
}
The background-image property sets one or more background
images for an element.
/* Single image */
.hero {
background-image: url('hero-bg.jpg');
}
/* Multiple images (layered) */
.banner {
background-image:
url('overlay.png'),
url('background.jpg');
}
/* Linear gradient */
.gradient-box {
background-image: linear-gradient(to right, #667eea, #764ba2);
}
Controls how background images are repeated.
/* No repeat */
.box1 { background-repeat: no-repeat; }
/* Repeat horizontally */
.box2 { background-repeat: repeat-x; }
/* Repeat vertically */
.box3 { background-repeat: repeat-y; }
/* Repeat both directions (default) */
.box4 { background-repeat: repeat; }
Specifies the position of the background image.
/* Keywords */
.box { background-position: center; }
.box { background-position: top right; }
.box { background-position: bottom left; }
/* Percentage */
.box { background-position: 50% 50%; } /* center */
/* Pixels */
.box { background-position: 100px 200px; }
/* Mixed */
.box { background-position: center 20px; }
Specifies the size of the background image.
/* Cover - fills entire container */
.hero {
background-size: cover;
}
/* Contain - fits within container */
.banner {
background-size: contain;
}
/* Specific size */
.box {
background-size: 200px 100px;
}
/* Auto (original size) */
.image {
background-size: auto;
}
Controls if background scrolls with the page or stays fixed.
/* Scrolls with page (default) */
.box {
background-attachment: scroll;
}
/* Fixed position (parallax effect) */
.parallax {
background-attachment: fixed;
}
/* Scrolls with element content */
.scroll-box {
background-attachment: local;
}
Combine all background properties in one declaration.
background: [color] [image] [repeat] [attachment] [position] / [size];
/* Basic */
.box {
background: #667eea;
}
/* With image */
.hero {
background: url('bg.jpg') no-repeat center center;
}
/* Complete */
.banner {
background:
#f0f0f0
url('pattern.png')
repeat-x
fixed
top center / cover;
}
/* Multiple backgrounds */
.complex {
background:
url('top.png') top center no-repeat,
url('bottom.png') bottom center no-repeat,
linear-gradient(to bottom, #667eea, #764ba2);
}
Gradients are smooth transitions between colors.
/* Top to bottom (default) */
.box1 {
background: linear-gradient(#667eea, #764ba2);
}
/* Direction */
.box2 {
background: linear-gradient(to right, red, blue);
}
/* Angle */
.box3 {
background: linear-gradient(45deg, yellow, green);
}
/* Multiple colors */
.box4 {
background: linear-gradient(to right, red, yellow, green, blue);
}
/* Color stops */
.box5 {
background: linear-gradient(to right,
red 0%,
yellow 50%,
green 100%
);
}
/* Center to edges */
.box1 {
background: radial-gradient(red, blue);
}
/* With shape */
.box2 {
background: radial-gradient(circle, red, blue);
}
/* With size and position */
.box3 {
background: radial-gradient(circle at top right, red, blue);
}
/* Rotating gradient */
.box {
background: conic-gradient(red, yellow, green, blue, red);
}
/* Pie chart effect */
.pie {
background: conic-gradient(
red 0deg 90deg,
blue 90deg 180deg,
green 180deg 270deg,
yellow 270deg 360deg
);
}
Q1: Which color format allows transparency?
Q2: What does background-size: cover do?
Q3: Which property creates a parallax scrolling effect?
Q4: In HSL, what does the 'H' stand for?
Q5: How do you prevent a background image from repeating?
linear-gradient(direction, color1, color2)
radial-gradient(shape, color1, color2)
background declaration