search
HomeWeb Front-endCSS TutorialGoing From Solid to Knockout Text on Scroll

Going From Solid to Knockout Text on Scroll

This CSS trick creates a captivating title that transitions from solid color to knockout text as the background image scrolls. Achieved using only HTML and CSS, this effect involves two fixed-position containers. One displays white background with knockout text; the other, a background image with white text. Clever clipping techniques hide and reveal text based on scroll position, simulating a background change.

Important Note: This technique may not function correctly on older Internet Explorer versions or mobile WebKit browsers. Consider implementing fallback solutions for compatibility.

HTML Structure

The HTML consists of an outer wrapper containing two identical containers, each with an <h1></h1> element within a .title_wrapper div:

<div class="wrapper">
  <div class="container container_solid">
    <div class="title_wrapper">
      <h1 id="The-Great-Outdoors">The Great Outdoors</h1>
    </div>
  </div>

  <div class="container container_image" aria-hidden="true">
    <div class="title_wrapper">
      <h1 id="The-Great-Outdoors">The Great Outdoors</h1>
    </div>
  </div>
</div>

Each container uses a .container class and a unique identifier (.container_solid, .container_image). This allows for shared base styles and individual targeting.

Initial CSS Styles

The CSS sets both containers to full screen height (100vh). .container_solid receives a white background, while .container_image gets a fixed background image:

.wrapper {
  position: relative; /* Added for proper positioning */
  height: 100%;
  overflow: hidden; /* Added for better overflow control */
}

.container {
  height: 100vh;
  position: absolute;
  width: 100%;
  left: 0;
}

.container_solid {
  background: white;
  top: 0;
}

.container_image {
  background-image: url(/path/to/img.jpg);
  background-size: 100vw auto;
  background-position: center;
  background-attachment: fixed;
  top: 100vh;
}

The <h1></h1> elements are styled. .container_image's text is white. .container_solid's <h1></h1> uses background-clip: text for the knockout effect, with a fallback black color:

.title_wrapper h1 {
  position: absolute; /* Added for precise positioning */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  font-size: 4em; /* Example font size */
}

.container_solid .title_wrapper h1 {
  background: url(https://images.unsplash.com/photo-1575058752200-a9d6c0f41945?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ);
  background-size: 100vw auto;
  background-position: center;
  -webkit-text-fill-color: transparent;
  text-fill-color: transparent;
  -webkit-background-clip: text;
  background-clip: text;
  color: black;
}

.container_image .title_wrapper h1 {
  color: white;
}

Clipping and Container Positioning

The crucial step involves clipping the containers to hide overflowing content. Absolute positioning is used, requiring manual positioning:

.container {
  clip: rect(0, auto, auto, 0);
  overflow: hidden;
  position: absolute;
  height: 100vh;
  left: 0;
  width: 100%;
  -webkit-mask-image: -webkit-linear-gradient(top, #ffffff 0%,#ffffff 100%); /* Safari fix */
}

.container_image is positioned below .container_solid using top: 100vh;.

Accessibility Improvement

The aria-hidden="true" attribute is added to the .container_image to inform screen readers that this container is purely decorative.

This refined approach ensures both visual appeal and accessibility. Further styling adjustments can be made to personalize the text and overall effect. Remember to always prioritize accessibility considerations when adding dynamic effects.

The above is the detailed content of Going From Solid to Knockout Text on Scroll. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Modern Scroll Shadows Using Scroll-Driven AnimationsModern Scroll Shadows Using Scroll-Driven AnimationsMay 07, 2025 am 10:34 AM

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Revisiting Image MapsRevisiting Image MapsMay 07, 2025 am 09:40 AM

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

State of Devs: A Survey for Every DeveloperState of Devs: A Survey for Every DeveloperMay 07, 2025 am 09:30 AM

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more. 

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor