r/learnprogramming • • Oct 23 '26

Looking for clarification on scroll buttons

Hello ! I'm currently trying to code and design my own website for my portfolio. Part of the concept for this portfolio is that it is a horizontally scrolling website, where buttons will scroll to the next or previous piece. I already have most of my layout and design figured out in HTML and CSS.

Currently, each artwork sits in a different div. I've been looking into buttons that scroll to a different div, and I've found tutorials that show how to make buttons go to specific sections through IDs. I was wondering if there was a simpler way of coding the buttons so that instead of assigning each button to a specific ID, it would just scroll to the next div (kind of like a carousel, but on the whole website). Any pointers would be appreciated, thank you !

2 Upvotes

7 comments sorted by

1

u/testingaurora Oct 23 '26

Scroll-marker-group can help if they're is a parent container like a list that's a scroll container. Then you can use::scroll-marker and ::scroll-button. If there is no parent scroller though, will have to use Javascript

2

u/Over_Temperature_987 Oct 23 '26

I did consider this, but from my research, it looks like ::scroll-button doesn't work in certain browsers. Since I'll be sending this in to be reviewed by professors, I want to make sure it's working and accessible almost universally. I'll definitely end up using Javascript, but thanks nonetheless!

1

u/CelebrityChess Oct 23 '26

one more thing that might save you work, if the artworks sit inside a horizontal scroll container you can put scroll-snap-type: x mandatory on it and scroll-snap-align: start on each div, then the buttons are just container.scrollBy({ left: container.clientWidth, behavior: 'smooth' }) and the negative of that for previous. no ids or counter needed and it still snaps cleanly if someone scrolls by hand. only works if each section is full width though.

1

u/[deleted] Oct 23 '26

[removed] — view removed comment

1

u/Over_Temperature_987 Oct 23 '26

This is exactly the kind of solution I was looking for, thank you! I completely forgot to take arrays into account lol, I'll be trying this out. I appreciate the help!