Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 967

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T03:46:08+00:00 2024-11-25T03:46:08+00:00

How to Build a Multi-Toggle Component Using TailwindCSS

  • 62k

Multi-toggle Component

When building a web application, toggling an element is one of the key features you are likely to come across and may need to implement in your project.

There are various ways you can toggle an element. In this section, we will be working on a multi-toggle component. That's we will be able to toggle the design of multiple elements in a structure.

Without wasting too much time, let's get started

Multi-toggle demo

Understanding the Task

We will be building a multi-toggle component, we can toggle or select multiple elements. For this exercise, we will be using TailwindCSS and a small piece of JavaScript. This session will be very short and straightforward.

Structure of Code

Our code root code is still the same as in all our tutorials.

<body> <!-- First Layer -->  <div> <!-- Second Layer -->   <div>         <!--                  Here Goes the content           -->   </div>  </div>  </body> 
Enter fullscreen mode Exit fullscreen mode

We will be building this component as one, all the different categories will be inside a big container and general styling will be added to them from the parent container as follows

 <div>                     <!-- Header -->     <div class="text-center mb-5">          <h2 class="text-2xl mb-3">Browse by Interest</h2>          <p class="w-[80%] sm:w-[50%] text-sm mx-auto">Select some interest to help us personalize your experience on Edmondo</p>     </div>                     <!-- Categories -->     <div class="grid grid-cols-2 md:grid-cols-3 gap-4 sm:gap-10 mb-10 [&>*]:px-4 [&>*]:h-14 [&>*]:flex [&>*]:items-center [&>*]:justify-center [&>*]:bg-[#16181f] [&>*]:border-2 [&>*]:border-[#171920] [&>*]:text-xs [&>*]:sm:text-sm [&>*]:text-slate-400 [&>*]:rounded-sm [&>*]:cursor-pointer [&>*:hover]:border-rose-500 [&>*:hover]:shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] [&>*:hover]:text-slate-100">                 <div class="element">                     <h2>Math</h2>                 </div>                 <div class="element">                     <h2>Science</h2>                 </div>                 <div class="element border-rose-500 shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] text-slate-100">                     <h2>English</h2>                 </div>                 <div class="element">                     <h2>World Languages</h2>                 </div>                 <div class="element border-rose-500 shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] text-slate-100">                     <h2>Health & PE</h2>                 </div>                 <div class="element">                     <h2>Special Education</h2>                 </div>                 <div class="element">                     <h2>Development</h2>                 </div>                 <div class="element">                     <h2>Technology</h2>                 </div>                 <div class="element border-rose-500 shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] text-slate-100">                     <h2>Social Studies</h2>                 </div>  </div> 
Enter fullscreen mode Exit fullscreen mode

To the header of this design, we applied some basic stylings font-size of text-2xl for the topmost header and text-sm for the lower header

The majority of the styling was applied to the different categories. To apply general styling to the different categories from the parent container, we made use of the [&>*] property of TailwindCSS.

The property [&>*] simply means “select each child individually”, this allows us to apply the same styling properties to all the immediate children.

To each of the different categories, we gave it a padding-inline of px-4 and a height of h-14. We gave each of them a display of flex and centered their content by using flex items-center justify-center. We also gave them background-color of bg-[#16181f], a border-width, border-radius and color of border-2 border-[#171920] rounded-sm respectively. Each of these categories has a hover effect of [&>*:hover]:border-rose-500 [&>*:hover]:shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] [&>*:hover]:text-slate-100

Trust me, that's pretty much it for the Styling, and the UI of our component.

The whole code looks like this

<body class="bg-[#22242c] flex justify-center items-center min-h-screen">                 <!-- First layer -->     <div class="bg-[#1b1d25] w-full sm:max-w-3xl py-9 px-9 text-slate-200 relative">                 <!-- Close icon -->         <div class="absolute top-2 right-2 w-10 h-10 rounded-full text-2xl bg-[#16181f] text-slate-400 flex items-center justify-center cursor-pointer hover:text-slate-100 ">             &times;         </div>          <div>                 <!-- Header -->             <div class="text-center mb-5">                 <h2 class="text-2xl mb-3">Browse by Interest</h2>                 <p class="w-[80%] sm:w-[50%] text-sm mx-auto">Select some interest to help us personalize your experience on Edmondo</p>             </div>                 <!-- Categories container -->             <div class="grid grid-cols-2 md:grid-cols-3 gap-4 sm:gap-10 mb-10 [&>*]:px-4 [&>*]:h-14 [&>*]:flex [&>*]:items-center [&>*]:justify-center [&>*]:bg-[#16181f] [&>*]:border-2 [&>*]:border-[#171920] [&>*]:text-xs [&>*]:sm:text-sm [&>*]:text-slate-400 [&>*]:rounded-sm [&>*]:cursor-pointer [&>*:hover]:border-rose-500 [&>*:hover]:shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] [&>*:hover]:text-slate-100">                 <div class="element">                     <h2>Math</h2>                 </div>                 <div class="element">                     <h2>Science</h2>                 </div>                 <div class="element border-rose-500 shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] text-slate-100">                     <h2>English</h2>                 </div>                 <div class="element">                     <h2>World Languages</h2>                 </div>                 <div class="element border-rose-500 shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] text-slate-100">                     <h2>Health & PE</h2>                 </div>                 <div class="element">                     <h2>Special Education</h2>                 </div>                 <div class="element">                     <h2>Development</h2>                 </div>                 <div class="element">                     <h2>Technology</h2>                 </div>                 <div class="element border-rose-500 shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] text-slate-100">                     <h2>Social Studies</h2>                 </div>             </div>                  <!-- Navigation Buttons -->             <div class="flex gap-5 justify-center [&>*]:px-6 [&>*]:py-2 [&>*]:border [&>*]:rounded-lg [&>*]:cursor-pointer">                 <div class="border-slate-900 hover:bg-slate-800 hover:text-white">                     <h2>Skip</h2>                 </div>                  <div class="border-rose-500 bg-rose-500 text-white hover:text-rose-500 hover:bg-transparent">                     <h2>Save</h2>                 </div>             </div>         </div>     </div>      <script src="index.js"></script> </body> 
Enter fullscreen mode Exit fullscreen mode

It is worth mentioning that we gave every category a custom class = “element”, this is the class we will use to add the toggling effect to the different categories.

JavaScript

The JavaScript here is mainly to add a toggling effect to the different categories when clicked.

const elem = document.querySelectorAll(".element");  console.log(elem);  elem.forEach(item => {     item.addEventListener("click", ()=> {         item.classList.toggle("shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)]");      })  }) 
Enter fullscreen mode Exit fullscreen mode

We started by selecting all the node elements having the class=”element” using the document.querySelectorAll(“.element”) method and store them in the constant elem.

elem is now a nodeArray having all the different categories.

Using the forEach method in this array, to each of these categories, we applied an on-click event to it, such that when clicked, the shadow-[0rem_0rem_0.3rem_0.2rem_rgba(244,63,94,0.8)] is toggled.

And that's pretty much it for this tutorial session.

End Result

Conclusion

We just built a Help Center Component 🤩. And I bet you realized it was very easy than we thought in the beginning 😏.

You can have a Live preview on Codepen or find the code on GitHub

Don’t hesitate to share with me if you were able to complete the tutorial on your end, I’d be happy to see any additional component and styling you added to your work

If you have any worries or suggestions, don’t hesitate to bring them up! 😊

See ya! 👋

htmltailwindcsswebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.