22
loading...
This website collects cookies to deliver better user experience
block-size
and inline-size
are logical properties: they can interchargeably refer to height or width, depending on the writing mode.
If you never ever heard about them don't worry: all you need to know is that - in this context – inline-size
represents the width and block-size
represents the height;
the <div>
element is a container with a fixed aspect-ratio
suitable for the images I've chosen, but there is nothing special with the current value (2:1
) so feel free to adjust it to your need (or to remove it);
the inner flex
list has flex-wrap
set to nowrap
and each list-item has a flex-basis: 100%
. These properties will align the slides side-by-side and will make the list scrollable;
in order to hide that scrollbar the list is some pixel (25px
) taller
than the parent container (block-size: calc(100% + 25px)
)
whose overflow is hidden. Now, for the sake of consistency, we just need to restore the height on the list items (block-size: calc(100% - 25px)
);
finally object-fit: cover
adapts the size of the images to their parent list-items.
scroll-snap-type: x mandatory;
; scroll-snap-align: start;
and
scroll-snap-stop: always;
.<nav>
) with nested links pointing to each slide: try to click on the dots!How can we make it clear what's the current slide in the bottom navigation? Could we have the dot disabled when we click on it and coloured differently? At a first sight we can think to use the :active/:focus pseudoclasses but they won't be reliable.
How can we update the bottom navigation when we navigate the carousel by gestures/mouse/trackpad? They are unrelated elements in the DOM and how a scroll event on the list can affect the state of the bottom navigation?
"It is the program, it is the magic!"
::before
pseudoelement of the <nav>
element will be triggered.@keyframes
and the style of the pseudoelement is the code that requires an in-depth focus:@keyframes dot {
0% { --slide: 1; }
12.5% { --slide: 2; }
37.5% { --slide: 3; }
62.5% { --slide: 4; }
87.5%, 100% { --slide: 5; }
}
0%
and the last slide is visible when this value is 100%
: we want to move the pseudoelement from one dot to the next one when, on scrolling, we already see half of the next slide.--slide
will change with the index of the current slide.@scroll-timeline slide {
source: selector(#s);
orientation: inline;
time-range: 1s;
}
source
property, which is tied to the id (#s
) of the list.transform: translateX(
calc((100% + var(--gap, .5rem)) *
calc(var(--slide, 1) - 1))
);
--gap
variable has been defined for the nav
element and it is the space between the dots, therefore both the variables are necessary inside this calc()
expression in order to set the right translateX
value to the overlapping pseudoelement.scroll-behavior
, a scroll-padding-top
to make some room above the carousel and the use of @supports
to detect the browser capability). CSS
.