Table of Contents
HTML Structure
The Great Outdoors
Initial CSS Styles
Clipping and Container Positioning
Accessibility Improvement
Home Web Front-end CSS Tutorial Going From Solid to Knockout Text on Scroll

Going From Solid to Knockout Text on Scroll

Mar 27, 2025 pm 12:24 PM

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>
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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 */
}
Copy after login

.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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

While You Weren't Looking, CSS Gradients Got Better While You Weren't Looking, CSS Gradients Got Better Apr 11, 2025 am 09:16 AM

One thing that caught my eye on the list of features for Lea Verou&#039;s conic-gradient() polyfill was the last item:

A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn&#039;t have to be the case. Let’s look at how PHP projects can enforce a basic

The Three Types of Code The Three Types of Code Apr 11, 2025 pm 12:02 PM

Every time I start a new project, I organize the code I’m looking at into three types, or categories if you like. And I think these types can be applied to

See all articles