Create a vertical Timeline using HTML, CSS and JavaScript

In this tutorial, we are going to learn how to create a timeline to show work experience or projects on a portfolio website. Here we will use HTML, CSS, and JavaScript.


The markup we will use to structure elements with the help of <div> tags, we divided every event inside the class section. 

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
    <div class="Main-container">
        <div class="top">
            <p>A Place Where You Find Solutions In Coding And Programming For PHP, WordPress, HTML, CSS, JavaScript, Python, C++ and much more.
                Hire us for your software development, mobile app development and web development project.
        <div class="middle">
            <div class="center-line"></div>
            <div class="section">
                <div class="rect-box"></div>
                <div class="content">
                    <h2>Web Development</h2>
                        <li>HTML and JavaScript QR Code Generator Script</li>
                        <li>Bulk Domain Authority Checker PHP Script</li>
                        <li>Blue AJAX Weather Forecast PHP Script</li>

            <div class="section">
                <div class="rect-box"></div>
                <div class="content">
                    <h2>Programming Blogs</h2>
                        <li>Get Base64 string of an image in JavaScript</li>
                        <li>Stop form submission in JavaScript</li>
                        <li>Database CRUD Operation with MySQL</li>
    <script src="script.js"></script>


After applying basic CSS, we will also style ::after pseudo-element.

We will differentiate the style because of :nth-child(odd). You can change the size of the section according to you need. If you want to change the middle line symbol then change the property of the rect-box.


    margin: 0px;
    padding: 0px;
    color: #fff;
    background-color: #32069b;
    padding-bottom: 400px;
    margin: 0 auto;
    max-width: 700px;
    text-align: center;
    padding: 4rem 0;
    max-width: 800px;
    margin-right: auto;
    position: relative;
    min-height: 150px;
    position: absolute;
    z-index: 2;
    left: calc(50% - 1px);
    width: 2px;
    top: -50px;
    background-color: rgb(19, 17, 17);
    display: none;
    height: 1.5rem;
    width: 1.5rem;
    position: absolute;
    display: block;
    content: '';
    background-color: rgb(0, 0, 0);
    left: 50%;
    transform: translateX(-50%);
    bottom: 0;
    position: absolute;
    display: block;
    height: 1rem;
    width: 1rem;
    border-radius: 100%;
    background-color: #fff;
    left: 50%;
    top: 20%;
    transform: translateX(-50%);
    display: flex;
    transition: 600ms ease;
    position: relative;
    z-index: 1;
    margin: 5px 0;
    padding: 0.5rem;
    align-items: center;
    min-height: 200px;
    flex-direction: row-reverse;
    transform: translateX(100%);
    width: calc(50% - 2rem);
    transform: none;
    opacity: 1;
    font-weight: 400px;
    letter-spacing: 1px;
    background-color: cornsilk;
    color: #027c31;
    background-color: rgb(255, 80, 80);
    padding: 5px;


To manipulate the HTML we are using DOM(Document Object Model) queries. Everything is an object in javascript. Here is the scrollHandler function which controls the window scroll, on the basis of that we make our section visible.

function query(selector, all = false) {
    return all ? document.querySelectorAll(selector) : document.querySelector(selector)
let sections = query('.section', true);
let middle = query('.middle');
let center_line = query('.center-line'); = `calc(100% - 20px)`;
let y0 = window.scrollY;
let up, down;
let full = false;
let set = 0;
const y = window.innerHeight * 0.8;
//function definition
let scrollHandler=function(e){
    } = window;
    up = scrollY < y0;
    down = !up;
    const middleRect = middle.getBoundingClientRect();
    const Rect = center_line.getBoundingClientRect();
    const dist = y -;
    if (down && !full){
        set = Math.max(set, dist);
   = `calc(100% - ${set}px)`;
    if (dist > middle.offsetHeight + 50 && !full){
        full = true; = `-50px`;
    sections.forEach(function(item) {
        const rect = item.getBoundingClientRect();

        if( + item.offsetHeight / 5 < y) {

    y0 = window.scrollY;
//function call
scrollHandler(); = 'block';
window.addEventListener('scroll', scrollHandler)


Create a vertical Timeline using HTML, CSS and JavaScript.

To make it responsive required to use the @media query in CSS. It will help you to resize your content based on screen size.

If you think this is helpful then please share it with your friends and give suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *