Components using only HTML and CSS

Nuno Andrade
Dec 21, 2016 · 2 min read

First things first, I want to start saying that i am really an enthusiastic about CSS, I really enjoy to push further what we can do with just a couple lines of HTML & CSS.

When I started working in web development, I remember spending some days just researching jquery plugins to help me build some components such as modals, dropdowns, tabs, accordions etc., etc … It was as easy as adding the plugins script, initialise the plugin, add the css to it and voila it was working.

And it was this way until we started caring about performance. So we started replacing those jquery plugins to native javascript. At that point I started wondering, how it would be if we could do that with just pure HTML & CSS?

The answer to that is a big yes, yes you can do a lot of HTML & CSS only components, and even if you have to care about some old browsers the answer still remains a big yes

For example you can build a nice modal:

This example consists in having an anchor to our modal.

<a href="#modalDialog">Open Modal!</a>

Then and with the help of :target pseudo class we match if the url is the same as the modal id, and if it is we show up the modal.

<section id=”modalDialog” class=”modalDialog” role=”dialog” aria-labelledby=”modal-label” aria-hidden=”true”>

And this is the css that is triggered when we click the anchor link.

.modalDialog:target {
opacity: 1;
pointer-events: auto;
}

What about tooltips? Yes. And yes you read it right, you can build tooltips just with HTML & CSS:

If you look at the HTML we have a data-attribute that is called ‘data-tooltip’, where we put the content that will show up in our tooltip.

<span class="tooltip" data-tooltip="I found what I need. And it's not friends, it's things." data-tooltip-pos="right" data-tooltip-length="medium">Hover Me</span>

We also have two others data-attributes, one is the tooltip position ‘data-tooltip-pos=”right”’, and the other one is the tooltip width ’data-tooltip-length=”medium”’.

But how does it displays the tooltip content from the data attribute?

You can access that title attribute from the content property, as you can see in the code below:

.tooltip:after {
content: attr(data-tooltip);
}

Modals and Tooltips were just two examples of what you can do, using css pseudo classes like :target, or :checked along with checkboxes and radio buttons, you can do a lot of plugins using only HTML & CSS like the one’s above without any line of javascript code.

And well there is a lot more to coming with CSS Selectors Level 4.

The UI files

Ramblings on UI development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store