Table of Contents
HTML structure
Section A
Section B
Section C
position
Pure CSS dark mode
Show scrolling progress
Create a course navigation
Automatic shutdown mechanism (optional JavaScript)
Automatic color scheme (optional JavaScript)
Summarize
Home Web Front-end CSS Tutorial Case Study: Combining Cutting-Edge CSS Features Into a 'Course Navigation” Component

Case Study: Combining Cutting-Edge CSS Features Into a 'Course Navigation” Component

Apr 22, 2025 am 09:34 AM

Case Study: Combining Cutting-Edge CSS Features Into a “Course Navigation” Component

This article introduces the excellent article navigator designed by Jhey Tompkins and adapts it into the "course navigator" for online courses. We will dig deep into how it works, enabling a smooth interactive experience without JavaScript.

The main functions of this component include:

  • Links to all course content;
  • Smoothly scroll to the anchored chapter title;
  • Shows the reading progress of the current chapter;
  • Toggle light and dark mode;
  • Fixed to the bottom and supports folding when scrolling.

We will implement these features using the latest CSS features that are currently best supported in Chrome.

HTML structure

We use<details></details> The element creates an expandable component and pins it to the bottom of the page. Course content is included in<article></article> In the element, the chapter title is with an ID for the anchor link within the page. Click<summary></summary> Elements can switch course navigation, and the navigation content is wrapped in ::details-content pseudo-element. Navigate to links to other chapters and scroll to the title of the current chapter.<summary></summary> The element contains a label (used as a toggle button), current chapter name, scroll distance, and dark mode toggle switches.

The sample code is as follows:

<details><summary>
    <label></label>
  </summary></details>
<h1 id="Section-A">Section A</h1>
<p>...</p>
<h2 id="Section-B">Section B</h2>
<p>...</p>
<h2 id="Section-C">Section C</h2>
<p>...</p>
Copy after login

position

Using position: fixed and inset properties will<details></details> Elements are fixed at the bottom of the page:

 details {
  position: fixed;
  inset: 24px; /* used as margin*/
  place-self: end center; /* yx */
}
Copy after login

Pure CSS dark mode

In order to improve the readability of long-form content, we implement CSS dark mode.

In the HTML structure, we use a hidden checkbox<input type="checkbox"> , and a<i></i> Elements as styled checkboxes, and a<label></label> Elements as labels. All of this is wrapped in<summary></summary> In the element, and use<div> Add spacing to elements.<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;details&gt;&lt;summary&gt;&lt;label aria-label=&quot;Dark mode&quot;&gt; &lt;i&gt;&lt;/i&gt; Dark mode &lt;/label&gt; &lt;/summary&gt;&lt;/details&gt;</pre><div class="contentsignin">Copy after login</div></div> <p>Icon switching is implemented using Font Awesome CSS attribute:</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"> /* Copy from Font Awesome CSS*/ i::before { font-style: normal; font-family: &quot;Font Awesome 6 Free&quot;; display: inline-block; width: 1.25em; } /* Selected status*/ input[type=checkbox]:checked i::before { content: &quot;\f058&quot;; font-weight: 900; } /* Unselected status*/ input[type=checkbox]:not(:checked) i::before { content: &quot;\f111&quot;; font-weight: 400; }</pre><div class="contentsignin">Copy after login</div></div> <p> Use <code>:root selector and :has pseudo-class to achieve light and dark mode switching:

 /* Dark Mode*/
:root:has(input[type=checkbox]:checked) {
  color-scheme: dark;
}

/* Ming mode*/
:root:not(:has(input[type=checkbox]:checked)) {
  color-scheme: light;
}
Copy after login

Use the light-dark() function to set the color:

 color: light-dark(hsl(var(--hs) 90%), hsl(var(--hs) 10%));
background: light-dark(hsl(var(--hs) 10%), hsl(var(--hs) 90%));
Copy after login

Show scrolling progress

Use progress loops and text percentages to display reading progress.

Define custom properties --percentage :

 @property --percentage {
  syntax: "<integer> ";
  inherits: true;
  initial-value: 0;
}</integer>
Copy after login

Use scroll() animation function to implement scroll-driven animation:

 @keyframes updatePercentage {
  to {
    --percentage: 100;
  }
}

:root {
  animation: updatePercentage;
  animation-timeline: scroll();
  counter-reset: percentage var(--percentage);
}
Copy after login

Use counter() function to display the percentage:

 #progress-percentage::before {
  content: counter(percentage) "%";
  min-width: 40px;
  display: inline-block;
}
Copy after login

Use conic-gradient() function to create a progress ring:

 #progress-pie {
  aspect-ratio: 1;
  background: conic-gradient(hsl(var(--hs) 50%) calc(var(--percentage) * 1%), light-dark(hsl(var(--hs) 90%), hsl(var(--hs) 10%)) 0%);
  border-radius: 50%;
  width: 17px;
}
Copy after login

Create a course navigation

Reset list style:

 ol {
  padding-left: 0;
  list-style-position: inside;
}

ol ol li::marker {
  color: transparent;
}
Copy after login

Use the :has and :not pseudo-classes to fade list items that are not current chapters:

 details {
  color: light-dark(hsl(var(--hs) 90%), hsl(var(--hs) 10%));
}

ol:has(ol) > li:not(.active) {
  color: light-dark(hsl(var(--hs) 80%), hsl(var(--hs) 20%));
}

a {
  color: inherit;
}
Copy after login

Enable Smooth Scrolling:

 :root {
  scroll-behavior: smooth;
  scroll-padding-top: 20px;
}
Copy after login

<details></details> Element transition effect

Use interpolate-size: allow-keywords and transition properties to achieve smooth expansion and collapse:

 :root {
  interpolate-size: allow-keywords;
}

details::details-content {
  overflow-y: clip;
}

/* ... (Style codes for details:not([open])::details-content and details[open]::details-content) ... */
Copy after login

<summary></summary> Element style

Add tags and icons:

<details><summary aria-label="Navigate course">
    <i></i>
    Navigate course
  </summary></details>
Copy after login

Set the layout using display: flex and gap properties:

 /* ... (i::before style code) ... */

summary::-webkit-details-marker {
  display: none;
}

summary {
  cursor: pointer;
}

label {
  cursor: inherit;
}
Copy after login

Automatic shutdown mechanism (optional JavaScript)

Use JavaScript to achieve automatic closing of the mouse when leaving:

 document.querySelector("details").addEventListener("mouseleave", e => e.target.removeAttribute("open"));
Copy after login

Automatic color scheme (optional JavaScript)

Use JavaScript to detect user preference color schemes and automatically set:

 if (window.matchMedia("prefers-color-scheme: dark").matches) {
  document.querySelector("input[type=checkbox]").checked = true;
}
Copy after login

Summarize

This article shows how to use a range of cutting-edge CSS features to create a powerful course navigation component, including scroll-driven animations, interpolate-size attributes, light-dark() function, conic-gradient() function, and style settings for ::details-content pseudo-elements. Thanks to Jhey Tompkins for his inspiration!

The above is the detailed content of Case Study: Combining Cutting-Edge CSS Features Into a 'Course Navigation” Component. 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
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

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 We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

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.

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

Programming Sass to Create Accessible Color Combinations Programming Sass to Create Accessible Color Combinations Apr 09, 2025 am 11:30 AM

We are always looking to make the web more accessible. Color contrast is just math, so Sass can help cover edge cases that designers might have missed.

See all articles