Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions files/en-us/web/css/guides/overflow/carousels/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ The CSS carousel features provide pseudo-elements and pseudo-classes that enable
- : Generated inside the children of a scroll container ancestor or within a scroll container's columns, to represent their scroll markers. These can be selected to scroll the container to their associated child elements or column, and are collected inside the scroll container's {{cssxref("::scroll-marker-group")}} for layout purposes.
- {{cssxref(":target-current")}}
- : This pseudo-class can be used to select the currently-active scroll marker. It can be used to provide a highlight style to the currently active marker, which is important for usability and accessibility.
- [`:target-before`](/en-US/docs/Web/CSS/Reference/Selectors/:target-before) and [`:target-after`](/en-US/docs/Web/CSS/Reference/Selectors/:target-after)
- : These pseudo-classes can be used to select scroll markers before and after the currently-active scroll marker, respectively. This is useful for styling navigation items that come before and after the active navigation position, to help give the user an indication of what they've already looked at, and what is still to come.
- {{cssxref("::column")}}
- : This pseudo-element represents the individual columns generated when a container is set to display its content in multiple columns via [CSS multi-column layout](/en-US/docs/Web/CSS/Guides/Multicol_layout). It can be used in conjunction with {{cssxref("::scroll-marker")}} to generate a scroll marker for each column.

Expand Down Expand Up @@ -533,9 +535,23 @@ ul::column::scroll-marker {
border: 2px solid black;
border-radius: 10px;
}
```

Finally, we again use the `:target-current` pseudo-element to mark the active scroll marker, providing the user with an idea of where they are in the navigation. However, we also use the [`:target-before`](/en-US/docs/Web/CSS/Reference/Selectors/:target-before) and [`:target-after`](/en-US/docs/Web/CSS/Reference/Selectors/:target-after) pseudo-classes to give the scroll markers before and after the active scroll marker some custom styling. We also set a {{cssxref("transition")}} on the `ul::column::scroll-marker:target-current` ruleset, so that the style changes between the different states animate smoothly.

```css live-sample___second-example
ul::column::scroll-marker:target-before {
border: 2px solid gray;
}

ul::column::scroll-marker:target-current {
background-color: black;
transition: all 0.7s;
}

ul::column::scroll-marker:target-after {
border: 2px solid red;
background-color: red;
}
```

Expand Down
2 changes: 2 additions & 0 deletions files/en-us/web/css/guides/overflow/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ The CSS overflow level 4 module also introduces the `block-ellipsis`, `continue`
- {{cssxref("::scroll-button()")}}
- {{cssxref("::scroll-marker")}}
- {{cssxref("::scroll-marker-group")}}
- [`:target-after`](/en-US/docs/Web/CSS/Reference/Selectors/:target-after)
- [`:target-before`](/en-US/docs/Web/CSS/Reference/Selectors/:target-before)
- {{cssxref(":target-current")}}

### Data types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ Note the placement of the scroll marker group container. Check out how the keybo
- {{cssxref("::scroll-marker-group")}}
- {{cssxref("::scroll-marker")}}
- {{cssxref(":target-current")}}
- [`:target-before`](/en-US/docs/Web/CSS/Reference/Selectors/:target-before)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use macros in the css docs whenever possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually do, but at the moment, I'm using raw markdown links until the cssxref macro is fixed. Last time I checked, it was still broken for new links.

- [`:target-after`](/en-US/docs/Web/CSS/Reference/Selectors/:target-after)
- [Creating CSS carousels](/en-US/docs/Web/CSS/Guides/Overflow/Carousels)
- [CSS overflow](/en-US/docs/Web/CSS/Guides/Overflow) module
- [CSS Carousel Gallery](https://chrome.dev/carousel/) via chrome.dev (2025)
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,6 @@ Try navigating in each of these three ways: by activating the scroll marker link

- {{cssxref("scroll-marker-group")}}
- {{cssxref("::scroll-marker-group")}} and {{cssxref("::scroll-marker")}} pseudo-elements
- {{cssxref(":target-current")}} pseudo-class
- {{cssxref(":target-current")}}, [`:target-before`](/en-US/docs/Web/CSS/Reference/Selectors/:target-before), and [`:target-after`](/en-US/docs/Web/CSS/Reference/Selectors/:target-after) pseudo-classes
- [Creating CSS carousels](/en-US/docs/Web/CSS/Guides/Overflow/Carousels)
- [CSS overflow](/en-US/docs/Web/CSS/Guides/Overflow) module
259 changes: 259 additions & 0 deletions files/en-us/web/css/reference/selectors/_colon_target-after/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
---
title: :target-after
slug: Web/CSS/Reference/Selectors/:target-after
page-type: css-pseudo-class
status:
- experimental
browser-compat: css.selectors.target-after
sidebar: cssref
---

{{SeeCompatTable}}

The **`:target-after`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Reference/Selectors/Pseudo-classes) selects elements that come after the **active** scroll marker (the scroll marker of a {{cssxref("scroll-marker-group")}} that is currently scrolled to) in a navigation menu. This [selector](/en-US/docs/Web/CSS/Guides/Selectors) can be used to style navigation items that come after the active navigation position within a scroll marker group.

> [!NOTE]
> The `:target-after` pseudo-class is only valid on {{cssxref("::scroll-marker")}} pseudo-elements and elements that have been designated as scroll markers via the [`scroll-target-group`](/en-US/docs/Web/CSS/Reference/Properties/scroll-target-group) property.
## Syntax

```css-nolint
:target-after {
/* ... */
}
```

## Examples

### Targeting navigation items before and after the active scroll marker

In this example, we use the [`:target-before`](/en-US/docs/Web/CSS/Reference/Selectors/:target-before) and `:target-after` pseudo-classes to highlight the scroll markers before and after the active scroll marker appropriately, giving the user an indication of what items have already been viewed, and which are still to come.

#### HTML

Our markup has a series of {{htmlelement("section")}} elements containing content, and a table of contents created using an ordered list ({{htmlelement("ol")}}/{{htmlelement("li")}}) and {{htmlelement("a")}} elements.

```html
<nav id="toc">
<ol>
<li><a href="#intro">Introduction</a></li>
<li><a href="#ch1">Chapter 1</a></li>
<li><a href="#ch2">Chapter 2</a></li>
<li><a href="#ch3">Chapter 3</a></li>
<li><a href="#ch4">Chapter 4</a></li>
</ol>
</nav>
<section id="intro" class="chapter">
<h1>Prose of the century</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus
aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci,
pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
ultricies tellus laoreet sit amet.
</p>
</section>
<section id="ch1" class="chapter">
<h2>Chapter 1</h2>

<!-- ... -->
</section>
<section id="ch2" class="chapter">
<h2>Chapter 2</h2>

<!-- ... -->
</section>

<!-- ... -->
```

```html hidden live-sample___targeting-before-and-after
<nav id="toc">
<ol>
<li><a href="#intro">Introduction</a></li>
<li><a href="#ch1">Chapter 1</a></li>
<li><a href="#ch2">Chapter 2</a></li>
<li><a href="#ch3">Chapter 3</a></li>
<li><a href="#ch4">Chapter 4</a></li>
</ol>
</nav>
<section id="intro" class="chapter">
<h1>My story</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus
aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci,
pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
ultricies tellus laoreet sit amet.
</p>
</section>
<section id="ch1" class="chapter">
<h2>Chapter 1</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus
aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci,
pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
ultricies tellus laoreet sit amet.
</p>
<p>
Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet
orci vel, viverra egestas ligula. Curabitur vehicula tellus neque, ac ornare
ex malesuada et. In vitae convallis lacus. Aliquam erat volutpat.
Suspendisse ac imperdiet turpis. Aenean finibus sollicitudin eros pharetra
congue. Duis ornare egestas augue ut luctus. Proin blandit quam nec lacus
varius commodo et a urna. Ut id ornare felis, eget fermentum sapien.
</p>
<p>
Nam vulputate diam nec tempor bibendum. Donec luctus augue eget malesuada
ultrices. Phasellus turpis est, posuere sit amet dapibus ut, facilisis sed
est. Nam id risus quis ante semper consectetur eget aliquam lorem. Vivamus
tristique elit dolor, sed pretium metus suscipit vel. Mauris ultricies
lectus sed lobortis finibus. Vivamus eu urna eget velit cursus viverra quis
vestibulum sem. Aliquam tincidunt eget purus in interdum. Cum sociis natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus.
</p>
</section>
<section id="ch2" class="chapter">
<h2>Chapter 2</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus
aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci,
pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
ultricies tellus laoreet sit amet.
</p>
<p>
Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet
orci vel, viverra egestas ligula. Curabitur vehicula tellus neque, ac ornare
ex malesuada et. In vitae convallis lacus. Aliquam erat volutpat.
Suspendisse ac imperdiet turpis. Aenean finibus sollicitudin eros pharetra
congue. Duis ornare egestas augue ut luctus. Proin blandit quam nec lacus
varius commodo et a urna. Ut id ornare felis, eget fermentum sapien.
</p>
</section>
<section id="ch3" class="chapter">
<h2>Chapter 3</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus
aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci,
pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
ultricies tellus laoreet sit amet.
</p>
<p>
Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet
orci vel, viverra egestas ligula. Curabitur vehicula tellus neque, ac ornare
ex malesuada et. In vitae convallis lacus. Aliquam erat volutpat.
Suspendisse ac imperdiet turpis. Aenean finibus sollicitudin eros pharetra
congue. Duis ornare egestas augue ut luctus. Proin blandit quam nec lacus
varius commodo et a urna. Ut id ornare felis, eget fermentum sapien.
</p>
</section>
<section id="ch4" class="chapter">
<h2>Chapter 4</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus
aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci,
pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at
ultricies tellus laoreet sit amet.
</p>
<p>
Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet
orci vel, viverra egestas ligula. Curabitur vehicula tellus neque, ac ornare
ex malesuada et. In vitae convallis lacus. Aliquam erat volutpat.
Suspendisse ac imperdiet turpis. Aenean finibus sollicitudin eros pharetra
congue. Duis ornare egestas augue ut luctus. Proin blandit quam nec lacus
varius commodo et a urna. Ut id ornare felis, eget fermentum sapien.
</p>
</section>
```

#### CSS

We've set `scroll-target-group: auto` on the `<ol>` to turn it into a scroll marker group container and trigger the browser's algorithm for calculating which `<a>` element is the active scroll marker at any given time (that is, which link's target is currently in view). We then style the `:target-current` pseudo-class with a `red` {{cssxref("color")}} so that it stands out clearly.

```css hidden live-sample___targeting-before-and-after
body {
font: 1.2em / 1.5 system-ui;
width: 50%;
max-width: 700px;
margin: 0 auto;
}

nav {
position: fixed;
top: 0;
left: 0;
right: 0;
display: flex;
justify-content: center;
}

section {
padding-top: 60px;
}

a {
color: black;
text-decoration: none;
}

a:hover,
a:focus {
text-decoration: underline;
}

ol {
display: flex;
width: 100%;
justify-content: space-around;
list-style-type: none;
padding: 20px 0;
margin: 0;
background: white;
}
```

```css live-sample___targeting-before-and-after
ol {
scroll-target-group: auto;
}

:target-current {
color: red;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an outline or some other non-color differentiator for those who can't distinguish reds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the three different link states (before, current, and after) are distinguished by gray and line-through, red, and black and underlined, so they are not distinguished by color alone.

}
```

Finally, we use the `:target-before` pseudo-class to style all `<a>` elements before the active scroll marker with a gray color and strike-through, to make them look completed/finished, and we use the `:target-after` pseudo-class to style all `<a>` elements after the active scroll marker with a bright underline.

```css live-sample___targeting-before-and-after
a:target-before {
color: gray;
text-decoration: line-through;
}

a:target-after {
text-decoration: underline orange 2px;
}
```

#### Result

Try navigating by activating the links and by scrolling. You'll see that in each case, the red highlight moves between the links to match the section currently being shown, and the links before and after the current red link update to adopt the styles defined in our `a:target-before` and `a:target-after` rules.

{{EmbedLiveSample("targeting-before-and-after", "100%", 500)}}

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- {{cssxref("scroll-marker-group")}}
- [`scroll-target-group`](/en-US/docs/Web/CSS/Reference/Properties/scroll-target-group)
- [`:target-current`](/en-US/docs/Web/CSS/Reference/Selectors/:target-current), [`:target-before`](/en-US/docs/Web/CSS/Reference/Selectors/:target-before)
- {{cssxref("::scroll-marker")}}
- {{cssxref("::scroll-marker-group")}}
- [Creating CSS carousels](/en-US/docs/Web/CSS/Guides/Overflow/Carousels)
- [CSS overflow](/en-US/docs/Web/CSS/Guides/Overflow) module
- [CSS Carousel Gallery](https://chrome.dev/carousel/) via chrome.dev (2025)
Loading