Keywords: CSS | :target pseudo-class | scroll animation | CSS animation | anchor link | progressive enhancement
Abstract: This article provides an in-depth exploration of implementing page scroll animations using the CSS3 :target pseudo-class. By analyzing the collaborative working principles of anchor links and the :target selector, it details how to achieve smooth page scrolling effects without relying on JavaScript. The article includes specific code examples demonstrating the integration of the :target selector with CSS animations, and discusses browser compatibility and progressive enhancement strategies. Additionally, it supplements with the latest developments in CSS scroll-driven animations, including concepts and applications of scroll progress timelines and view progress timelines.
CSS :target Pseudo-class and Scroll Animations
In modern web development, achieving smooth page scrolling effects is crucial for enhancing user experience. Traditional methods often rely on JavaScript, but with the continuous enrichment of CSS3 features, pure CSS solutions are becoming increasingly viable. This article focuses on implementing scroll animations using the CSS :target pseudo-class.
Working Principle of the :target Pseudo-class
The :target pseudo-class is a significant feature in CSS3 that matches the element pointed to by the current URL fragment identifier (i.e., hash). When a user clicks on an element with an anchor link, the browser automatically scrolls to the corresponding target element, and the :target selector is activated at this moment.
Its basic syntax is as follows:
#target-element:target {
/* Styles when this element becomes the target */
}
Core Techniques for Implementing Scroll Animations
By combining the :target selector with CSS animations, we can create rich scrolling effects. Here is a complete implementation example:
<style>
/* Define animation effects when the target element is activated */
#section1:target {
animation: slideIn 0.8s ease-out;
}
#section2:target {
animation: fadeIn 1s ease-in-out;
}
/* Keyframe animation definitions */
@keyframes slideIn {
0% {
transform: translateY(100px);
opacity: 0;
}
100% {
transform: translateY(0);
opacity: 1;
}
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
</style>
<!-- HTML Structure -->
<nav>
<a href="#section1">Jump to Section 1</a>
<a href="#section2">Jump to Section 2</a>
</nav>
<section id="section1">
<h3>Section 1 Content</h3>
<p>This is the specific content of section 1...</p>
</section>
<section id="section2">
<h3>Section 2 Content</h3>
<p>This is the specific content of section 2...</p>
</section>
Advanced Applications with Combinator Selectors
By combining combinator selectors (such as + and ~), we can achieve more complex animation effects. For example, when a section becomes the target, we can simultaneously animate its adjacent elements:
#section1:target + .content {
animation: expand 0.6s ease-in-out;
}
@keyframes expand {
0% {
max-height: 0;
opacity: 0;
}
100% {
max-height: 500px;
opacity: 1;
}
}
Browser Compatibility and Progressive Enhancement
The :target pseudo-class is widely supported in modern browsers, including:
- Chrome 1.0+
- Firefox 1.0+
- Safari 1.3+
- Edge 12+
- Opera 9.5+
For older browsers that do not support the :target selector, the page can still jump to the target position normally, albeit without animation effects, which reflects the design philosophy of progressive enhancement.
New Developments in CSS Scroll-Driven Animations
With the continuous evolution of CSS specifications, more powerful scroll-driven animation features have emerged. These new characteristics provide more possibilities for creating complex scrolling interactions.
Scroll Progress Timelines
Scroll progress timelines allow animations to be directly linked to the scroll position of a scroll container. Using the animation-timeline: scroll() property, animations based on scroll progress can be created:
.progress-bar {
animation: growWidth linear;
animation-timeline: scroll(root);
}
@keyframes growWidth {
from { width: 0%; }
to { width: 100%; }
}
View Progress Timelines
View progress timelines track the process of elements entering or exiting the scrollport, similar to the functionality of IntersectionObserver in JavaScript:
.fade-in-element {
animation: fadeIn;
animation-timeline: view();
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
Performance Optimization and Best Practices
When implementing CSS scroll animations, the following performance optimization points should be noted:
- Prioritize using transform and opacity properties for animations, as these can be GPU-accelerated
- Avoid triggering frequent reflows and repaints during scrolling
- Use the
will-changeproperty appropriately to hint browser optimizations - Consider accessibility needs and provide alternatives for users who prefer reduced motion
Practical Application Scenarios
:target-based scroll animation technology is suitable for various scenarios:
- Navigation switching in single-page applications
- Chapter jumps in product showcase pages
- Table of contents navigation in document readers
- Page transitions in portfolio websites
Conclusion
The CSS :target pseudo-class provides a robust foundation for implementing pure CSS scroll animations. By reasonably applying this feature, combined with CSS animations and combinator selectors, smooth and expressive page scrolling effects can be created. Simultaneously, understanding the latest CSS scroll-driven animation specifications offers more technical choices for future project development. In practical applications, browser compatibility, performance optimization, and user experience should always be considered to ensure the appropriate use of technology.