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.
Top comments (85)
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.
codepen.io/jcoulterdesign/pen/NOMeEb
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
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
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.
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.
Thank you! Glad you found it helpful
Taleek مهكر
ome tv مهكر
smule مهكر
<a href="https://gmdroid.com/%D8%AA%D8%AD%D9%85%D9%8A%D9%84-%D8%AA%D8%B7%D8%A8%D9%8A%D9%82-remini-%D9%85%D9%87%D9%83%D8%B1-2022-%D9%84%D9%84%D8%A7%D9%86%D8%AF%D8%B1%D9%88%D9%8A%D8%AF/" class="mozbar-highlight-followed-wGA7MhRhQ3WS">remini</a>
تنزيل ببجي نيو ستيت
truckers of europe 3 مهكرة
تحميل فيفا 22 مجانا
dream zone مهكرة
تنزيل hello neighbor 2 للاندرويد
viu مهكر apk
shazam مهكر 2023
Why do you think that it's a downside?
Yacine TV APK
KineMaster Pro APK
Gta 5 APK
Battlefield Mobile APK
Stumble Guys MOD APK
TikTok MOD APK
pubg mobile apk
Drive Zone Online APK
eFootball 2023
new state mobile
total football apk
total football apk
CRSED: Cuisine Royale APK
Alight Motion Pro APK
snaptube premium apk
FMWhatsApp APK
capcut pro apk
Wattpad Premium APK
Fortnite APK
PUBG MOBILE LITE
Minecraft APK MOD
netflix premium apk
hbo max apk premium
Remini Pro APK
Instabridge Premium APK
Amazon Music Premium APK
Adobe Express Premium APK
genshin impact mod apk
Telegram MOD APK
VSCO Premium APK
The Division Resurgence APK
Photoshop Express Premium APK
Spotify Premium APK
YouTube Vanced APK
ES File Explorer Pro
store-arab.com
store-arab.com/2021/09/Gradient-Pr...
store-arab.com/2020/11/egybest-ad-...
store-arab.com/2020/09/duolingo-pl...
store-arab.com/2021/02/dream-leagu...
store-arab.com/2020/09/vivavideo-p...
store-arab.com/2021/03/camscanner-...
store-arab.com/2020/09/powerdirect...
store-arab.com/2020/09/2022-minecr...
store-arab.com/2020/09/tiktok-prem...
store-arab.com/2021/12/YTV-Player-...
store-arab.com/2020/09/pixaloop-pr...
store-arab.com/2021/01/snaptube-vi...
store-arab.com/2020/08/zarchiver-P...
store-arab.com/2021/04/flipaclip-p...
store-arab.com/2020/11/airbrush-pr...
store-arab.com/2022/03/Shahid-vip....
store-arab.com/2020/08/black-tv-Pr...
store-arab.com/2021/03/vivacut-pro...
store-arab.com/2021/03/remini-pro-...
store-arab.com/2020/08/lucky-patch...
store-arab.com/2020/08/ufo-vpn-pre...
store-arab.com/2020/09/kinemaster-...
store-arab.com/2020/10/vsco-pro-Mo...
store-arab.com/2021/02/picsart-gol...
store-arab.com/2020/10/faceapp-pro...
store-arab.com/2020/08/subway-surf...
store-arab.com/2020/09/whatsapp-20...
store-arab.com/2020/11/reface-pro....
store-arab.com/2021/08/google-play...
store-arab.com/2021/01/truecaller-...
store-arab.com/2020/11/filmorago-p...
larbi-tech.com
apkrio.com
store-arab.com/2021/11/2022-pubg-m...
store-arab.com/2021/08/Cupcut-Pro....
store-arab.com/2021/11/tempo-pro-m...
store-arab.com/2021/04/alight-moti...
store-arab.com/2020/11/reface-pro...."
store-arab.com/2021/03/adobe-light...
store-arab.com/2021/03/2021-spotif...
store-arab.com/2020/09/instagram-m...
store-arab.com/2020/08/yacinetv-Ad...
store-arab.com/2020/09/deezer-Prem...
store-arab.com/2021/04/inshot-pro-...
Some comments may only be visible to logged-in visitors. Sign in to view all comments.