I am attempting to design a timeline for a react app where odd children are displayed on the left side of the timeline and even children on the right side. Here is an image for reference: https://i.sstatic.net/YnNFU.png
The issue I am facing is that I am implementing this within a loop. I want each item in the loop to be positioned to the left or right based on whether the child is odd or even. I attempted using the variant utility with Tailwind for odd and even elements, but I have not been successful in configuring it. I also tried using simple CSS, but I couldn't achieve the alternating effect.
/project.js
return (
<div className="antialiased text-gray-800 bg-gray-100">
<div className="container relative flex flex-col px-6 mx-auto space-y-8">
<div className="absolute inset-0 z-0 w-2 h-full bg-white shadow-md left-17 md:mx-auto md:right-0 md:left-0"></div>
{projectData && projectData.map((project, index)=> (
<div className="relative z-10">
<img src={project.mainImage.asset.url} alt={project.mainImage.alt} className="timeline-img" />
<div id="container" className="timeline-container">
<div id="pointer" className="timeline-pointer" aria-hidden="true"></div>
<div className="p-6 bg-white rounded-md shadow-md">
<p className="pt-1">{project.description}</p>
</div>
</div>
</div>
))}
</div>
</div>
)
index.css
@tailwind base;
@tailwind components;
@tailwind utilities;
#container:nth-child(odd) {
@apply timeline-container-left timeline-container;
}
#pointer:nth-child(odd) {
@apply timeline-pointer timeline-pointer-left;
}
@layer components {
.timeline-img {
@apply object-cover w-24 h-24 border-4 border-white rounded-full shadow-md xs:absolute md:mx-auto md:left-0 md:right-0;
}
.timeline-container {
@apply relative pt-2 xs:pl-28 xs:pt-0 md:w-1/2 md:ml-auto md:pl-16;
}
.timeline-container-left {
@apply md:ml-0 md:mr-auto md:pl-0 md:pr-16;
}
.timeline-pointer {
@apply absolute inset-0 w-4 h-4 transform rotate-45 bg-white left-10 xs:top-11 xs:left-26 md:left-14;
}
.timeline-pointer-left {
@apply md:left-auto md:right-14;
}
}
I just need to apply
@apply timeline-container-left
and
@apply timeline-pointer-left
onto the container and pointer divs