Cover image for You can create these elements without JavaScript

You can create these elements without JavaScript

adrianbdesigns profile image Adrian Bece Updated on ・3 min read

We've got used to writing JavaScript for common UI element functionality like accordions, tooltips, text truncation, etc. But as HTML and CSS got new features and older browsers were no longer supported, we've been able to use less and less JavaScript for creating UI elements and focusing it more on logic part of the code (validation, data processing, etc.).

Some solutions do still feel hacky and not flexible, but these are useful on smaller projects and if those are single-instance elements. Why write JavaScript function (or use jQuery if you feel nostalgic) for a single accordion on your website? That was my thought process when adding an accordion on the footer for mobile-only on my personal website.

Alt Text

Accordion with no JavaScript!

Here are some examples of elements you can create with no JavaScript.

Responsive text truncation

CSS text truncation is very simple to implement and is very performant since we are not editing the HTML content of the text, only its render. While single-line text truncation is well supported in older browsers, multi-line text truncation is only supported on newer browsers.

Star rating input

Star ratings are a must-have element of most review forms and functionalities. There are many ways of implementing this with CSS: using a background image, JavaScript, icons, etc. The most accessible way of implementing this is with icons and native radio inputs.

The downside of this implementation is that the input HTML radio inputs are in the reverse order (from 5 to 1 rating value) because we need to select all the starts up to and including the checked input which is not possible to select with CSS. That's why we reverse the order and select inputs from the checked input to the first input.

This implementation is very flexible and can be easily customized.

Tooltip / dropdown menu

This is a very flexible element because it's CSS logic can be used for both tooltips and dropdown menus, because they work in a similar way and both support the hover and click (touch) functionalities.

Something that can be considered an issue with this implementation is that due to it's focus styles, the tooltip (dropdown) will remain open on click until the user clicks away from the element (element uses focus).


This a bit of a hacky implementation that relies entirely on the query string of the URL. Id in the URL needs to match the modal element that we need to open.

Floating label input

I've covered floating label inputs implementation in a separate article since this implementation is a bit more complex.

Toggle / Accordion

Recently, HTML got its native accordion (toggle) element with <details> and <summary> elements, but the downside of using those elements is that they don't have many styling options, so developers still continue to use their own implementation. Luckily, with checkbox or radio input logic we can create toggleable elements and accordions without relying on JavaScript.

The downside of using this implementation is that it relies on input HTML element and it's logic resulting in extra HTML code needed, but also on the flip side, this results in a very accessible element.


As you can see, these CSS-only implementations rely on CSS selector logic like :focus and :placeholder-shown to replace JavaScript logic. Some of these CSS solutions can be considered hacky, but they are performant, flexible and don't rely on JavaScript.

I've used some of those CSS solutions in my projects, so I can avoid adding any extra JavaScript or avoid using JavaScript entirely for visual presentation.

Of course, there are many more CSS-only solutions out there, but I've found these ones most interesting. If you are using any other solutions, please do let me know in the comments.

These articles are fueled by coffee. So if you enjoy my work and found it useful, consider buying me a coffee! I would really appreciate it.

Buy Me A Coffee

Thank you for taking the time to read this post. If you've found this useful, please give it a ❤️ or 🦄, share and comment.

Posted on by:

adrianbdesigns profile

Adrian Bece


React, Frontend, Magento 2 certified developer. Magento PWA Studio contributor. Rock and metal music fan. Reads Dune, sci-fi novels and Calvin & Hobbes. Creates amazing interfaces @ prototyp.digital


markdown guide

If you think that relying on extra HTML is a downside (and I do, too) then solutions like the "pure CSS" floating label you've included are off the table. That uses extra divs and spans and data attributes.
I know people often say that HTML + CSS is "pure CSS" but it's definitely not!


Why do you think that it's a downside?


If you have a site and want to change its appearance, doing so in a stylesheet makes sense. Why would it make sense to go through the content, which is usually going to be held in a database by a CMS, to change the structure of things? There's no reason I can think of to marry style and content.


Cool Adrian, Nice😍😍 to see that we can make amazing things using only css. i am sure gonna use this Css only tricks in my projects.


Awesome, glad you like the article. If you enjoy creating cool stuff with CSS, check out the games that are created with only CSS.



Wow nice, Css is really awesome i would say.


Would these be accessible, with semantic markup and aria attributes? Sorry, posting from mobile, so can't verify.


Markup is semantic, but you can add aria tags according to your use case. These examples also work with basic keyboard navigation.


There are still a few a11ythings to consider.
You don't need to utilize the checkbox hack for the accordion, there's a tag for that: /.
When you use the checkbox hack, the input comes up in the form controls pane of screenreaders, where it doesn't really belong.

Also, giving an an empty href lets browsers consider it as a navigational element, where your example behaves like a button. The contents of a modal will also never be tab-able. The semantic tag for a modal would be , but I wouldn't know how to toggle that without js. I can't really think of a pure css solution that considers these points, though.

You didn't escape or wrap your tags properly, so we can't see the tag you tried to tell us. From my experience, I guess they are <details> and <summary>.


How do you operate the star rating with the keyboard?

Focus then arrow keys. Trouble is that there's no visible focus indicator. I seem to remember trying an implementation like this a while back but it didn't quite work right if JAWS was running.

I've tried to do something similar to the star rating, with the keyboard navigation and accessibility in mind.
It's not ideal but it tries to take into account accessibility and keyboard navigation as far as possible.

Code Example


Nice Adrian, I preach this a lot, I've had plenty of SEO success from reducing on-page JavaScript as Google isn't good at reading it 👌


Why would on-page JS affect SEO? As long as the content is on the page (with proper semantics) it should be visible to search engines?


On-Page JS tends to slow the site down, and speed IS a huge ranking factor (Note that even small amounts of JS can add up to a huge amount of work, especially with DOM mutations since they cannot be pre-composed). That is not to say all JS is bad, but there are plenty of cases where your JS is hurting your ability for your site to perform adequately.

It doesn't speed down the website if there are no DOM mutations at painting time.


Thank you! Glad you found it helpful


I'm not gonna lie, when I saw the title of this post I thought "oh great, another CSS hackfest that will be totally inaccessible"... but I gotta give you credit that it's not. Kudos. :)

That being said, I still think it's better to encourage people to just use a teeny bit of JS and do things the standard way instead of relying on CSS hacks. Far more robust/reliable for a11y.


Thank you, I'm really glad you've enjoyed it.


Another cool HTML5 tag is <datalist> element. Autocomplete without JS.
I haven't figured out how to style the autocomplete control though.


Half of these didn't work on iPhone for me. Nevertheless, cool stuff.


I've done a lot of these things with using radio and checkboxes and their states to change CSS for years. It's a pretty neat thing once you figure out their powers with CSS


Spectre CSS is a CSS framework without JS that can do this stuff.


Wow, haven't heard of it. Thanks for sharing, looks cool. In any case, by creating these components I've became more familiar with CSS, I've had to think outside the box and It was fun.


I love the 'tooltip' code.Thanks for writing this useful post.


Very useful, thanks for sharing


Nice collection; I particularly like the floating labels in the input fields. One thing about the star rating - css and html alone won't store the rating, so we'd need some code to do that.


Thanks. Yeah, you can use JavaScript or wrap the inputs in <form> element.


These are so good! Is there any way to check for support for this level of CSS in browser in case I'd need to fallback to a JS version?


I only used modernizr.com/ and it worked well for my usage. Not sure if there is any better solution out there.


Beautiful. It is indeed a great collection and surely encourages newbies..


Wowowooww that was pretty nice I like that method


This is really cool Adrian. Already thinking of ways to implement them


Thanks a lot for this, It's a huge boost for me.


Amazing tutorial.. Thx for sharing