-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add CSS carousel examples #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chrisdavidmills
merged 4 commits into
mdn:main
from
chrisdavidmills:css-carousel-examples
Aug 7, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dfb845e
Add CSS carousel examples
chrisdavidmills aedab9c
Merge branch 'main' into css-carousel-examples
pepelsbey 682b18e
Update css-carousels/scroll-button-scroll-marker-with-columns/styles.css
chrisdavidmills 5bf4bc2
Update css-carousels/scroll-button-scroll-marker/styles.css
chrisdavidmills File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
css-carousels/scroll-button-scroll-marker-with-columns/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>CSS carousel demo</title> | ||
|
||
<link href="styles.css" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<h1>CSS carousel demo 2</h1> | ||
<ul> | ||
<li> | ||
<h2>Content 1</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 2</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 3</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 4</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 5</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 6</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 7</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 8</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 9</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 10</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 11</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 12</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 13</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 14</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
<li> | ||
<h2>Content 15</h2> | ||
<p><a href="#">A demo link</a>.</p> | ||
</li> | ||
</ul> | ||
</body> | ||
</html> |
158 changes: 158 additions & 0 deletions
158
css-carousels/scroll-button-scroll-marker-with-columns/styles.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* General styles */ | ||
|
||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
body { | ||
margin: 0; | ||
font-family: Arial, Helvetica, sans-serif; | ||
} | ||
|
||
h1 { | ||
text-align: center; | ||
} | ||
|
||
button { | ||
background-color: white; | ||
} | ||
|
||
/* General styling of list as scrolling carousel */ | ||
|
||
ul { | ||
width: 100vw; | ||
height: 400px; | ||
padding: 10px; | ||
|
||
overflow-x: scroll; | ||
scroll-snap-type: x mandatory; | ||
|
||
columns: 1; | ||
text-align: center; | ||
} | ||
|
||
li { | ||
list-style-type: none; | ||
|
||
display: inline-block; | ||
height: 100%; | ||
aspect-ratio: 2/3; | ||
|
||
background-color: #eee; | ||
border: 1px solid #ddd; | ||
padding: 20px; | ||
margin: 0 20px; | ||
|
||
text-align: left; | ||
|
||
view-timeline: --inertChange inline; | ||
animation-timeline: --inertChange; | ||
animation-name: inert-change; | ||
animation-fill-mode: both; | ||
} | ||
|
||
ul::column { | ||
scroll-snap-align: center; | ||
} | ||
|
||
/* Keyframes for setting inertness on pages outside the scroller */ | ||
|
||
@keyframes inert-change { | ||
entry 0%, | ||
exit 100% { | ||
interactivity: inert; | ||
} | ||
|
||
entry 1%, | ||
exit 99% { | ||
interactivity: auto; | ||
} | ||
} | ||
|
||
/* CSS scroll buttons */ | ||
|
||
/* Style the scroll buttons */ | ||
|
||
ul::scroll-button(*) { | ||
border: 0; | ||
font-size: 2rem; | ||
background: none; | ||
color: rgb(0 0 0 / 0.7); | ||
cursor: pointer; | ||
} | ||
|
||
ul::scroll-button(*):hover, | ||
ul::scroll-button(*):focus { | ||
color: rgb(0 0 0 / 1); | ||
} | ||
|
||
ul::scroll-button(*):active { | ||
translate: 1px 1px; | ||
} | ||
|
||
ul::scroll-button(*):disabled { | ||
color: rgb(0 0 0 / 0.2); | ||
} | ||
|
||
ul::scroll-button(left) { | ||
content: "◄"; | ||
} | ||
|
||
ul::scroll-button(right) { | ||
content: "►"; | ||
} | ||
|
||
/* Position the scroll buttons */ | ||
|
||
ul { | ||
anchor-name: --myCarousel; | ||
} | ||
|
||
ul::scroll-button(*) { | ||
position: absolute; | ||
position-anchor: --myCarousel; | ||
} | ||
|
||
ul::scroll-button(left) { | ||
right: calc(anchor(left) - 70px); | ||
bottom: calc(anchor(top) + 21px); | ||
} | ||
|
||
ul::scroll-button(right) { | ||
left: calc(anchor(right) - 70px); | ||
bottom: calc(anchor(top) + 21px); | ||
} | ||
|
||
/* Scroll marker styling */ | ||
|
||
ul { | ||
/* Needed for the pseudo to be generated */ | ||
scroll-marker-group: after; | ||
} | ||
|
||
/* Once tabbed to, the scroll marker group can then be moved through via the cursor keys */ | ||
/* It in effect acts as a single control for tabbing purposes */ | ||
ul::scroll-marker-group { | ||
position: absolute; | ||
position-anchor: --myCarousel; | ||
top: calc(anchor(bottom) - 70px); | ||
justify-self: anchor-center; | ||
display: flex; | ||
justify-content: center; | ||
gap: 20px; | ||
} | ||
|
||
ul::column::scroll-marker { | ||
content: ""; | ||
width: 16px; | ||
height: 16px; | ||
background-color: transparent; | ||
border: 2px solid black; | ||
border-radius: 10px; | ||
/* This is needed because non-current targets are made inert */ | ||
interactivity: auto; | ||
} | ||
|
||
ul::column::scroll-marker:target-current { | ||
background-color: black; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>CSS carousel demo</title> | ||
|
||
<link href="styles.css" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<h1>CSS carousel demo 1</h1> | ||
<ul> | ||
<li> | ||
<h2>Page 1</h2> | ||
<p>This is the first page of content.</p> | ||
<p><a href="#">A demo link</a>.</p> | ||
<p><button>Press me</button></p> | ||
</li> | ||
<li> | ||
<h2>Page 2</h2> | ||
<p>This is the second page of content.</p> | ||
<p><a href="#">A demo link</a>.</p> | ||
<p><button>Press me</button></p> | ||
</li> | ||
<li> | ||
<h2>Page 3</h2> | ||
<p>This is the third page of content.</p> | ||
<p><a href="#">A demo link</a>.</p> | ||
<p><button>Press me</button></p> | ||
</li> | ||
<li> | ||
<h2>Page 4</h2> | ||
<p>This is the fourth page of content.</p> | ||
<p><a href="#">A demo link</a>.</p> | ||
<p><button>Press me</button></p> | ||
</li> | ||
</ul> | ||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.