Unit 6 ยท Relationships to the Land
Stage 5 of 7
Unit 6 ยท Stage 5
Your page already shows a hero image. In this stage you will stack two layers on top of it, a semi-transparent overlay and a title, using CSS positioning. Play in the editor, or press ๐ Type it with me to be guided. (pause on a highlighted word to see what it does)
.hero div showing a background image. Any property you have met lives in the ๐ Glossary at the top. This stage adds two new ideas: CSS positioning and the rgba() color format.In normal HTML flow, elements stack vertically: each block sits below the one before it. CSS positioning breaks out of that flow, so you can place elements at exact spots inside a container and layer them on top of each other.
Stacking layers takes two pieces that work together:
position: relative on the parentposition: absolute on the childtop, right, bottom, and left.Before adding the overlay, meet the color format it uses: rgba(). You already know hex colors like #1a3020. The rgba() function is another way to write a color, but it adds a fourth value, alpha, which controls transparency.
rgba(red, green, blue, alpha)rgba(0, 0, 0, 0.45)Add position: relative to your existing .hero rule, then add four new rules: one for the overlay, one for the text layer, and one each for the heading and paragraph inside it.
position: relative on .herotop: 0; left: 0; right: 0; bottom: 0 on .hero-overlaybackground-color: rgba(0, 0, 0, 0.45)bottom: 60px on .hero-textposition: relative to .hero, then add the .hero-overlay, .hero-text, .hero-text h2, and .hero-text p rules inside <style>. Use the example below, then press โถ Run./* add position: relative to your existing .hero rule */
.hero { background-image: url(https://placehold.co/1400x600/1a3020/f2ede5?text=Relationships+to+the+Land); background-size: cover; background-position: center; background-repeat: no-repeat; height: 500px; position: relative; }
/* then add these four new rules */
.hero-overlay { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.45); }
.hero-text { position: absolute; bottom: 60px; left: 0; right: 0; text-align: center; color: white; padding: 0 20px; }
.hero-text h2 { font-size: 2.5rem; margin: 0 0 12px; }
.hero-text p { font-size: 1.125rem; opacity: 0.9; margin: 0; }
What position value did you add to .hero so the overlay and text measure from its edges? Type it, then press Submit to unlock the last step.
Right now the hero is an empty container: <div class="hero"></div>. Give it two children, the overlay and the text, so the layers you styled have something to show.
<div class="hero-overlay"><div class="hero-text"><h2> title and the <p> subtitle that sit over the image.<div class="hero"></div> with a hero that has an overlay div and a text div inside. Use the example below, then press โถ Run.<div class="hero">
<div class="hero-overlay"></div>
<div class="hero-text">
<h2>Relationships to the Land</h2>
<p>Traditional knowledge of place, carried across generations</p>
</div>
</div>
position: relative on .herorelative here means the children measure from the hero's edges.top: 0; left: 0; right: 0; bottom: 0 on the overlayrgba(0, 0, 0, 0.45)left: 0; right: 0; text-align: center on the texttext-align: center centers the words inside.Experiment, then set it back the way you like it.
rgba(0, 0, 0, 0.2), then rgba(0, 0, 0, 0.7), and press Run to feel the difference.bottom: 60px to top: 40px..hero-text div.To complete Stage 5, your hero needs position: relative, a .hero-overlay with a semi-transparent rgba() background, and a .hero-text layer with a title over the image. Press Check my work when you are ready. This opens Stage 6 and lets your teacher see that you finished.