Crafting a stylish vertical timeline using Tailwind and CSS: Step-by-step guide

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

Answer №1

To make the elements stand out, you can utilize the index of each item in a clever way by incorporating it into a ternary operator within the className attribute. Check if the index is divisible by 2 to determine if it's an even number. For example:

 <div className={`${index % 2 == 0 ? "ml-auto" : "mr-auto" }`}>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Text area featuring unique border design, with border color change upon focus?

Currently, I have a text area with borders that are designed in a unique way. However, I am looking to change the border color when the user focuses on the text area. Do you have any suggestions on how I can achieve this effect without affecting the curre ...

CSS will only be visible if it is enclosed within the <style> tag

While creating a view, I noticed that my CSS is not being displayed unless it's placed inside a <style> tag. I've tried using !important without success, as well as utilizing the selector .container-fluid > .row > .d-flex > .out_pr ...

I prefer not to permit components to receive undefined values

When using swr, the data type is IAge| undefined. I want to avoid passing undefined to AgeComponent, so I need the age type to be strictly IAge. Since AgeComponent does not allow undefined values, I am facing an error stating that 'IAge | undefined&ap ...

Troubleshooting issues when utilizing flexbox in Firefox and Chrome version 48

Chrome 47 Behavior: The .scroll div scrolls correctly in Chrome 47, taking up 100% height using flex. Firefox Behavior: In Firefox, the .scroll div does not scroll properly and instead uses the content height. What is the solution that works across diffe ...

Navigating the complexities of integrating Rollup, ES modules, TypeScript, and JSX can be a challenging endeavor due to

Lately, I've been learning about the tools mentioned in the title. However, I've encountered some bumps along the way, so I'm turning to StackOverflow for help. My Requirements I need something like Rollup to pack my stuff For bare module ...

Error when using ES6 modules in ts-jest

I keep running into an 'unexpected token' error whenever I run my tests using ts-jest. To show you exactly what's going on, I've created a small repo with all of my current configurations available here: https://github.com/ramoneguru/t ...

A guide to displaying a single field from a Firestore document using React

For our fourth-semester university project, we are creating a quiz application using React with MaterialUI for styling and Firestore as the database. I am currently facing challenges in displaying/rendering data from a document in Firestore. The Firestor ...

Can one utilize Javascript to write in plain text format?

Currently, using JavaScript I have a plain text containing data that is displayed within my form tags. Everything is functioning correctly, but now I need to update the values inside the code of my form tags in order for the changes to also be reflected in ...

What is the best way to implement dragging functionality for an image within a specific area?

Here's a snippet of the code I'm working with: link This is the HTML code: <div class="border"> <div id="i-choose"> <input type="file" name="file" id="file" class="inputfile"> <label for="file"& ...

Exploring the distinctions among library, package, and module in JavaScript

As I dive into learning React, I find myself feeling overwhelmed by the idea of packages. Why can't we simply use a CDN link instead? There's this module that seems to be crucial but still puzzles me. And what exactly is npm and why is it necessa ...

Utilizing Conditional Statements Within a Map Function

A DataGrid on my frontend application displays data fetched from a MongoDB backend. The data corresponds to keys defined in the MongoDB documents, each containing a set of boolean values. My goal is to check if any of these boolean values are true and disp ...

Setting up a React Package by reinstalling a git repository

While immersing myself in learning React JS, I decided to create a git repository containing React components that could be exported and used or installed as a package in a separate React application. I developed some UI Components in a git repository and ...

Searching dynamically using class names with JQuery

I am seeking to create a dynamic search input based on the class names within the span tags. However, I am struggling with displaying the class name that I have identified. My goal is to show the class names that match the entered value in the input on the ...

Two cascading style sheets (CSS) for an HTML document

Apologies for my poor English... I am facing a small issue with HTML commands. The problem is that I have two CSS commands with the same name (I hope this makes sense). Secondary Navigation Primary Navigation Is there a way to separate them? My tutor ...

Developing precision in Material UI Next and React

My goal is to establish a specific class in Material UI Next that functions similarly to pseudo classes, but for nested elements. For instance, I can define a class with styles within Material UI Next (MUI-Next) like this: const styles = { appbar: { ...

Transferring information via onSubmit from React to Node.js

In my React script, I have an onSubmit function that is triggered when the submit button is clicked: const handleSubmit = (event) => { event.preventDefault(); const form = event.target; const rawdata = new FormData(form); const data = { email: rawda ...

ReactJS continuously updating data with new props

Being a beginner in React, I'm still trying to navigate my way around the framework. I currently have a component that fetches data in order to render an HTML table. So, I utilize my Actions' fetchData() (which makes use of the browser's fe ...

What is the best way to align the navigation with a maximum of three elements (lists) in a row using responsive design?

I've been trying various methods but haven't been able to center it while keeping the padding intact. It's a mobile navigation bar. Here is what my code is producing currently: [1]: https://i.sstatic.net/sHEjj.png This is how I want it to ...

Why do images show up on Chrome and Mozilla but not on IE?

I have tested the code below. The images display in Chrome and Mozilla, but not in IE. The image format is .jpg. Can someone please assist? bodycontent+='<tr class="span12"><td class="span12"><div class="span12"><img class="span ...

Is it possible to integrate a personalized theme into react-dates?

Attempting to customize the styling of my react-dates DayPickerRangeController in Typescript using react-with-styles and Aphrodite. I have implemented the following code, mirroring the code found at https://github.com/airbnb/react-dates#interfaces: const ...