• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Responsive Tabs and Accordion using HTML, CSS, and JavaScript Tutorial

vallncia

Password Cracker
V Rep
0
0
0
Rep
0
V Vouches
0
0
0
Vouches
0
Posts
48
Likes
104
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
This is a simple tutorial on how to create Responsive Tabs and Accordion using HTML, CSS, and JavaScript. The tutorial aims to provide the students and beginners with a reference for learning some usable user interfaces and functionality for building a responsive website. Here, snippets and scripts of a sample web page that demonstrate this tutorial's topic is provided and the source code zip file is also available and is free to download.

What is a Tabs?

In web applications, websites, or computer software, Tabs are inline buttons or menus that open another area that contains content. This feature is commonly used for separating the page/window contents to easily navigate. Implementing this kind of feature in a single-page website or program is a great idea to prevent long scrolls and organize the page contents.

What is an Accordion?

Accordion is a list of content headers that trigger to show the content below the item header when click. Unlike the tabs, this feature is displayed vertically and the contents are revealed below the clicked item header.

How to create Responsive Tabs and Accordion?

Nowadays, there are a lot of very useful CSS frameworks available that come with Tabs and Accordion components such as the Bootstrap Framework. Here, I will show you how these components are being made from scratch using HTML, CSS, and JavaScript. We can achieve these components by simply designing the HTML Elements into a Tab or Accordion type and adding some JS lines of code to make it functional.

I will be providing the scripts of a simple web page that contains a Tab and Accordion components. The web page will organize the contents of the page using Tab Menu when the window screen is wider than the mobile devices such as tablets, laptops, and desktops otherwise it will be shown using Accordion.

HTML

Here's the HTML script of the web page. It contains the elements that will be used for displaying the tab/accordion headers and contents.

index.html

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    JS - Responsive Tabs/Accordion</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  9. </

    head

    >
  10. <body

    >
  11. <h1

    style

    =

    "text-align:center"

    >

    Responsive Tabs/Accordion using HTML, CSS, and JavaScript</

    h1

    >
  12. <section

    >
  13. <div

    class

    =

    "custom-tabs-accordion"

    >
  14. <div

    class

    =

    "custom-tabs-accordion-header-container"

    >
  15. <div

    class

    =

    "custom-tabs-accordion-header active"

    data-target

    =

    "tab1"

    >
  16. <b

    >

    Tab 1</

    b

    >
  17. </

    div

    >
  18. <div

    class

    =

    "custom-tabs-accordion-header"

    data-target

    =

    "tab2"

    >
  19. <b

    >

    Tab 2</

    b

    >
  20. </

    div

    >
  21. <div

    class

    =

    "custom-tabs-accordion-header"

    data-target

    =

    "tab3"

    >
  22. <b

    >

    Tab 3</

    b

    >
  23. </

    div

    >
  24. </

    div

    >
  25. <div

    class

    =

    "custom-tabs-accordion-body-container"

    >
  26. <div

    class

    =

    "custom-tabs-accordion-body active"

    data-tab=

    "tab1"

    >
  27. <p

    >

    Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus omnis architecto, quasi totam deleniti dolorum dolor earum sequi ut sint ducimus nobis, quia nemo modi odio accusamus consequuntur reiciendis voluptatibus.</

    p

    >
  28. </

    div

    >
  29. <div

    class

    =

    "custom-tabs-accordion-body"

    data-tab=

    "tab2"

    >
  30. <p

    >

    In vulputate consequat sollicitudin. Suspendisse venenatis efficitur leo et finibus. Integer in dictum urna, sit amet mattis eros. Nullam ut mi efficitur, bibendum justo a, bibendum diam. Morbi ante justo, dignissim eu tristique eu, imperdiet at massa. Sed pellentesque dolor non massa malesuada, vitae molestie enim scelerisque. Aenean aliquet urna vel turpis consectetur scelerisque. Interdum et malesuada fames ac ante ipsum primis in faucibus. Suspendisse vitae rhoncus mauris, ut maximus mi. Proin tempor ut nisi vel lobortis. Maecenas iaculis arcu a ex pulvinar dictum. Pellentesque a elit non nibh venenatis hendrerit.</

    p

    >
  31. </

    div

    >
  32. <div

    class

    =

    "custom-tabs-accordion-body"

    data-tab=

    "tab3"

    >
  33. <p

    >

    Nam pulvinar vel metus nec porttitor. Fusce in est augue. Suspendisse et elit bibendum, porttitor sapien eget, malesuada dui. Mauris dapibus pretium sollicitudin. Pellentesque lobortis lacus eu nibh posuere, vitae pellentesque dolor pellentesque. Interdum et malesuada fames ac ante ipsum primis in faucibus. Proin tincidunt ornare neque, at finibus nisl varius a. Nullam fermentum magna at massa varius, nec malesuada elit lobortis. Sed ullamcorper libero et urna auctor, in faucibus velit tempus. Curabitur euismod porttitor diam nec finibus. Donec sodales in nulla eu aliquet. Morbi nec ipsum condimentum, aliquet turpis eu, dictum libero.</

    p

    >
  34. </

    div

    >
  35. </

    div

    >
  36. </

    div

    >
  37. </

    section

    >


  38. <script

    src

    =

    "script.js"

    ></

    script

    >
  39. </

    body

    >
  40. </

    html

    >

CSS

Here's the stylesheet of the web page design. It contains the styles scripts of the web page layout, tabs, and accordion.

style.css

  1. /**
  2. * Web Page Container Design
  3. */
  4. html,
  5. body{
  6. height

    :

    100%

    ;
  7. width

    :

    100%

    ;
  8. margin

    :

    0

    ;
  9. }
  10. body {
  11. background-color

    :

    #cec9c3

    ;
  12. padding

    :

    50px

    0px

    ;
  13. }
  14. section{
  15. padding

    :

    50px

    75px

    ;
  16. }

  17. /*
  18. * Styles for Tabs
  19. */
  20. .custom-tabs-accordion

    {
  21. position

    :

    relative

    ;
  22. }
  23. .custom-tabs-accordion

    .custom-tabs-accordion-header-container{
  24. display

    :

    flex;
  25. width

    :

    100%

    ;
  26. position

    :

    relative

    ;
  27. flex-direction

    :

    row;

  28. }

  29. .custom-tabs-accordion

    .custom-tabs-accordion-header-container

    .custom-tabs-accordion-header

    {
  30. border

    :

    1px

    solid

    gray

    ;
  31. padding

    :

    5px

    10px

    ;
  32. background

    :

    #f4f4f4

    ;
  33. cursor

    :

    pointer

    ;
  34. }
  35. .custom-tabs-accordion

    .custom-tabs-accordion-body-container{
  36. display

    :

    flex;
  37. flex-direction

    :

    column;
  38. position

    :

    relative

    ;
  39. }
  40. .custom-tabs-accordion

    .custom-tabs-accordion-body{
  41. border

    :

    1px

    solid

    #d8d8d8

    ;
  42. background

    :

    #fff

    ;
  43. overflow

    :

    hidden

    ;
  44. height

    :

    0

    ;
  45. transition

    :

    height .3s

    ease-in-out;
  46. position

    :

    absolute

    ;
  47. }

  48. .custom-tabs-accordion

    .custom-tabs-accordion-header

    .active,
  49. .custom-tabs-accordion

    .custom-tabs-accordion-header

    :

    hover

    {
  50. background

    :

    #dbd2d2

    ;
  51. }

  52. .custom-tabs-accordion

    .custom-tabs-accordion-body

    .active{
  53. height

    :

    auto

    ;
  54. padding

    :

    5px

    10px

    ;
  55. position

    :

    relative

    ;
  56. }

  57. /*
  58. * Styles for Accordion
  59. */

  60. @media

    (

    max-width

    :

    480px

    )

    {
  61. .custom-tabs-accordion

    .custom-tabs-accordion-header-container{
  62. display

    :

    flex;
  63. flex-direction

    :

    column;
  64. width

    :

    100%

    ;
  65. position

    :

    relative

    ;
  66. }

  67. .custom-tabs-accordion

    .custom-tabs-accordion-header

    .active,
  68. .custom-tabs-accordion

    .custom-tabs-accordion-header

    :

    hover

    {
  69. background

    :

    #dbd2d2

    ;
  70. }

  71. .custom-tabs-accordion

    .custom-tabs-accordion-body

    .active{
  72. height

    :

    auto

    ;
  73. position

    :

    relative

    ;
  74. padding

    :

    5px

    10px

    ;
  75. }
  76. }

I have added some comments on the stylesheets so you can easily find the style of the layout and components. The CSS file is included in the HTML script.

JavaScript

The below script is the JS script that contains the codes that make the accordion and tabs functional. It also contains the code for resizing the content containers when the window screen is resized.

script.css

  1. const

    tabsContainer =

    document.querySelector

    (

    '.custom-tabs-accordion'

    )
  2. const

    tabsHeader =

    document.querySelectorAll

    (

    '.custom-tabs-accordion-header'

    )
  3. const

    tabsBody =

    document.querySelector

    (

    '.custom-tabs-accordion-body-container'

    )

  4. /**
  5. * Display Active Tab/Accordion Body
  6. * @param {string} tab = tab header element
  7. */
  8. const

    TabsHeaderActive =

    function

    (

    tab)

    {
  9. var

    _target =

    tab.dataset

    .target
  10. tabsContainer.querySelectorAll

    (

    '.active'

    )

    .forEach

    (

    el =>

    {
  11. el.classList

    .remove

    (

    'active'

    )
  12. }

    )
  13. tab.classList

    .add

    (

    'active'

    )
  14. tabsContainer.querySelectorAll

    (

    '.custom-tabs-accordion-body'

    )

    .forEach

    (

    el=>

    {
  15. el.style

    .height

    =

    0
  16. }

    )
  17. tabsContainer.querySelector

    (

    `[

    data-

    tab=

    "${_target}"

    ]

    `)

    .style

    .height

    =

    tabsContainer.querySelector

    (

    `[

    data-

    tab=

    "${_target}"

    ]

    `)

    .scrollHeight

    +

    `px`
  18. tabsContainer.querySelector

    (

    `[

    data-

    tab=

    "${_target}"

    ]

    `)

    .classList

    .add

    (

    'active'

    )

  19. }
  20. tabsHeader.forEach

    (

    tab =>

    {

  21. /**
  22. * Tab/Accordion Header Click Event
  23. */
  24. tab.addEventListener

    (

    'click'

    ,

    function

    (

    )

    {
  25. TabsHeaderActive(

    tab)
  26. }

    )
  27. }

    )

    ;

  28. /**
  29. * Update Display Format to Tabs or Accordion
  30. */
  31. const

    TabsAccordionFormat =

    function

    (

    )

    {
  32. if

    (

    window.innerWidth

    <=

    480

    )

    {
  33. tabsBody.querySelectorAll

    (

    '.custom-tabs-accordion-body'

    )

    .forEach

    (

    el=>

    {
  34. var

    dataTab =

    el.dataset

    .tab
  35. var

    cloneEl =

    el.cloneNode

    (

    true

    )
  36. tabsContainer.querySelector

    (

    `.custom

    -

    tabs-

    accordion-

    header[

    data-

    target=

    "${dataTab}"

    ]

    `)

    .after

    (

    cloneEl)
  37. if

    (

    cloneEl.classList

    .contains

    (

    'active'

    )

    ==

    true

    )
  38. cloneEl.style

    .height

    =

    'auto'

    ;
  39. el.remove

    (

    )
  40. }

    )
  41. }

    else

    {
  42. document.querySelector

    (

    '.custom-tabs-accordion-header-container'

    )

    .querySelectorAll

    (

    '.custom-tabs-accordion-body'

    )

    .forEach

    (

    el=>

    {
  43. // var dataTab = el.dataset.tab
  44. var

    cloneEl =

    el.cloneNode

    (

    true

    )
  45. tabsBody.appendChild

    (

    cloneEl)
  46. if

    (

    cloneEl.classList

    .contains

    (

    'active'

    )

    ==

    true

    )
  47. cloneEl.style

    .height

    =

    'auto'

    ;
  48. el.remove

    (

    )
  49. }

    )

  50. }
  51. }
  52. TabsAccordionFormat(

    )

  53. /**
  54. * Event when the window screen is resized
  55. */
  56. window.onresize

    =

    function

    (

    )

    {
  57. TabsAccordionFormat(

    )
  58. }

The JS file is also included or loaded in the HTML file script.

Snapshots

Here are the images of the web page result using the given scripts.

Tab View

Mobile View

There you go! I have also provided the source code zip file on this website and it is free to download. You can download it by clicking the Download button located below this tutorial's content. Feel free to modify the source code the way you desire to meet your own requirements.

That's it! I hope this Creating a Responsive Tabs and Accordion using HTML, CSS, and JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top