The solution can be broken down into two parts:
The image carousel layout consists of 3 parts:
The root of the image carousel will be the element responsible for the overall dimensions and containing the displayed image and button elements. Since the height is fixed, we can set height: 400px
on the root element. However, the width is fluid and on screens smaller than 600px, it should shrink to fit within the screen. For this behavior, we can use the CSS min
function (e.g. min(600px, 100vw)
).
The image can be rendered using normal <img>
tags and object-fit: contain
, which resizes the image to fit the available space of 600px x 400px while maintaining the innate aspect ratio of the image.
The various buttons are positioned at fixed locations within the image carousel. This is achieved through adding position: relative
on the root element of the image carousel and position: absolute
to the button elements.
To vertically center the prev/next buttons, use a combination of top: 50%; transform: translateY(-50%)
. top: 50%
positions the element to have a vertical offset of 50% of the container element, and translateY(-50%)
will shift the element up by half of the element's height. The pages buttons are horizontally-centered and can be done in a similar fashion.
The minimal state needed for an image carousel is the current image index. Only one state value is needed.
currIndex
by one.currIndex
by one.currIndex
to the page number.To enable the cycling behavior, we need to make sure that the currIndex
is always between 0
and images.length - 1
. We can achieve this by adding images.length
to any value, and using the modulo operator (%
).
alt
text that describes the image to support accessibility. It will be read out loud by screen readers and is also used by search engines.aria-label
s to the buttons since the buttons do not have visible labels.aria-live="polite"
attribute and alt
text of the new image as contents. This instructs screen readers to announce that a new image is displayed.