scroll-target-group
The scroll-target-group
CSS property specifies whether a container element is a scroll marker group container.
Syntax
/* Keyword values */
scroll-target-group: auto;
scroll-target-group: none;
/* Global values */
scroll-target-group: inherit;
scroll-target-group: initial;
scroll-target-group: revert;
scroll-target-group: revert-layer;
scroll-target-group: unset;
The scroll-target-group
property is specified as one of the keyword values listed below.
Values
Description
Setting scroll-target-group: auto
on a container element denotes it as a scroll marker group container, which groups together a set of navigation items that allow you to navigate between elements on a page (such as carousel items or article sections) and are highlighted to indicate which element is currently in view.
Any <a>
elements with fragment identifiers inside the container are automatically set as scroll markers. The anchor element whose scroll target is currently in view can be styled via the :target-current
pseudo-class.
Scroll marker group containers created using scroll-target-group
behave in a very similar way to those created using the scroll-marker-group
property, with some differences:
scroll-marker-group
automatically creates a set of pseudo-elements to represent the group container (::scroll-marker-group
) and markers (one or more instances of::scroll-marker
), and they automatically have the expected navigation associations made with the scroll container the group container is generated on. This is quicker to set up, and you don't need to create markup for the container and markers, but creating your own markup and setting it as a scroll marker group container viascroll-target-group
provides more control and flexibility.- A group container generated with
scroll-marker-group
automatically hastablist
/tab
semantics applied to it, meaning that it behaves like a single item in the tab index, and you can move between scroll markers using cursor keys. The markers specified usingscroll-target-group
behave like individual links by default, but you have the flexibility of providing alternative semantics and behavior if wished.
Formal definition
Formal syntax
scroll-target-group =
none |
auto
Examples
Basic scroll-target-group
usage
This example provides a basic page of content with a table of contents to link to different sections.
HTML
Our markup has a series of <section>
elements containing content, and a table of contents created using an ordered list (<ol>
/<li>
) and <a>
elements.
<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>
I'm baby xOXO bespoke cupidatat PBR&B, affogato cronut 3 wolf moon ea
narwhal asymmetrical. Af health goth shaman in slow-carb godard echo park.
Tofu farm-to-table labore salvia tote bag food truck dolore gluten-free
poutine kombucha fanny pack +1 franzen lyft fugiat. Chicharrones next level
jianbing, enamel pin seitan cardigan bruh snackwave beard incididunt dolor
lumbersexual before they sold out dreamcatcher single-origin coffee.
</p>
</section>
<section id="ch1" class="chapter">
<h2>Chapter 1</h2>
<!-- ... -->
</section>
<section id="ch2" class="chapter">
<h2>Chapter 2</h2>
<!-- ... -->
</section>
<!-- ... -->
CSS
We've hidden most of the styling for brevity. Most pertinently to this example, 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 :target-current
at any moment in time (that is, which link's target is currently in view). We then style the :target-current
pseudo-class with a color
of red so that it is clearly visible.
ol {
scroll-target-group: auto;
}
:target-current {
color: red;
}
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.
CSS carousel with scroll-target-group
scroll markers
This example shows how to create CSS carousel scroll markers using scroll-target-group
. The code for this example is similar to our Carousel with single pages example; we'll just explain the differences below.
HTML
The markup has IDs set on the list items that define each page, and an ordered list added that we'll turn into a scroll marker group container using CSS.
<h1>CSS carousel single item per page</h1>
<ul>
<li id="page1">
<h2>Page 1</h2>
</li>
<li id="page2">
<h2>Page 2</h2>
</li>
<li id="page3">
<h2>Page 3</h2>
</li>
<li id="page4">
<h2>Page 4</h2>
</li>
</ul>
<ol>
<li><a href="#page1">1</a></li>
<li><a href="#page2">2</a></li>
<li><a href="#page3">3</a></li>
<li><a href="#page4">4</a></li>
</ol>
CSS
We create the scroll marker group container and scroll markers by setting scroll-target-group
on the <ol>
element. The rest of the code for styling these is very similar, except that we are targeting elements of our own choosing (<ol>
and <a>
) rather than the ::scroll-marker-group
and ::scroll-marker
pseudo-elements.
We complete the code by setting some styles on the :target-current
, a:hover
, and a:focus
states to indicate which page is currently being shown and which links are being hovered/focused.
ol {
position: absolute;
position-anchor: --myCarousel;
top: calc(anchor(bottom) - 90px);
justify-self: anchor-center;
display: flex;
justify-content: center;
gap: 20px;
list-style-type: none;
padding: 0;
scroll-target-group: auto;
}
ol a {
width: 28px;
height: 28px;
display: inline-block;
text-align: center;
text-decoration: none;
padding-top: 4px;
color: black;
background-color: transparent;
border: 2px solid black;
border-radius: 50%;
}
ol a:hover,
ol a:focus,
:target-current {
background-color: black;
color: white;
}
Result
Try navigating by activating the scroll marker links, scrolling, and pressing the scroll buttons. You'll see that in each case, the highlight moves between the links to match the section currently being shown.
Specifications
Specification |
---|
CSS Overflow Module Level 5 # scroll-target-group |
Browser compatibility
See also
scroll-marker-group
::scroll-marker-group
and::scroll-marker
pseudo-elements:target-current
pseudo-class- Creating CSS carousels
- CSS overflow module