Episode 6 of 12
Header CSS
Style the header and hero section — layout, colors, typography, and the navigation bar.
Header CSS
Now that our HTML structure is in place, let's bring the header and hero section to life with CSS. We'll start with a base reset, set up our container, and style the navigation bar and hero section.
CSS Reset & Base Styles
/* ---- Reset ---- */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
color: #333;
background: #f4f4f4;
}
a {
text-decoration: none;
color: inherit;
}
ul {
list-style: none;
}
img {
max-width: 100%;
height: auto;
}
The Container
.container {
max-width: 1100px;
margin: 0 auto;
padding: 0 20px;
}
Header Styling
#main-header {
background: #333;
color: #fff;
padding: 15px 0;
position: sticky;
top: 0;
z-index: 1000;
}
.header-inner {
display: flex;
align-items: center;
justify-content: space-between;
}
.logo img {
width: 150px;
}
Navigation Styling
#main-nav ul {
display: flex;
align-items: center;
}
#main-nav ul li {
margin-left: 25px;
}
#main-nav ul li a {
color: #fff;
font-size: 15px;
font-weight: 500;
padding: 5px 0;
transition: color 0.3s ease;
border-bottom: 2px solid transparent;
}
#main-nav ul li a:hover,
#main-nav ul li a.active {
color: #31A8FF;
border-bottom: 2px solid #31A8FF;
}
Hamburger Button (Hidden on Desktop)
.hamburger {
display: none; /* Hidden on desktop */
background: none;
border: none;
cursor: pointer;
padding: 10px;
}
.hamburger .bar {
display: block;
width: 25px;
height: 3px;
background: #fff;
margin: 5px 0;
transition: all 0.3s ease;
}
Hero Section Styling
#hero {
background: url('../img/hero.jpg') no-repeat center center/cover;
color: #fff;
min-height: 500px;
display: flex;
align-items: center;
position: relative;
}
/* Dark overlay for text readability */
#hero::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
}
.hero-content {
position: relative; /* Above the overlay */
z-index: 1;
max-width: 600px;
}
.hero-content h1 {
font-size: 48px;
font-weight: 700;
margin-bottom: 20px;
line-height: 1.2;
}
.hero-text {
font-size: 18px;
margin-bottom: 30px;
line-height: 1.6;
}
Button Styles
.btn {
display: inline-block;
padding: 12px 30px;
background: #31A8FF;
color: #fff;
font-size: 16px;
font-weight: 600;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s ease, transform 0.2s ease;
}
.btn:hover {
background: #1a8ad4;
transform: translateY(-2px);
}
.hero-btn {
font-size: 18px;
padding: 14px 36px;
}
CSS Techniques Explained
| Technique | What It Does |
|---|---|
display: flex | Creates a flexbox layout for alignment |
position: sticky | Keeps header fixed at top when scrolling |
z-index: 1000 | Ensures header stays above all content |
::before pseudo-element | Creates the dark overlay on the hero background |
background: ... / cover | Makes the hero image cover the full section |
transition | Smooth hover effects on links and buttons |
The Sticky Header Pattern
position: sticky is the modern way to create a fixed header:
- It behaves like
relativeuntil you scroll past it - Then it "sticks" to the top of the viewport like
fixed - Unlike
position: fixed, it doesn't take the element out of document flow - It needs a
topvalue to work:top: 0
Key Takeaways
- Start with a CSS reset to normalize browser defaults
- Use Flexbox for header layout — logo on the left, nav on the right
position: stickycreates a modern fixed header effect- Use a
::beforeoverlay to ensure text readability on hero images - Add transitions for smooth hover effects on links and buttons
- The hamburger button is in the HTML but hidden on desktop with
display: none