Adobe
Products

Top destinations

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • SiteCatalyst
  • Students
  • Elements family

Adobe Creative Cloud

  • What is Adobe Creative Cloud?
  • Design
  • Web
  • Photography
  • Video
  • Students
  • Teams
  • Enterprise
  • Educational institutions

Design and photography

  • Photoshop
  • Illustrator
  • InDesign
  • Adobe Muse
  • Lightroom

Video

  • Adobe Premiere
  • After Effects

Web development and HTML5

  • Edge Tools & Services [opens in a new window]
  • Dreamweaver
  • Gaming [opens in a new window]

Adobe Marketing Cloud

  • What is Adobe Marketing Cloud?
  • Digital analytics
  • Social marketing
  • Web experience management
  • Testing and targeting
  • Media optimization

Analytics

  • SiteCatalyst
  • Adobe Discover
  • Insight

Social

  • Adobe Social

Experience Manager

  • CQ
  • Scene7

Target

  • Test&Target
  • Recommendations
  • Search&Promote

Media Optimizer

  • AdLens
  • AudienceManager
  • AudienceResearch

Document services

  • Acrobat
  • EchoSign [opens in a new window]
  • FormsCentral [opens in a new window]
  • SendNow [opens in a new window]
  • Acrobat.com [opens in a new window]

Publishing

  • Digital Publishing Suite

  • See all products
Business solutions

By business need

  • Digital analytics
  • Digital publishing
  • Document management
  • Media optimization
  • Social marketing
  • Testing and targeting
  • Video editing and serving
  • Web development [opens in a new window]
  • Web experience management
  • See all business needs

By industry

  • Broadcast
  • Education
  • Financial services
  • Government
  • Publishing
  • Retail
  • See all industries
Support & Learning

I need help

  • Products
  • Adobe Creative Cloud
  • Adobe Marketing Cloud
  • Forums [opens in a new window]

I want to learn

  • Training and tutorials
  • Certification [opens in a new window]
  • Adobe Developer Connection
  • Adobe Design Center
  • Adobe TV [opens in a new window]
  • Adobe Marketing Center
  • Adobe Labs [opens in a new window]
Download
  • Product trials
  • Adobe Flash Player
  • Adobe Reader
  • Adobe AIR
  • See all downloads
Company
  • Careers at Adobe
  • Investor Relations
  • Newsroom
  • Privacy
  • Corporate Social Responsibility
  • Customer Showcase
  • Contact us
  • More company info
Buy
  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers
  • Adobe Marketing Cloud sales [opens in a new window]
Search
 
Info Sign in
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Welcome,
My Adobe
My orders
My information
My preferences
My products and services
Sign out
My cart
Privacy My Adobe
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out Privacy My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Promotions
Estimated shipping
Tax
Calculated at checkout
Total
Review and Checkout
Adobe Developer Connection / HTML5, CSS3, and JavaScript /

Using Form Elements and CSS3 to Replace JavaScript

by Louis Lazaris

Louis Lazaris
  • www.impressivewebs.com

Content

  • Why Use These Techniques?
  • Initial Warning!
  • Tab switcher with radio buttons
  • Accordion slider with checkboxes
  • Revealing a text field with focus
  • Bugs, problems, and warnings
  • Where to go from here

Created

7 January 2013

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
CSS3HTML5JavaScript
Was this helpful?
Yes   No

By clicking Submit, you accept the Adobe Terms of Use.

 
Thanks for your feedback.

Requirements

Prerequisite knowledge

HTML, CSS and some knowledge of new CSS3 features.

User level

All

Since the advent of CSS3 and the continuing improvement in browser support for new features, more developers are coming up with unique ways to add functionality to web pages without the use of JavaScript.

In the last couple of years, a number of developer techniques have sprung up using form elements to replace functionality normally done with JavaScript and, commonly, with a library like jQuery.

In this article, I run through some of these techniques and discuss the concepts involved. This should help you get a good overall understanding of how these tricks work, and may give you insight into creating something with pure CSS for one of your future web projects.

Why Use These Techniques?

There are a few reasons why these techniques might be beneficial to your code. First of all, any extra JavaScript makes our websites slower because you'd be adding more code and in some cases potentially an extra external resource for the page to load. Additionally, a CSS-only solution is much cleaner and easier to maintain. With good commenting, it should be fairly easy to track down how certain functionality is operating, should you ever need to make any necessary corrections or improvements.

Initial Warning!

The fact is, the techniques mentioned in this article may not be good options because of the semantic problems and accessibility challenges that arise when using form elements in a way not intended.

So although this article has definite experimental and educational benefits for those learning CSS, take into consideration any drawbacks that might occur before using these techniques extensively in any project. Of course, if you're able to ensure you can use these without accessibility problems, then by all means do so. But please be forewarned that they could be problematic.

Tab switcher with radio buttons

Many websites feature a "tab switcher". In brief, this is a single unit, or widget, sometimes appearing in the sidebar of a website, that has two or more content areas that the user can choose from. Only one of these content areas is visible at one time, and the options are displayed in the form of tabs at the top of the widget.
Most older tutorials and demos use list items, div elements, and jQuery to create this functionality. But in the past couple of years, a number of scripts and tutorials have begun to use radio buttons and some new CSS3 pseudo-classes to assist with the functionality of the tabs.

Let's run through how this might be done. Our markup can be divided into multiple sections. Each section might look something like the following markup:

<section class="tab"> <input type="radio" id="tab-one" name="tab-set" checked> <label for="tab-one">Tab #1</label> <div class="tab-content"> <p>First tab content goes here.</p> </div><!-- tab-content --> </section><!-- .tab (1) -->

This might be repeated three or four times. Then, we could have a wrapper with a class of tab-switcher holding all these sections together. The key parts of the markup are the input elements with type radio and their associated label elements.

Here is the example in JS Bin. Select CSS to see that code.

JS Bin

Throughout this article, vendor CSS3 prefixes are omitted for brevity. If you want to see this code in action, you can check out this JS Bin demo. Now let's consider the concepts involved in creating this tab switcher. Notice the demo is not using any JavaScript.

Hidden Radio Buttons

Although this example is using radio buttons, we don't actually see any radio buttons on the page. This is because the radio buttons are set to display: none in our CSS. But if the radio buttons aren't visible on the page, how do they function?

Well, notice the markup also includes a label element that's associated with each radio button using IDs and for attributes. Although the radio buttons are not visible or clickable, the associated label elements are. And if you are familiar with label elements, you know that browsers allow the labels to be clicked to select their associated form elements.

Thus, all we have to do is style our labels to look like tabs (I used rounded corners and a few other styles) and then target them using the :checked pseudo-class along with the general sibling selector (the tilde character, ~).

The general sibling selector lets us target both the content section of the selected tab, as well as the label element. Using :checked in combination with the sibling selector lets us target only the parts of the tab switcher that are currently selected, or active.

With some absolute positioning along with z-index, we then can make the chosen section visible. In the demo, I also added some CSS3 transitions to prevent the switching from occurring too quickly.

Concepts to Remember

To summarize this technique, here are the concepts to keep in mind:

  • The radio buttons are not visible on the page.
  • Any label element can be associated with any form element, allowing the form element to be selected via the label without the form element being visible.
  • The :checked pseudo-class in combination with the general sibling selector lets us target elements associated with the selected form element.

Accordion slider with checkboxes

Once again, we create a commonly-used web page widget while avoiding any JavaScript. This time, we create a vertical accordion slider.

For our markup, similar to the tab switcher, we divide each accordion section using HTML5 section elements. We have a wrapper with a class of accordion-wrap enclosing all five sections. Each section looks something like the following markup:

<section class="accordion-item"> <input type="checkbox" id="accordion-one" name="accordion-group" checked> <label for="accordion-one">Accordion Section #1</label> <div class="accordion-content"> <p>content here... </p> </div><!-- .accordion-content --> </section><!-- .accordion-item -->

The actual example has five of these, but I'm showing you just one for brevity. Again, the key parts of the code are the checkbox input element and its associated label element.

Our CSS is similar to the previous example, and once again we utilize the :checked pseudo-class along with the general sibling selector. Here is the example in JS Bin. Select CSS to see that code.

JS Bin

Using max-height

This example uses checkboxes, which, unlike radio buttons, allow multiple choices to be selected. This means the different items in the accordion can all appear expanded at the same time.

The tabs example used a combination of opacity and z-index to show the selected item. But this time, we're using the max-height property. Any unchecked item starts out with a max-height value of 0, along with overflow: hidden, so it's not visible. When an item is checked, we set the max-height to any large number that exceeds the height value of the largest item.

We use max-height and not the height property, because although height can be animated via CSS3, you cannot animate from 0 to auto. So we have to choose an arbitrary max-height value, to ensure the height of the element doesn't extend far past the actual content it holds. (Credit to Lea Verou for this technique.)

Concepts to Remember

Here are the main points to remember in this example:

  • Checkboxes allow multiple elements to be selected, thus mimicking what we might do with JavaScript.
  • Again, we use labels to trigger the selection, but keep the checkboxes hidden.
  • We show the content of each accordion item by animating from max-height: 0 to a large max-height value that exceeds the tallest item.
  • We use a different transition-duration value for the checked state compared to the unchecked state, which is one of the flexibilities of CSS3 transitions.

Revealing a text field with focus

This final technique is simply a form button that, when clicked, animates to reveal a text input element. Let's look at the markup first, which is quite simple:

<input type="text" class="txt" id="textfield"> <label for="textfield">Click to Type</label>

In this example, we're using nothing but a single text field with an associated label element, as in the following JS Bin example. Select CSS to see the related code.

JS Bin

Using :focus

In this example, we're using the :focus pseudo-class to make the form field visible. The label element is styled to look like a button. It's positioned to appear on top of the text field, so the text field is not immediately visible.

We then use our super-handy sibling selector to animate the left property on the label element. This reveals the text field by moving the label element out of the way.
Take a look at this code in action at this JS Bin demo.

Concepts to Remember

To summarize what we did in this last example:

  • The :focus pseudo-class is used to detect if the user has clicked the label element, thus adding focus to the text field.
  • The label and the form element are positioned absolutely and stacked using z-index.
  • A CSS transition animates the left property to move the label out of the way when the user is ready to type.
  • The general sibling selector is once again used to target an element adjacent to the selected form field.

Bugs, problems, and warnings

As already mentioned, these techniques should be considered carefully before being used. First of all, older versions of WebKit had a bug that didn't allow the :checked pseudo-class to work in conjunction with a sibling selector. Fortunately, not many people are using older versions of WebKit so this is less of a problem today.

The other problem occurs in WebKit on iOS5 or below, which doesn't allow a checkbox's state to change by touching a label element. A workaround for this is to add an empty inline onclick handler to all label elements, as I did in the demos for this post. Additionally, iOS has slow reaction to label clicks that trigger input state changes.

A more significant problem, however, is the fact that the markup for these elements could cause problems with assistive technology devices like screen readers. This accessibility issue should be taken into consideration before implementing a method like these.

There has been discussion about the possibility of adding this type of behavior natively to elements with pure CSS, to prevent these bugs and accessibility problems, but that would be much further down the road and would not be implemented in all browsers for a very long time.

Where to go from here

To deal with some of the cross-browser problems, you can attempt to use a selector polyfill like Selectivizr to fill in any gaps, but you might end up with buggy or unpredictable results, and, unless you provide an animation polyfill, transitions are missing.

As mentioned in the introduction, if you take care to avoid any accessibility problems, these techniques can be useful. By avoiding JavaScript, your pages load faster, the functionality is  less likely to have any lag, and with a few simple comments in your HTML or CSS your code is easier to maintain.

Much of the credit and inspiration for this post is due to work done by a number of developers. Be sure to check out the articles and demos shown below if you want to delve more into this topic.

  • 101 ways to (ab)use a checkbox (slides) by Ryan Seddon
  • Tabbed Panels by Thierry Koblentz
  • Functional CSS Tabs Revisited by Chris Coyier
  • CSS Only Tabs jsFiddle by Jacob Dubail
  • Here's a Neat Trick Using the :Focus Pseudo-Class by Joshua Hibbert
  • Responsive CSS Timeline with 3D Effect by Manoela Ilic
  • Build a Freaking Awesome Pure CSS Accordion by Josh Johnson

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Using CSS3 transitions: A comprehensive guide
  • The pursuit of simplicity
  • An Overview of Brackets' Code Architecture
  • Backbone.js Wine Cellar tutorial – Part 1: Getting started
  • JavaScript design patterns – Part 1: Singleton, composite, and façade
  • Getting started with PhoneGap in Eclipse for Android
  • Using the Geolocation API
  • Unit test JavaScript applications with Jasmine
  • Backbone.js Wine Cellar tutorial – Part 2: CRUD
  • Build a Hangman game with HTML5 Canvas, JavaScript, and CSS – Part 1: Creating the interface

Products

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • Digital Publishing Suite
  • Elements family
  • SiteCatalyst
  • For education

Download

  • Product trials
  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR

Support & Learning

  • Product help
  • Forums

Buy

  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2013 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

Ad Choices

Reviewed by TRUSTe: site privacy statement