Published on

How To Create A Pure CSS Accordion With Tailwind CSS In 6 Easy Steps?

Tags
Pure CSS Accordion

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Pure CSS Accordion ui component

Pure css accordion built using tailwindcss. css is generated using the built tool by making tweaks to the tailwind.config.js

Why use Tailwind CSS to create a Pure CSS Accordion ui component?

  • It can make the building process of Pure CSS Accordion ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Pure CSS Accordion component file.

The preview of Pure CSS Accordion ui component

Free download of the Pure CSS Accordion's source code

The source code of Pure CSS Accordion ui component

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Tailwind Setup</title>
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css"
    />
    <style type="text/css">
      @import url("https://fonts.googleapis.com/css2?family=Rubik:wght@400;500;700;900&display=swap");
body {
  font-family: "Rubik", sans-serif;
}

// -------------------------------- code generated using the build tool ---------------------------

/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
html {
  line-height: 1.15;
  -webkit-text-size-adjust: 100%;
}
body {
  margin: 0;
}
main {
  display: block;
}
h1 {
  font-size: 2em;
  margin: 0.67em 0;
}
hr {
  box-sizing: content-box;
  height: 0;
  overflow: visible;
}
pre {
  font-family: monospace, monospace;
  font-size: 1em;
}
a {
  background-color: transparent;
}
abbr[title] {
  border-bottom: none;
  text-decoration: underline;
  -webkit-text-decoration: underline dotted;
  text-decoration: underline dotted;
}
b,
strong {
  font-weight: bolder;
}
code,
kbd,
samp {
  font-family: monospace, monospace;
  font-size: 1em;
}
small {
  font-size: 80%;
}
sub,
sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}
sub {
  bottom: -0.25em;
}
sup {
  top: -0.5em;
}
img {
  border-style: none;
}
button,
input,
optgroup,
select,
textarea {
  font-family: inherit;
  font-size: 100%;
  line-height: 1.15;
  margin: 0;
}
button,
input {
  overflow: visible;
}
button,
select {
  text-transform: none;
}
[type="button"],
[type="reset"],
[type="submit"],
button {
  -webkit-appearance: button;
}
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner,
button::-moz-focus-inner {
  border-style: none;
  padding: 0;
}
[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring,
button:-moz-focusring {
  outline: 1px dotted ButtonText;
}
fieldset {
  padding: 0.35em 0.75em 0.625em;
}
legend {
  box-sizing: border-box;
  color: inherit;
  display: table;
  max-width: 100%;
  padding: 0;
  white-space: normal;
}
progress {
  vertical-align: baseline;
}
textarea {
  overflow: auto;
}
[type="checkbox"],
[type="radio"] {
  box-sizing: border-box;
  padding: 0;
}
[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
  height: auto;
}
[type="search"] {
  -webkit-appearance: textfield;
  outline-offset: -2px;
}
[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
}
::-webkit-file-upload-button {
  -webkit-appearance: button;
  font: inherit;
}
details {
  display: block;
}
summary {
  display: list-item;
}
[hidden],
template {
  display: none;
}
blockquote,
dd,
dl,
figure,
h1,
h2,
h3,
h4,
h5,
h6,
hr,
p,
pre {
  margin: 0;
}
button {
  background-color: transparent;
  background-image: none;
}
button:focus {
  outline: 1px dotted;
  outline: 5px auto -webkit-focus-ring-color;
}
fieldset,
ol,
ul {
  margin: 0;
  padding: 0;
}
ol,
ul {
  list-style: none;
}
html {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
    Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji,
    Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
  line-height: 1.5;
}
*,
:after,
:before {
  box-sizing: border-box;
  border: 0 solid #e2e8f0;
}
hr {
  border-top-width: 1px;
}
img {
  border-style: solid;
}
textarea {
  resize: vertical;
}
input::-moz-placeholder,
textarea::-moz-placeholder {
  color: #a0aec0;
}
input:-ms-input-placeholder,
textarea:-ms-input-placeholder {
  color: #a0aec0;
}
input::-ms-input-placeholder,
textarea::-ms-input-placeholder {
  color: #a0aec0;
}
input::placeholder,
textarea::placeholder {
  color: #a0aec0;
}
[role="button"],
button {
  cursor: pointer;
}
table {
  border-collapse: collapse;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  font-size: inherit;
  font-weight: inherit;
}
a {
  color: inherit;
  text-decoration: inherit;
}
button,
input,
optgroup,
select,
textarea {
  padding: 0;
  line-height: inherit;
  color: inherit;
}
code,
kbd,
pre,
samp {
  font-family: Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace;
}
audio,
canvas,
embed,
iframe,
img,
object,
svg,
video {
  display: block;
  vertical-align: middle;
}
img,
video {
  max-width: 100%;
  height: auto;
}
.container {
  width: 100%;
}
@media (min-width: 640px) {
  .container {
    max-width: 640px;
  }
}
@media (min-width: 768px) {
  .container {
    max-width: 768px;
  }
}
@media (min-width: 1024px) {
  .container {
    max-width: 1024px;
  }
}
@media (min-width: 1280px) {
  .container {
    max-width: 1280px;
  }
}
.bg-gray-100 {
  --bg-opacity: 1;
  background-color: #f7fafc;
  background-color: rgba(247, 250, 252, var(--bg-opacity));
}
.bg-gray-800 {
  --bg-opacity: 1;
  background-color: #2d3748;
  background-color: rgba(45, 55, 72, var(--bg-opacity));
}
.bg-gray-900 {
  --bg-opacity: 1;
  background-color: #1a202c;
  background-color: rgba(26, 32, 44, var(--bg-opacity));
}
.bg-red-700 {
  --bg-opacity: 1;
  background-color: #c53030;
  background-color: rgba(197, 48, 48, var(--bg-opacity));
}
.border-gray-700 {
  --border-opacity: 1;
  border-color: #4a5568;
  border-color: rgba(74, 85, 104, var(--border-opacity));
}
.rounded {
  border-radius: 0.25rem;
}
.rounded-full {
  border-radius: 9999px;
}
.border {
  border-width: 1px;
}
.cursor-pointer {
  cursor: pointer;
}
.flex {
  display: flex;
}
.inline-flex {
  display: inline-flex;
}
.items-center {
  align-items: center;
}
.justify-center {
  justify-content: center;
}
.justify-between {
  justify-content: space-between;
}
.h-8 {
  height: 2rem;
}
.m-8 {
  margin: 2rem;
}
.mx-auto {
  margin-left: auto;
  margin-right: auto;
}
.mt-2 {
  margin-top: 0.5rem;
}
.mr-2 {
  margin-right: 0.5rem;
}
.mb-auto {
  margin-bottom: auto;
}
.ml-auto {
  margin-left: auto;
}
.max-h-0 {
  max-height: 0;
}
.group:focus .group-focus\:max-h-screen {
  max-height: 100vh;
}
.outline-none {
  outline: 0;
}
.overflow-hidden {
  overflow: hidden;
}
.p-2 {
  padding: 0.5rem;
}
.py-3 {
  padding-top: 0.75rem;
  padding-bottom: 0.75rem;
}
.py-4 {
  padding-top: 1rem;
  padding-bottom: 1rem;
}
.px-4 {
  padding-left: 1rem;
  padding-right: 1rem;
}
.pr-10 {
  padding-right: 2.5rem;
}
.fixed {
  position: fixed;
}
.absolute {
  position: absolute;
}
.relative {
  position: relative;
}
.top-0 {
  top: 0;
}
.right-0 {
  right: 0;
}
.bottom-0 {
  bottom: 0;
}
.text-center {
  text-align: center;
}
.text-justify {
  text-align: justify;
}
.text-white {
  --text-opacity: 1;
  color: #fff;
  color: rgba(255, 255, 255, var(--text-opacity));
}
.text-gray-400 {
  --text-opacity: 1;
  color: #cbd5e0;
  color: rgba(203, 213, 224, var(--text-opacity));
}
.text-gray-500 {
  --text-opacity: 1;
  color: #a0aec0;
  color: rgba(160, 174, 192, var(--text-opacity));
}
.group:focus .group-focus\:text-white {
  --text-opacity: 1;
  color: #fff;
  color: rgba(255, 255, 255, var(--text-opacity));
}
.w-8 {
  width: 2rem;
}
.w-full {
  width: 100%;
}
.transform {
  --transform-translate-x: 0;
  --transform-translate-y: 0;
  --transform-rotate: 0;
  --transform-skew-x: 0;
  --transform-skew-y: 0;
  --transform-scale-x: 1;
  --transform-scale-y: 1;
  transform: translateX(var(--transform-translate-x))
    translateY(var(--transform-translate-y)) rotate(var(--transform-rotate))
    skewX(var(--transform-skew-x)) skewY(var(--transform-skew-y))
    scaleX(var(--transform-scale-x)) scaleY(var(--transform-scale-y));
}
.group:focus .group-focus\:-rotate-180 {
  --transform-rotate: -180deg;
}
.transition {
  transition-property: background-color, border-color, color, fill, stroke,
    opacity, box-shadow, transform;
}
.duration-500 {
  transition-duration: 0.5s;
}
@media (min-width: 640px) {
  .sm\:container {
    width: 100%;
    max-width: 640px;
  }
  @media (min-width: 768px) {
    .sm\:container {
      max-width: 768px;
    }
  }
  @media (min-width: 1024px) {
    .sm\:container {
      max-width: 1024px;
    }
  }
  @media (min-width: 1280px) {
    .sm\:container {
      max-width: 1280px;
    }
  }
}
@media (min-width: 768px) {
  .md\:container {
    width: 100%;
  }
  @media (min-width: 640px) {
    .md\:container {
      max-width: 640px;
    }
  }
  @media (min-width: 768px) {
    .md\:container {
      max-width: 768px;
    }
  }
  @media (min-width: 1024px) {
    .md\:container {
      max-width: 1024px;
    }
  }
  @media (min-width: 1280px) {
    .md\:container {
      max-width: 1280px;
    }
  }
}
@media (min-width: 1024px) {
  .lg\:container {
    width: 100%;
  }
  @media (min-width: 640px) {
    .lg\:container {
      max-width: 640px;
    }
  }
  @media (min-width: 768px) {
    .lg\:container {
      max-width: 768px;
    }
  }
  @media (min-width: 1024px) {
    .lg\:container {
      max-width: 1024px;
    }
  }
  @media (min-width: 1280px) {
    .lg\:container {
      max-width: 1280px;
    }
  }
}
@media (min-width: 1280px) {
  .xl\:container {
    width: 100%;
  }
  @media (min-width: 640px) {
    .xl\:container {
      max-width: 640px;
    }
  }
  @media (min-width: 768px) {
    .xl\:container {
      max-width: 768px;
    }
  }
  @media (min-width: 1024px) {
    .xl\:container {
      max-width: 1024px;
    }
  }
  @media (min-width: 1280px) {
    .xl\:container {
      max-width: 1280px;
    }
  }
}

    </style>
  </head>
  <body class="bg-gray-100">
    <div class="container mx-auto">
      <div class="m-8 rounded overflow-hidden">
        <!-- accordion-tab  -->
        <div class="group outline-none accordion-section" tabindex="1">
          <div
            class="group bg-gray-900 flex justify-between px-4 py-3 items-center text-gray-500 transition ease duration-500 cursor-pointer pr-10 relative"
          >
            <div class="group-focus:text-white transition ease duration-500">
              Title for Tab - 1
            </div>
            <div
              class="h-8 w-8 border border-gray-700 rounded-full items-center inline-flex justify-center transform transition ease duration-500 group-focus:text-white group-focus:-rotate-180 absolute top-0 right-0 mb-auto ml-auto mt-2 mr-2"
            >
              <i class="fas fa-chevron-down"></i>
            </div>
          </div>
          <div
            class="group-focus:max-h-screen max-h-0 bg-gray-800 px-4 overflow-hidden ease duration-500"
          >
            <p class="p-2 text-gray-400 text-justify">
              Lorem ipsum dolor sit amet consectetur adipisicing elit. Fugiat,
              repellat amet doloribus consequuntur eos similique provident
              tempora voluptates iure quia fuga dicta voluptatibus culpa
              mollitia recusandae delectus id suscipit labore?
            </p>
          </div>
        </div>
        <!-- accordion-tab -->
        <!-- accordion-tab  -->
        <div class="group outline-none accordion-section" tabindex="2">
          <div
            class="group bg-gray-900 flex justify-between px-4 py-3 items-center text-gray-500 transition ease duration-500 cursor-pointer pr-10 relative"
          >
            <div class="group-focus:text-white transition ease duration-500">
              Title for Tab - 2
            </div>
            <div
              class="h-8 w-8 border border-gray-700 rounded-full items-center inline-flex justify-center transform transition ease duration-500 group-focus:text-white group-focus:-rotate-180 absolute top-0 right-0 mb-auto ml-auto mt-2 mr-2"
            >
              <i class="fas fa-chevron-down"></i>
            </div>
          </div>
          <div
            class="group-focus:max-h-screen max-h-0 bg-gray-800 px-4 overflow-hidden ease duration-500"
          >
            <p class="p-2 text-gray-400 text-justify">
              Lorem ipsum dolor sit amet consectetur adipisicing elit. Fugiat,
              repellat amet doloribus consequuntur eos similique provident
              tempora voluptates iure quia fuga dicta voluptatibus culpa
              mollitia recusandae delectus id suscipit labore?
            </p>
          </div>
        </div>
        <!-- accordion-tab -->
        <!-- accordion-tab  -->
        <div class="group outline-none accordion-section" tabindex="3">
          <div
            class="group bg-gray-900 flex justify-between px-4 py-3 items-center text-gray-500 transition ease duration-500 cursor-pointer pr-10 relative"
          >
            <div class="group-focus:text-white transition ease duration-500">
              Title for Tab - 3
            </div>
            <div
              class="h-8 w-8 border border-gray-700 rounded-full items-center inline-flex justify-center transform transition ease duration-500 group-focus:text-white group-focus:-rotate-180 absolute top-0 right-0 mb-auto ml-auto mt-2 mr-2"
            >
              <i class="fas fa-chevron-down"></i>
            </div>
          </div>
          <div
            class="group-focus:max-h-screen max-h-0 bg-gray-800 px-4 overflow-hidden ease duration-500"
          >
            <p class="p-2 text-gray-400 text-justify">
              Lorem ipsum dolor sit amet consectetur adipisicing elit. Fugiat,
              repellat amet doloribus consequuntur eos similique provident
              tempora voluptates iure quia fuga dicta voluptatibus culpa
              mollitia recusandae delectus id suscipit labore?
            </p>
          </div>
        </div>
        <!-- accordion-tab -->
      </div>
    </div>

    <!-- message -->
    <div class="w-full fixed bottom-0 text-center bg-red-700 py-4 text-white">
      Pure CSS Accordion Built using TailwindCSS. CSS is generated using the
      built tool by making tweaks to the <strong>tailwind.config.js</strong>
    </div>
    <!-- message -->
   
  </body>
</html>

How to create a Pure CSS Accordion with Tailwind CSS?

Install tailwind css of verion 1.4.6

Use the link html tag to import the stylesheet of Tailwind CSS of the version 1.4.6

<link href=https://unpkg.com/[email protected]/dist/tailwind.min.css rel="stylesheet">

All the unility class needed to create a Pure CSS Accordion component

  • bg-gray-100
  • mx-auto
  • m-8
  • overflow-hidden
  • bg-gray-900
  • flex
  • px-4
  • py-3
  • text-gray-500
  • pr-10
  • relative
  • group-focus:text-white
  • h-8
  • w-8
  • border-gray-700
  • inline-flex
  • absolute
  • top-0
  • right-0
  • mb-auto
  • ml-auto
  • mt-2
  • mr-2
  • group-focus:max-h-screen
  • max-h-0
  • bg-gray-800
  • p-2
  • text-gray-400
  • text-justify
  • w-full
  • fixed
  • bottom-0
  • text-center
  • bg-red-700
  • py-4
  • text-white

36 steps to create a Pure CSS Accordion component with Tailwind CSS

  1. Control the background color of an element to gray-100 using the bg-gray-100 utilities.

  2. Control the horizontal margin of an element to auto using the mx-auto utilities.

  3. Control the margin on all sides of an element to 2rem using the m-8 utilities.

  4. Use overflow-hidden to clip any content within an element that overflows the bounds of that element.

  5. Control the background color of an element to gray-900 using the bg-gray-900 utilities.

  6. Use flex to create a block-level flex container.

  7. Control the horizontal padding of an element to 1rem using the px-4 utilities.

  8. Control the vertical padding of an element to 0.75rem using the py-3 utilities.

  9. Control the text color of an element to gray-500 using the text-gray-500 utilities.

  10. Control the padding on right side of an element to 2.5rem using the pr-10 utilities.

  11. Use relative to position an element according to the normal flow of the document.

  12. Control the text color of an element to whiteundefined using the group-focus:text-white utilities.

  13. Use h-8 to set an element to a fixed height(2rem).

  14. Use w-8 to set an element to a fixed width(2rem).

  15. Control the border color of an element to gray-700 using the border-gray-700 utilities.

  16. Use inline-flex to create an inline flex container that flows with text.

  17. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  18. Use the top-0 utilities to set the top position of a positioned element to 0rem.

  19. Use the right-0 utilities to set the right position of a positioned element to 0rem.

  20. Control the margin on bottom side of an element to auto using the mb-auto utilities.

  21. Control the margin on left side of an element to auto using the ml-auto utilities.

  22. Control the margin on top side of an element to 0.5rem using the mt-2 utilities.

  23. Control the margin on right side of an element to 0.5rem using the mr-2 utilities.

  24. Set the maximum width/height of an element using the group-focus:max-h-screen utilitiesundefined.

  25. Set the maximum width/height of an element using the max-h-0 utilities.

  26. Control the background color of an element to gray-800 using the bg-gray-800 utilities.

  27. Control the padding on all sides of an element to 0.5rem using the p-2 utilities.

  28. Control the text color of an element to gray-400 using the text-gray-400 utilities.

  29. Control the text color of an element to justify using the text-justify utilities.

  30. Use w-full to set an element to a 100% based width.

  31. Use fixed to position an element relative to the browser window.

  32. Use the bottom-0 utilities to set the bottom position of a positioned element to 0rem.

  33. Control the text color of an element to center using the text-center utilities.

  34. Control the background color of an element to red-700 using the bg-red-700 utilities.

  35. Control the vertical padding of an element to 1rem using the py-4 utilities.

  36. Control the text color of an element to white using the text-white utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Pure CSS Accordion components, learn and follow along to implement your own components.