{"id":11383,"date":"2024-03-21T16:03:53","date_gmt":"2024-03-21T11:03:53","guid":{"rendered":"https:\/\/codehim.com\/?p=11383"},"modified":"2024-03-21T16:03:53","modified_gmt":"2024-03-21T11:03:53","slug":"collapsible-timeline-using-css3-and-vanilla-js","status":"publish","type":"post","link":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/","title":{"rendered":"Collapsible Timeline Using CSS3 and Vanilla JS"},"content":{"rendered":"<p>This code snippet helps you to create a collapsible timeline using CSS3 and Vanilla JS. It allows you to expand and collapse timeline items. The `itemAction` method manages the main functionality, which handles the expanding and collapsing of timeline items based on user interaction. It helps present chronological information in a visually organized and interactive manner.<\/p>\n<h2>How to Create Collapsible Timeline Using CSS3 and Vanilla JS<\/h2>\n<p>1. First of all, load the <a href=\"https:\/\/fonts.google.com\/\" target=\"_blank\" rel=\"noopener\">Google Fonts<\/a> by adding the following CDN link into the head tag of your HTML document.<\/p>\n<pre class=\"prettyprint linenums lang-html\">&lt;link rel='stylesheet' href='https:\/\/fonts.googleapis.com\/css2?family=IBM+Plex+Sans:wght@400;700&amp;display=swap'&gt;<\/pre>\n<p>2. Set up the HTML structure for our timeline. We&#8217;ll need a container element for the timeline and individual elements for each timeline item. Each item will consist of a header and a body.<\/p>\n<pre class=\"prettyprint linenums lang-html\">&lt;svg display=\"none\"&gt;\r\n\t&lt;symbol id=\"arrow\"&gt;\r\n\t\t&lt;polyline points=\"7 10,12 15,17 10\" fill=\"none\" stroke=\"currentcolor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" \/&gt;\r\n\t&lt;\/symbol&gt;\r\n&lt;\/svg&gt;\r\n&lt;h1&gt;A Brief History of Unix Time&lt;\/h1&gt;\r\n&lt;div id=\"timeline\" class=\"timeline\"&gt;\r\n\t&lt;div class=\"btn-group\"&gt;\r\n\t\t&lt;button class=\"btn\" type=\"button\" data-action=\"expand\"&gt;Expand All&lt;\/button&gt;\r\n\t\t&lt;button class=\"btn\" type=\"button\" data-action=\"collapse\"&gt;Collapse All&lt;\/button&gt;\r\n\t&lt;\/div&gt;\r\n\t&lt;div class=\"timeline__item\"&gt;\r\n\t\t&lt;div class=\"timeline__item-header\"&gt;\r\n\t\t\t&lt;button class=\"timeline__arrow\" type=\"button\" id=\"item1\" aria-labelledby=\"item1-name\" aria-expanded=\"false\" aria-controls=\"item1-ctrld\" aria-haspopup=\"true\" data-item=\"1\"&gt;\r\n\t\t\t\t&lt;svg class=\"timeline__arrow-icon\" viewBox=\"0 0 24 24\" width=\"24px\" height=\"24px\"&gt;\r\n\t\t\t\t\t&lt;use href=\"#arrow\" \/&gt;\r\n\t\t\t\t&lt;\/svg&gt;\r\n\t\t\t&lt;\/button&gt;\r\n\t\t\t&lt;span class=\"timeline__dot\"&gt;&lt;\/span&gt;\r\n\t\t\t&lt;span id=\"item1-name\" class=\"timeline__meta\"&gt;\r\n\t\t\t\t&lt;time class=\"timeline__date\" datetime=\"1970-01-01\"&gt;January 1, 1970&lt;\/time&gt;&lt;br&gt;\r\n\t\t\t\t&lt;strong class=\"timeline__title\"&gt;Unix Epoch&lt;\/strong&gt;\r\n\t\t\t&lt;\/span&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;div class=\"timeline__item-body\" id=\"item1-ctrld\" role=\"region\" aria-labelledby=\"item1\" aria-hidden=\"true\"&gt;\r\n\t\t\t&lt;div class=\"timeline__item-body-content\"&gt;\r\n\t\t\t\t&lt;p class=\"timeline__item-p\"&gt;This is the day the Unix clock began (or &lt;time datetime=\"1969-12-31\"&gt;December 31, 1969&lt;\/time&gt; if you live behind UTC &#x1f609;).&lt;\/p&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/div&gt;\r\n\t&lt;div class=\"timeline__item\"&gt;\r\n\t\t&lt;div class=\"timeline__item-header\"&gt;\r\n\t\t\t&lt;button class=\"timeline__arrow\" type=\"button\" id=\"item2\" aria-labelledby=\"item2-name\" aria-expanded=\"false\" aria-controls=\"item2-ctrld\" aria-haspopup=\"true\" data-item=\"2\"&gt;\r\n\t\t\t\t&lt;svg class=\"timeline__arrow-icon\" viewBox=\"0 0 24 24\" width=\"24px\" height=\"24px\"&gt;\r\n\t\t\t\t\t&lt;use href=\"#arrow\" \/&gt;\r\n\t\t\t\t&lt;\/svg&gt;\r\n\t\t\t&lt;\/button&gt;\r\n\t\t\t&lt;span class=\"timeline__dot\"&gt;&lt;\/span&gt;\r\n\t\t\t&lt;span id=\"item2-name\" class=\"timeline__meta\"&gt;\r\n\t\t\t\t&lt;time class=\"timeline__date\" datetime=\"1973-10-17\"&gt;October 17, 1973&lt;\/time&gt;&lt;br&gt;\r\n\t\t\t\t&lt;strong class=\"timeline__title\"&gt;Digits Within ISO 8601 Format&lt;\/strong&gt;\r\n\t\t\t&lt;\/span&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;div class=\"timeline__item-body\" id=\"item2-ctrld\" role=\"region\" aria-labelledby=\"item2\" aria-hidden=\"true\"&gt;\r\n\t\t\t&lt;div class=\"timeline__item-body-content\"&gt;\r\n\t\t\t\t&lt;p class=\"timeline__item-p\"&gt;At 6:36:57 PM UTC, the date in ISO 8601 format (1973-10-17) within the time digits (119731017) appeared for the first time.&lt;\/p&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/div&gt;\r\n\t&lt;div class=\"timeline__item\"&gt;\r\n\t\t&lt;div class=\"timeline__item-header\"&gt;\r\n\t\t\t&lt;button class=\"timeline__arrow\" type=\"button\" id=\"item3\" aria-labelledby=\"item3-name\" aria-expanded=\"false\" aria-controls=\"item3-ctrld\" aria-haspopup=\"true\" data-item=\"3\"&gt;\r\n\t\t\t\t&lt;svg class=\"timeline__arrow-icon\" viewBox=\"0 0 24 24\" width=\"24px\" height=\"24px\"&gt;\r\n\t\t\t\t\t&lt;use href=\"#arrow\" \/&gt;\r\n\t\t\t\t&lt;\/svg&gt;\r\n\t\t\t&lt;\/button&gt;\r\n\t\t\t&lt;span class=\"timeline__dot\"&gt;&lt;\/span&gt;\r\n\t\t\t&lt;span id=\"item3-name\" class=\"timeline__meta\"&gt;\r\n\t\t\t\t&lt;time class=\"timeline__date\" datetime=\"2001-09-09\"&gt;September 9, 2001&lt;\/time&gt;&lt;br&gt;\r\n\t\t\t\t&lt;strong class=\"timeline__title\"&gt;1 Billion Seconds&lt;\/strong&gt;\r\n\t\t\t&lt;\/span&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;div class=\"timeline__item-body\" id=\"item3-ctrld\" role=\"region\" aria-labelledby=\"item3\" aria-hidden=\"true\"&gt;\r\n\t\t\t&lt;div class=\"timeline__item-body-content\"&gt;\r\n\t\t\t\t&lt;p class=\"timeline__item-p\"&gt;Unix time reached 1,000,000,000 seconds at 1:46:40 AM UTC. The Danish UNIX User Group celebrated this in Copenhagen, Denmark.&lt;\/p&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/div&gt;\r\n\t&lt;div class=\"timeline__item\"&gt;\r\n\t\t&lt;div class=\"timeline__item-header\"&gt;\r\n\t\t\t&lt;button class=\"timeline__arrow\" type=\"button\" id=\"item4\" aria-labelledby=\"item4-name\" aria-expanded=\"false\" aria-controls=\"item4-ctrld\" aria-haspopup=\"true\" data-item=\"4\"&gt;\r\n\t\t\t\t&lt;svg class=\"timeline__arrow-icon\" viewBox=\"0 0 24 24\" width=\"24px\" height=\"24px\"&gt;\r\n\t\t\t\t\t&lt;use href=\"#arrow\" \/&gt;\r\n\t\t\t\t&lt;\/svg&gt;\r\n\t\t\t&lt;\/button&gt;\r\n\t\t\t&lt;span class=\"timeline__dot\"&gt;&lt;\/span&gt;\r\n\t\t\t&lt;span id=\"item4-name\" class=\"timeline__meta\"&gt;\r\n\t\t\t\t&lt;time class=\"timeline__date\" datetime=\"2009-02-13\"&gt;February 13, 2009&lt;\/time&gt;&lt;br&gt;\r\n\t\t\t\t&lt;strong class=\"timeline__title\"&gt;1,234,567,890 Seconds&lt;\/strong&gt;\r\n\t\t\t&lt;\/span&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;div class=\"timeline__item-body\" id=\"item4-ctrld\" role=\"region\" aria-labelledby=\"item4\" aria-hidden=\"true\"&gt;\r\n\t\t\t&lt;div class=\"timeline__item-body-content\"&gt;\r\n\t\t\t\t&lt;p class=\"timeline__item-p\"&gt;At 11:31:30 PM UTC, the digits of the time were 1234567890. This was celebrated worldwide, and even Google had a &lt;a href=\"https:\/\/www.google.com\/logos\/unix1234567890.gif\" target=\"_blank\" rel=\"noopener\"&gt;doodle&lt;\/a&gt; for it.&lt;\/p&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/div&gt;\r\n\t&lt;div class=\"timeline__item\"&gt;\r\n\t\t&lt;div class=\"timeline__item-header\"&gt;\r\n\t\t\t&lt;button class=\"timeline__arrow\" type=\"button\" id=\"item5\" aria-labelledby=\"item5-name\" aria-expanded=\"false\" aria-controls=\"item5-ctrld\" aria-haspopup=\"true\" data-item=\"5\"&gt;\r\n\t\t\t\t&lt;svg class=\"timeline__arrow-icon\" viewBox=\"0 0 24 24\" width=\"24px\" height=\"24px\"&gt;\r\n\t\t\t\t\t&lt;use href=\"#arrow\" \/&gt;\r\n\t\t\t\t&lt;\/svg&gt;\r\n\t\t\t&lt;\/button&gt;\r\n\t\t\t&lt;span class=\"timeline__dot\"&gt;&lt;\/span&gt;\r\n\t\t\t&lt;span id=\"item5-name\" class=\"timeline__meta\"&gt;\r\n\t\t\t\t&lt;time class=\"timeline__date\" datetime=\"2033-05-18\"&gt;May 18, 2033&lt;\/time&gt;&lt;br&gt;\r\n\t\t\t\t&lt;strong class=\"timeline__title\"&gt;2 Billion Seconds&lt;\/strong&gt;\r\n\t\t\t&lt;\/span&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;div class=\"timeline__item-body\" id=\"item5-ctrld\" role=\"region\" aria-labelledby=\"item5\" aria-hidden=\"true\"&gt;\r\n\t\t\t&lt;div class=\"timeline__item-body-content\"&gt;\r\n\t\t\t\t&lt;p class=\"timeline__item-p\"&gt;Unix time will reach 2,000,000,000 seconds at 3:33:20 AM UTC.&lt;\/p&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/div&gt;\r\n\t&lt;div class=\"timeline__item\"&gt;\r\n\t\t&lt;div class=\"timeline__item-header\"&gt;\r\n\t\t\t&lt;button class=\"timeline__arrow\" type=\"button\" id=\"item6\" aria-labelledby=\"item6-name\" aria-expanded=\"false\" aria-controls=\"item6-ctrld\" aria-haspopup=\"true\" data-item=\"6\"&gt;\r\n\t\t\t\t&lt;svg class=\"timeline__arrow-icon\" viewBox=\"0 0 24 24\" width=\"24px\" height=\"24px\"&gt;\r\n\t\t\t\t\t&lt;use href=\"#arrow\" \/&gt;\r\n\t\t\t\t&lt;\/svg&gt;\r\n\t\t\t&lt;\/button&gt;\r\n\t\t\t&lt;span class=\"timeline__dot\"&gt;&lt;\/span&gt;\r\n\t\t\t&lt;span id=\"item6-name\" class=\"timeline__meta\"&gt;\r\n\t\t\t\t&lt;time class=\"timeline__date\" datetime=\"2038-01-19\"&gt;January 19, 2038&lt;\/time&gt;&lt;br&gt;\r\n\t\t\t\t&lt;strong class=\"timeline__title\"&gt;Unix Epochalypse&lt;\/strong&gt;\r\n\t\t\t&lt;\/span&gt;\r\n\t\t&lt;\/div&gt;\r\n\t\t&lt;div class=\"timeline__item-body\" id=\"item6-ctrld\" role=\"region\" aria-labelledby=\"item6\" aria-hidden=\"true\"&gt;\r\n\t\t\t&lt;div class=\"timeline__item-body-content\"&gt;\r\n\t\t\t\t&lt;p class=\"timeline__item-p\"&gt;Also known as the year 2038 problem, clocks running on a 32-bit signed integer will flip from 3:14:08 AM UTC on this day to 8:45:52 PM UTC on December 13, 1901. Therefore, values only from -2,147,483,648 to 2,147,483,647 for the second are supported.&lt;\/p&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>3. Next, let&#8217;s define the styles for our timeline in a separate CSS file (<code>styles.css<\/code>).<\/p>\n<pre class=\"prettyprint linenums lang-css\">* {\r\n\tborder: 0;\r\n\tbox-sizing: border-box;\r\n\tmargin: 0;\r\n\tpadding: 0;\r\n}\r\n\r\n\r\n:root {\r\n\t--hue: 223;\r\n\t--bg: hsl(var(--hue),10%,90%);\r\n\t--fg: hsl(var(--hue),10%,10%);\r\n\t--primary: hsl(var(--hue),90%,50%);\r\n\t--trans-dur: 0.3s;\r\n\t--trans-timing: cubic-bezier(0.65,0,0.35,1);\r\n\tfont-size: calc(16px + (24 - 16) * (100vw - 320px) \/ (2560 - 320));\r\n}\r\na {\r\n\tcolor: var(--primary);\r\n\ttransition: color var(--trans-dur);\r\n}\r\nbody,\r\nbutton {\r\n\tcolor: var(--fg);\r\n\tfont: 1em\/1.5 \"IBM Plex Sans\", sans-serif;\r\n}\r\nbody {\r\n\tbackground-color: var(--bg);\r\n\theight: 100vh;\r\n\ttransition:\r\n\t\tbackground-color var(--trans-dur),\r\n\t\tcolor var(--trans-dur);\r\n}\r\nh1 {\r\n\tfont-size: 2em;\r\n\tmargin: 0 0 3rem;\r\n\tpadding-top: 1.5rem;\r\n\ttext-align: center;\r\n}\r\n.btn {\r\n\tbackground-color: var(--fg);\r\n\tborder-radius: 0.25em;\r\n\tcolor: var(--bg);\r\n\tcursor: pointer;\r\n\tpadding: 0.375em 0.75em;\r\n\ttransition:\r\n\t\tbackground-color calc(var(--trans-dur) \/ 2) linear,\r\n\t\tcolor var(--trans-dur);\r\n\t-webkit-tap-highlight-color: transparent;\r\n}\r\n.btn:hover {\r\n\tbackground-color: hsl(var(--hue),10%,50%);\r\n}\r\n.btn-group {\r\n\tdisplay: flex;\r\n\tgap: 0.375em;\r\n\tmargin-bottom: 1.5em;\r\n}\r\n.timeline {\r\n\tmargin: auto;\r\n\tpadding: 0 1.5em;\r\n\twidth: 100%;\r\n\tmax-width: 36em;\r\n}\r\n.timeline__arrow {\r\n\tbackground-color: transparent;\r\n\tborder-radius: 0.25em;\r\n\tcursor: pointer;\r\n\tflex-shrink: 0;\r\n\tmargin-inline-end: 0.25em;\r\n\toutline: transparent;\r\n\twidth: 2em;\r\n\theight: 2em;\r\n\ttransition:\r\n\t\tbackground-color calc(var(--trans-dur) \/ 2) linear,\r\n\t\tcolor var(--trans-dur);\r\n\t-webkit-appearance: none;\r\n\tappearance: none;\r\n\t-webkit-tap-highlight-color: transparent;\r\n}\r\n.timeline__arrow:focus-visible,\r\n.timeline__arrow:hover {\r\n\tbackground-color: hsl(var(--hue),10%,50%,0.4);\r\n}\r\n.timeline__arrow-icon {\r\n\tdisplay: block;\r\n\tpointer-events: none;\r\n\ttransform: rotate(-90deg);\r\n\ttransition: transform var(--trans-dur) var(--trans-timing);\r\n\twidth: 100%;\r\n\theight: auto;\r\n}\r\n.timeline__date {\r\n\tfont-size: 0.833em;\r\n\tline-height: 2.4;\r\n}\r\n.timeline__dot {\r\n\tbackground-color: currentColor;\r\n\tborder-radius: 50%;\r\n\tdisplay: inline-block;\r\n\tflex-shrink: 0;\r\n\tmargin: 0.625em 0;\r\n\tmargin-inline-end: 1em;\r\n\tposition: relative;\r\n\twidth: 0.75em;\r\n\theight: 0.75em;\r\n}\r\n.timeline__item {\r\n\tposition: relative;\r\n\tpadding-bottom: 2.25em;\r\n}\r\n.timeline__item:not(:last-child):before {\r\n\tbackground-color: currentColor;\r\n\tcontent: \"\";\r\n\tdisplay: block;\r\n\tposition: absolute;\r\n\ttop: 1em;\r\n\tleft: 2.625em;\r\n\twidth: 0.125em;\r\n\theight: 100%;\r\n\ttransform: translateX(-50%);\r\n}\r\n[dir=\"rtl\"] .timeline__arrow-icon {\r\n\ttransform: rotate(90deg);\r\n}\r\n[dir=\"rtl\"] .timeline__item:not(:last-child):before {\r\n\tright: 2.625em;\r\n\tleft: auto;\r\n\ttransform: translateX(50%);\r\n}\r\n.timeline__item-header {\r\n\tdisplay: flex;\r\n}\r\n.timeline__item-body {\r\n\tborder-radius: 0.375em;\r\n\toverflow: hidden;\r\n\tmargin-top: 0.5em;\r\n\tmargin-inline-start: 4em;\r\n\theight: 0;\r\n}\r\n.timeline__item-body-content {\r\n\tbackground-color: hsl(var(--hue),10%,50%,0.2);\r\n\topacity: 0;\r\n\tpadding: 0.5em 0.75em;\r\n\tvisibility: hidden;\r\n\ttransition:\r\n\t\topacity var(--trans-dur) var(--trans-timing),\r\n\t\tvisibility var(--trans-dur) steps(1,end);\r\n}\r\n.timeline__meta {\r\n\twidth: 100%;\r\n}\r\n.timeline__title {\r\n\tfont-size: 1.5em;\r\n\tline-height: 1.333;\r\n}\r\n\/* Expanded state *\/\r\n.timeline__item-body--expanded {\r\n\theight: auto;\r\n}\r\n.timeline__item-body--expanded .timeline__item-body-content {\r\n\topacity: 1;\r\n\tvisibility: visible;\r\n\ttransition-delay: var(--trans-dur), 0s;\r\n}\r\n.timeline__arrow[aria-expanded=\"true\"] .timeline__arrow-icon {\r\n\ttransform: rotate(0);\r\n}\r\n\r\n\/* Dark theme *\/\r\n@media (prefers-color-scheme: dark) {\r\n\t:root {\r\n\t\t--bg: hsl(var(--hue),10%,10%);\r\n\t\t--fg: hsl(var(--hue),10%,90%);\r\n\t\t--primary: hsl(var(--hue),90%,70%);\r\n\t}\r\n}<\/pre>\n<p>4. Now, let&#8217;s implement the JavaScript functionality to make our timeline collapsible. This script will handle user interactions and animate the expansion and collapse of timeline items.<\/p>\n<pre class=\"prettyprint linenums lang-js\">window.addEventListener(\"DOMContentLoaded\",() =&gt; {\r\n\tconst ctl = new CollapsibleTimeline(\"#timeline\");\r\n});\r\n\r\nclass CollapsibleTimeline {\r\n\tconstructor(el) {\r\n\t\tthis.el = document.querySelector(el);\r\n\r\n\t\tthis.init();\r\n\t}\r\n\tinit() {\r\n\t\tthis.el?.addEventListener(\"click\",this.itemAction.bind(this));\r\n\t}\r\n\tanimateItemAction(button,ctrld,contentHeight,shouldCollapse) {\r\n\t\tconst expandedClass = \"timeline__item-body--expanded\";\r\n\t\tconst animOptions = {\r\n\t\t\tduration: 300,\r\n\t\t\teasing: \"cubic-bezier(0.65,0,0.35,1)\"\r\n\t\t};\r\n\r\n\t\tif (shouldCollapse) {\r\n\t\t\tbutton.ariaExpanded = \"false\";\r\n\t\t\tctrld.ariaHidden = \"true\";\r\n\t\t\tctrld.classList.remove(expandedClass);\r\n\t\t\tanimOptions.duration *= 2;\r\n\t\t\tthis.animation = ctrld.animate([\r\n\t\t\t\t{ height: `${contentHeight}px` },\r\n\t\t\t\t{ height: `${contentHeight}px` },\r\n\t\t\t\t{ height: \"0px\" }\r\n\t\t\t],animOptions);\r\n\t\t} else {\r\n\t\t\tbutton.ariaExpanded = \"true\";\r\n\t\t\tctrld.ariaHidden = \"false\";\r\n\t\t\tctrld.classList.add(expandedClass);\r\n\t\t\tthis.animation = ctrld.animate([\r\n\t\t\t\t{ height: \"0px\" },\r\n\t\t\t\t{ height: `${contentHeight}px` }\r\n\t\t\t],animOptions);\r\n\t\t}\r\n\t}\r\n\titemAction(e) {\r\n\t\tconst { target } = e;\r\n\t\tconst action = target?.getAttribute(\"data-action\");\r\n\t\tconst item = target?.getAttribute(\"data-item\");\r\n\r\n\t\tif (action) {\r\n\t\t\tconst targetExpanded = action === \"expand\" ? \"false\" : \"true\";\r\n\t\t\tconst buttons = Array.from(this.el?.querySelectorAll(`[aria-expanded=\"${targetExpanded}\"]`));\r\n\t\t\tconst wasExpanded = action === \"collapse\";\r\n\r\n\t\t\tfor (let button of buttons) {\r\n\t\t\t\tconst buttonID = button.getAttribute(\"data-item\");\r\n\t\t\t\tconst ctrld = this.el?.querySelector(`#item${buttonID}-ctrld`);\r\n\t\t\t\tconst contentHeight = ctrld.firstElementChild?.offsetHeight;\r\n\r\n\t\t\t\tthis.animateItemAction(button,ctrld,contentHeight,wasExpanded);\r\n\t\t\t}\r\n\r\n\t\t} else if (item) {\r\n\t\t\tconst button = this.el?.querySelector(`[data-item=\"${item}\"]`);\r\n\t\t\tconst expanded = button?.getAttribute(\"aria-expanded\");\r\n\r\n\t\t\tif (!expanded) return;\r\n\r\n\t\t\tconst wasExpanded = expanded === \"true\";\r\n\t\t\tconst ctrld = this.el?.querySelector(`#item${item}-ctrld`);\r\n\t\t\tconst contentHeight = ctrld.firstElementChild?.offsetHeight;\r\n\r\n\t\t\tthis.animateItemAction(button,ctrld,contentHeight,wasExpanded);\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<p>You can now add your timeline items within the <code>&lt;div id=\"timeline\" class=\"timeline\"&gt;<\/code> container in your HTML file. Each timeline item should have a header and a body, as demonstrated in the provided code.<\/p>\n<p>That&#8217;s all! hopefully, you have successfully created a Collapsible Timeline on your website. If you have any questions or suggestions, feel free to comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This code snippet helps you to create a collapsible timeline using CSS3 and Vanilla JS. It allows you to expand&#8230;<\/p>\n","protected":false},"author":1,"featured_media":11396,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[116],"tags":[],"class_list":["post-11383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vanilla-javascript"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Collapsible Timeline Using CSS3 and Vanilla JS &#8212; CodeHim<\/title>\n<meta name=\"description\" content=\"Here is a free code snippet to create a Collapsible Timeline Using CSS3 and Vanilla JS. You can view demo and download the source code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Collapsible Timeline Using CSS3 and Vanilla JS &#8212; CodeHim\" \/>\n<meta property=\"og:description\" content=\"Here is a free code snippet to create a Collapsible Timeline Using CSS3 and Vanilla JS. You can view demo and download the source code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/\" \/>\n<meta property=\"og:site_name\" content=\"CodeHim\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codehimofficial\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-21T11:03:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"960\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Asif Mughal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:site\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Asif Mughal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/\"},\"author\":{\"name\":\"Asif Mughal\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\"},\"headline\":\"Collapsible Timeline Using CSS3 and Vanilla JS\",\"datePublished\":\"2024-03-21T11:03:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/\"},\"wordCount\":225,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png\",\"articleSection\":[\"Vanilla JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/\",\"url\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/\",\"name\":\"Collapsible Timeline Using CSS3 and Vanilla JS &#8212; CodeHim\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png\",\"datePublished\":\"2024-03-21T11:03:53+00:00\",\"description\":\"Here is a free code snippet to create a Collapsible Timeline Using CSS3 and Vanilla JS. You can view demo and download the source code.\",\"breadcrumb\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#primaryimage\",\"url\":\"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png\",\"contentUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png\",\"width\":1280,\"height\":960,\"caption\":\"Collapsible Timeline Using CSS3 and Vanilla JS\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codehim.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Vanilla JavaScript\",\"item\":\"https:\/\/codehim.com\/category\/vanilla-javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Collapsible Timeline Using CSS3 and Vanilla JS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codehim.com\/#website\",\"url\":\"https:\/\/codehim.com\/\",\"name\":\"CodeHim\",\"description\":\"Web Design Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"alternateName\":\"Web Design Codes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codehim.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/codehim.com\/#organization\",\"name\":\"CodeHim - Web Design Code & Scripts\",\"url\":\"https:\/\/codehim.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"contentUrl\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"width\":280,\"height\":280,\"caption\":\"CodeHim - Web Design Code & Scripts\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/codehimofficial\",\"https:\/\/x.com\/CodeHimOfficial\",\"https:\/\/www.instagram.com\/codehim\/\",\"https:\/\/www.linkedin.com\/company\/codehim\",\"https:\/\/co.pinterest.com\/codehim\/\",\"https:\/\/www.youtube.com\/@codehim\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\",\"name\":\"Asif Mughal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"caption\":\"Asif Mughal\"},\"description\":\"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.\",\"sameAs\":[\"https:\/\/codehim.com\"],\"url\":\"https:\/\/codehim.com\/author\/asif-mughal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Collapsible Timeline Using CSS3 and Vanilla JS &#8212; CodeHim","description":"Here is a free code snippet to create a Collapsible Timeline Using CSS3 and Vanilla JS. You can view demo and download the source code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/","og_locale":"en_US","og_type":"article","og_title":"Collapsible Timeline Using CSS3 and Vanilla JS &#8212; CodeHim","og_description":"Here is a free code snippet to create a Collapsible Timeline Using CSS3 and Vanilla JS. You can view demo and download the source code.","og_url":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/","og_site_name":"CodeHim","article_publisher":"https:\/\/www.facebook.com\/codehimofficial","article_published_time":"2024-03-21T11:03:53+00:00","og_image":[{"width":1280,"height":960,"url":"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png","type":"image\/png"}],"author":"Asif Mughal","twitter_card":"summary_large_image","twitter_creator":"@CodeHimOfficial","twitter_site":"@CodeHimOfficial","twitter_misc":{"Written by":"Asif Mughal","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#article","isPartOf":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/"},"author":{"name":"Asif Mughal","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed"},"headline":"Collapsible Timeline Using CSS3 and Vanilla JS","datePublished":"2024-03-21T11:03:53+00:00","mainEntityOfPage":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/"},"wordCount":225,"commentCount":0,"publisher":{"@id":"https:\/\/codehim.com\/#organization"},"image":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png","articleSection":["Vanilla JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/","url":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/","name":"Collapsible Timeline Using CSS3 and Vanilla JS &#8212; CodeHim","isPartOf":{"@id":"https:\/\/codehim.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#primaryimage"},"image":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png","datePublished":"2024-03-21T11:03:53+00:00","description":"Here is a free code snippet to create a Collapsible Timeline Using CSS3 and Vanilla JS. You can view demo and download the source code.","breadcrumb":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#primaryimage","url":"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png","contentUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2024\/03\/Collapsible-Timeline-Using-CSS3-and-Vanilla-JS.png","width":1280,"height":960,"caption":"Collapsible Timeline Using CSS3 and Vanilla JS"},{"@type":"BreadcrumbList","@id":"https:\/\/codehim.com\/vanilla-javascript\/collapsible-timeline-using-css3-and-vanilla-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codehim.com\/"},{"@type":"ListItem","position":2,"name":"Vanilla JavaScript","item":"https:\/\/codehim.com\/category\/vanilla-javascript\/"},{"@type":"ListItem","position":3,"name":"Collapsible Timeline Using CSS3 and Vanilla JS"}]},{"@type":"WebSite","@id":"https:\/\/codehim.com\/#website","url":"https:\/\/codehim.com\/","name":"CodeHim","description":"Web Design Code Snippets","publisher":{"@id":"https:\/\/codehim.com\/#organization"},"alternateName":"Web Design Codes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codehim.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codehim.com\/#organization","name":"CodeHim - Web Design Code & Scripts","url":"https:\/\/codehim.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/","url":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","contentUrl":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","width":280,"height":280,"caption":"CodeHim - Web Design Code & Scripts"},"image":{"@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codehimofficial","https:\/\/x.com\/CodeHimOfficial","https:\/\/www.instagram.com\/codehim\/","https:\/\/www.linkedin.com\/company\/codehim","https:\/\/co.pinterest.com\/codehim\/","https:\/\/www.youtube.com\/@codehim"]},{"@type":"Person","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed","name":"Asif Mughal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","caption":"Asif Mughal"},"description":"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.","sameAs":["https:\/\/codehim.com"],"url":"https:\/\/codehim.com\/author\/asif-mughal\/"}]}},"views":1784,"_links":{"self":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/11383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/comments?post=11383"}],"version-history":[{"count":3,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/11383\/revisions"}],"predecessor-version":[{"id":11417,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/11383\/revisions\/11417"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media\/11396"}],"wp:attachment":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media?parent=11383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/categories?post=11383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/tags?post=11383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}