Web development today can be a whirlwind of various technologies, and even the simplest of widgets can often be complex under the hood.
Building a Tabs Widget
:focus pseudo-class in CSS.
:focus pseudo-class is used to target an element that has received focus by the user (either by using the keyboard or the mouse). It is supported in every browser including IE8+. Additionally, you can apply a
:focus state to any HTML element as long as you give it a
tabindex attribute indicates if an element can receive focus. It can take several values, including a negative value, zero, or a positive value. Each of these values determines what order an element should be focused on.
Demo for the Tabs Widget
In the HTML, each tab is a button with the content for each tab inside of a paragraph element. Each paragraph element is hidden, then set to
position: absolute so that the content will display in the same area for each tab. The first button element has the
autofocus attribute, so that the first tab will be visible on the initial page load. Each tab is wrapped in a div, and each div is given a
tabindex value, which allows the
<div> to be focusable.
As for the CSS, each button is set to
display: inline-block, allowing for the tabs to appear side by side. We apply the
:focuspsuedo-class to the button and use the adjacent sibling selector to show the related paragraph element when the button is focused. I’ve also added focus styles to the tab’s container
div, which means that the tab will keep its focused state until the user clicks outside the entire widget.
Caveats for the Tabs Widget
Because this widget depends on the
:focus psuedo-class, the tabbed content will be visible only when the tabs have a focused state. This means that the user will always have to click on a tab to see content. The
autofocus attribute does allow one of the tabs to be visible on initial page load, but as soon as the user interacts with the page, the focus will be lost.
Browser Support for the Tabs Widget
I’ve tested this in IE8, and everything works as expected, except for the
autofocus attribute, being that it’s an HTML5 feature. Chrome, Firefox, and Opera also work as expected, but the demo does not work in Safari unless the tab key is being used to apply focus. In the following demo the
:focus pseudo-class doesn’t work in any of the versions of Safari that I’ve tested, which I suspect is due to a WebKit bug. I haven’t found a way around this issue, but as long as the WebKit bug is addressed, this technique should work just fine when the user clicks on the tabs.
Building an Image Slider
If you need a simple static site with an image slider, a CSS only approach is a great way to keep your website fast an light. Let’s look at a quick example.
HTML and CSS for the Slider
We start with an outer container using a
section element with a class of
slideshow. Inside is a slideshow container where the images are placed. This slideshow also works with content other than images. To highlight that, we have a
div with a class of
text-container that is used to hold our non-image content.
The outermost container of the demo has a fixed width, and its
overflow property is set to “hidden”. The container inside of this, which holds our slide content, has a much larger width, so that the images and text content can be placed next to each other without wrapping to a new line. It’s important to note that this container has to be exactly as wide as our slide content, or else the slides won’t transition properly.
Using CSS Animation
slide class is where we apply the animation. The animation uses the
translateX property to drag the long row of content across the outer container, which forms a mask, simulating the transition of slides.
The animation timing is one of the most important factors here. To get an even number of seconds between slide transitions, we need to multiply our desired transition time by the number of slides in our slider. Here we have four slides, and our desired transition is six seconds per slide – so our animation time needs to be 24 seconds long before it repeats. The infinite repetition is controlled by the “infinite” value on the
Setting up the Keyframes for this animation can be tedious. The goal is to get the animation to look like it pauses for each image, even though it never really does. To do this we need to define at least two empty keyframes per slide. This creates the pause effect, because the keyframes define key intervals in which the
translateX property does not update. You can see this in the demo on line 66 of the CSS.
As an added bonus, hovering over the slider will pause the animation. This is done by setting the
animation-play-state property to “paused” when targeting the
The Image Slider Demo
Browser Support for the Image Slider
Works as expected in all browsers including IE10 and up!
Creating Icons with File Type Indicators
HTML and CSS for the File Type Icons
In this demo, I’m using data attributes to display the type of file that the user will be redirected to when they click the link.
content property takes as a value an
attr() function that allows us to get the value of our HTML data attribute. Using the
::after pseudo-element, we create a small black bar that holds the tet in the content property, and we position it to display on the left side of our icon.
attr() function represents an exciting part of the specification, but currently does not have any browser support for use outside of the
File Type Icons Demo
Browser Support for the File Type Icons
I was surprised to find that this demo worked in every browser I tested it in, except for IE8 of course!
The Principles of Beautiful Web Design, 4th Edition
Learn CSS in One Day and Learn It Well
Learn PHP in One Day and Learn It Well