How to Build Interactive Web Pages Without Using JavaScript

Hello my frontend developer friends, today i will be discussing an awesome thing with you guys which we could help you in writing less javascript code to create interactive elements. I will be using tailwind css v4 for the styling I'll be creating my code snippets on Scribbler.live first, which is a fantastic platform that allows you to run a JavaScript Notebook, Online Compiler, and Editor without the need for manual setup. Additionally, I am including a link to code snippets that includes all of the code examples so you can open the snippet and run it yourself to see the results. I will be using scrib.show from scribbler.live, it is equivalent to console.log Lets dive in... Table of contents How to create interactive elements without JS? Notification component Dropdown component Modal component Sidebar navigation component Conclusion How to create interactive elements without JS? We will be using few new css selectors which has good browser support now. Using these selectors in a creative way could help in creating many interactive elements like modal, dropdown, popup, sidebar nav, etc. has() - it allow us to check the states of the child elements and do the styling to the parent or child itself [&] - parent selector which allow us to select a element or state of element using class, ids, data attributes, etc and style itself or the child elements. Syntax: Notification component This notification is interactive and could be closed without Javascript X This is a simple logic, here we are checking on the parent element that if it has an input with checked state, then hide the entire element. Clicking the "X" will check the input and hide the element just like a notification element. Dropdown component Dropdown Content 1 Content 2 Content 3 Content 4 Content 5 In the dropdown component, we will have a initial height just to show the dropdown heading, when we check the input, it will increase the height of the parent element showing the dropdown content. On clicking it again will uncheck the input and hide the content. Modal component Open modal X Modal without javascript This modal is created with tailwind css has selector and no javascript is used for the interactivity This one is a bit tricky, here we are combining parent selector and has selector to check if the parent element has an input with class name "modal-btn" and if it is checked then show the content with class name "modal-content". Inside the modal content, we have another input checkbox with same class name, clicking it will uncheck the input and close the modal. We also has a backdrop element covering the entire screen when the modal is visible, it is also shown using the same way as modal. Breakdown of the classname: [&:has(.modal-btn:checked)_.modal-content] [&:has(.modal-btn:checked)] - this part is used to check whether the element has input with class name modal-btn with a checked state _.modal-content - Here underscore ( _ ) checks the element at nested level, it is checking for the modal-content classname using ".modal-content" dot notation. Sidebar navigation component Content 1 Content 2 Content 3 Content 4 Content 5 Content 6 Content 7 → ← This one is also a simple one, here we are setting the translate x property with negative value to move it outside the screen on the left side. When we click the right arrow icon, it will check the input and add the positive translate x value to make it visible on the screen. has-checked:[&_.left-arrow]:block has-checked:[&_.right-arrow]:hidden - These classes just hide and show the left/right arrows based on the sidebar visibility. Check out all the examples on scribbler.live and run it yourself to see the output from each of the examples above app.scribbler.live

Apr 4, 2025 - 09:30
 0
How to Build Interactive Web Pages Without Using JavaScript

Hello my frontend developer friends, today i will be discussing an awesome thing with you guys which we could help you in writing less javascript code to create interactive elements.

  • I will be using tailwind css v4 for the styling
  • I'll be creating my code snippets on Scribbler.live first, which is a fantastic platform that allows you to run a JavaScript Notebook, Online Compiler, and Editor without the need for manual setup.
  • Additionally, I am including a link to code snippets that includes all of the code examples so you can open the snippet and run it yourself to see the results.
  • I will be using scrib.show from scribbler.live, it is equivalent to console.log

Lets dive in...
Table of contents

  • How to create interactive elements without JS?
  • Notification component
  • Dropdown component
  • Modal component
  • Sidebar navigation component
  • Conclusion

How to create interactive elements without JS?

We will be using few new css selectors which has good browser support now. Using these selectors in a creative way could help in creating many interactive elements like modal, dropdown, popup, sidebar nav, etc.

  • has() - it allow us to check the states of the child elements and do the styling to the parent or child itself
  • [&] - parent selector which allow us to select a element or state of element using class, ids, data attributes, etc and style itself or the child elements.
  • Syntax:
 class="has-[h2]:bg-slate-900>
   

note">

class="[&_.warning]:bg-warning-600>

warning">