HTML Backgrounds
HTML backgrounds are used to style the background of a page or element.
CSS Background Properties
background-color
body {
background-color: #f3f4f6;
}
.card {
background-color: white;
}
background-image
body {
background-image: url('background.jpg');
}
.card {
background-image: url('pattern.png');
}
background-repeat
body {
background-repeat: no-repeat;
background-repeat: repeat-x;
background-repeat: repeat-y;
}
background-position
body {
background-position: center;
background-position: top left;
background-position: 50% 50%;
}
background-size
body {
background-size: cover;
background-size: contain;
background-size: 100% 100%;
}
background-attachment
body {
background-attachment: fixed;
background-attachment: scroll;
}
Case Study: Campus Hero Section
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Campus Hero Section</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.hero {
background-image: url('campus.jpg');
background-size: cover;
background-position: center;
background-attachment: fixed;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
color: white;
position: relative;
}
.hero::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
}
.hero-content {
position: relative;
z-index: 1;
}
.hero h1 {
font-size: 3rem;
margin-bottom: 1rem;
}
.hero p {
font-size: 1.25rem;
margin-bottom: 2rem;
}
.hero-btn {
display: inline-block;
background: #1e40af;
color: white;
padding: 1rem 2rem;
text-decoration: none;
border-radius: 8px;
font-size: 1.125rem;
}
.hero-btn:hover {
background: #1e3a8a;
}
.section {
padding: 4rem 2rem;
}
.section-alt {
background: #f3f4f6;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="hero">
<div class="hero-content">
<h1>Technology University</h1>
<p>Building an Excellent Generation for the Future</p>
<a href="register.html" class="hero-btn">Register Now</a>
</div>
</div>
<section class="section">
<div class="container">
<h2>Study Programs</h2>
<p>A selection of top study programs.</p>
</div>
</section>
<section class="section section-alt">
<div class="container">
<h2>Facilities</h2>
<p>Modern facilities to support learning.</p>
</div>
</section>
</body>
</html>
Everyday Case Study
Case: A developer needs to create a pattern background.
/* Pattern dots */
.pattern-dots {
background-image: radial-gradient(circle, #ccc 1px, transparent 1px);
background-size: 20px 20px;
}
/* Pattern lines */
.pattern-lines {
background-image: repeating-linear-gradient(
45deg,
#f3f4f6,
#f3f4f6 10px,
#e5e7eb 10px,
#e5e7eb 20px
);
}
/* Gradient */
.gradient-bg {
background: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
}
Best Practices
- Optimize images: Compress background images
- Fallback color: Always provide a background-color
- Cover vs contain: Choose the right one
- Fixed vs scroll: Consider performance
- Responsive: Make sure backgrounds are responsive
Summary
Backgrounds affect the appearance of a web page. Use them wisely and consider performance.