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.
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).
Modal
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.
Let's create a floating label input with HTML and CSS only
Adrian Bece ・ Nov 6 '19
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.
Conclusion
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.
Thank you for taking the time to read this post. If you've found this useful, please give it a ❤️ or 🦄, share and comment.
Oldest comments (85)
This article is gold!!!
Thank you very much
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 👌
Thank you! Glad you found it helpful
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.
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.
codepen.io/jcoulterdesign/pen/NOMeEb
Wow nice, Css is really awesome i would say.
Awesome! I recently wrote about CSS games, I will definitely include this! 😍
8 Games to learn CSS the fun way
Andreas ・ Oct 24 '19 ・ 4 min read
This is really cool Adrian. Already thinking of ways to implement them
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.
very awesome!!!
تحميل العاب مهكرة للاندرويد و الايفون
تحميل لعبة PUBG NEW STATE مهكرة
تحميل لعبة قارينا فري فاير مهكرة
تحميل لعبة يلا لودو مهكرة
تحميل لعبة Ludo King™ مهكرة
تحميل لعبة Monster Box مهكرة
تحميل لعبة NBA 2K22 مهكرة
تحميل لعبة Water Sort Puzzle مهكرة
تحميل لعبة Twerk Race 3D مهكرة
تحميل لعبة Roblox مهكرة
امونج اس مهكرة
كول اوف ديوتي موبايل مهكرة
تحميل لعبة Talking Tom Gold Run مهكرة
ستيليا | تحميل العاب مهكرة للاندرويد و الايفون
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.
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
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..
Very useful, thanks for sharing
Wowowooww that was pretty nice I like that method
Half of these didn't work on iPhone for me. Nevertheless, cool stuff.
Thanks a lot for this, It's a huge boost for me.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.