After relying solely on "pre-made" components like Mui or TailWind, I decided to create a component using only CSS and maybe JavaScript. However, I encountered some difficulties when attempting to position a div inside an image using relative and absolute positions. I am aware that absolute positioning disrupts the natural flow... and I need to find a way to make this scrollable div work.
The issue at hand: when the page width is less than the combined width of all images, the images overflow into the next div, disregarding the margins set by another div.
I am considering a solution where I use useRef to retrieve the width from the 'a' tag and calculate the left position to create a gap. For example, left: 0, left: 580*funcGetWidthFromRef(), left: funcGetWidthFromRef().
Is there another alternative solution?
Here is my code:
import Image from "next/image";
import styles from "../../styles/Card.module.css";
export default function Card() {
const listArticles = [1, 2, 3, 4];
const textTitle = ["Vant", "Vam", "Puits", "JUmpP"];
return (
<>
{" "}
<section className={`${styles.scrollableCategoryContent}`}>
{listArticles.map((art, index) => {
return (
<article key={index} className={`styles.Game${art}`}>
<a
href=""
className={`${styles.cardAnchorConteirner}`}
style={{
left: `${170 * index}px`,
}}
>
<Image
className={`${styles.cardImage}`}
width={580}
height={480}
objectFit="cover"
objectPosition="top left"
src={
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Postgres_Query.jpg/1920px-Postgres_Query.jpg"
}
alt="test"
></Image>
<div
className={`${styles.textCardBackGround}`}
>
<h2
className={`${styles.cardTitleText}`}
>{`${textTitle[index]}`}</h2>
</div>
</a>
</article>
);
})}
</section>
</>
);
}
.scrollableCategoryContent{
display: grid;
grid-auto-flow: column;
background-color: black;
max-width: 700;
height: 480px;
overflow-x: scroll;
}
Game1, .Game2, .Game3, .Game4 {
}
.cardAnchorConteirner{
width: 100%;
height: 100%;
position: relative;
}
.cardImage{
position: absolute;
}
.textCardBackGround {
position: absolute;
background-color: aqua;
width: 100%;
height: 20%;
left: 0px;
right: 0px;
bottom: 20%;
}
.cardTitleText{
color: white;
}