Skip to main content Skip to docs navigation

Learn how to style web forms using CSS elements to ensure accessibility, usability and readability for different devices and screens.

On this page

Styling web forms

In the previous few articles, we showed how to create web forms in HTML. Now, we'll show how to style them in CSS .

Prerequisites: A basic understanding of HTML and CSS .
Objective: To understand the issues behind styling forms, and learn some of the basic styling techniques that will be useful to you.

Challenges in styling form widgets

History

In 1995, the HTML 2 specification introduced form controls (a.k.a. "form widgets", or "form elements"). But CSS wasn't released until late 1996, and wasn't supported by most browsers until years afterward; so, in the interim, browsers relied on the underlying operating system to render form widgets.

Even with CSS available, browser vendors were reluctant at first to make form elements stylable, because users were so accustomed to the looks of their respective browsers. But things have changed, and forms widgets are now mostly stylable, with a few exceptions.

Types of widgets

Easy-to-style

  1. <form >
  2. <fieldset > and <legend >
  3. Single-line text <input > s (e.g. type text, url, email), except for <input type="search"> .
  4. Multi-line <textarea >
  5. Buttons (both <input > and <button > )
  6. <label >
  7. <output >

Harder-to-style

The article Advanced form styling shows how to style these.

Having internals can't be styled in CSS alone

For example, the date picker calendar, and the button on <select >that displays an options list when clicked, can't be styled using CSS alone.

The articles Advanced form styling and How to build custom form controls describe how to style these.

Note: some proprietary CSS pseudo-elements, such as ::-moz-range-track , are capable of styling such internal components, but these aren't consistent across browsers, so aren't very reliable. We will mention these later.

Styling simple form widgets

The "easy-to-style" widgets in the previous section may be styled using techniques from the articles Your first form and CSS building blocks . There are also special selectors — UI pseudo-classes — that enable styling based on the current state of the UI.

We'll walk through an example at the end of this article — but first, here are some special aspects of form styling that are worth knowing about.

Fonts and text

CSS font and text features can be used easily with any widget (and yes, you can use @font-face with form widgets). However, browser behavior is often inconsistent. By default, some widgets do not inherit font-family and font-size from their parents. Many browsers use the system's default appearance instead. To make your forms' appearance consistent with the rest of your content, you can add the following rules to your stylesheet:

css
                                        
                                            button,
input,
select,
textarea
                                            {
                                            font-family
                                            :
                                            inherit;
                                            font-size
                                            :
                                            100%;
                                            }
                                        
                                    

The inherit property value causes the property value to match the computed value of the property of its parent element; inheriting the value of the parent.

The screenshots below show the difference. On the left is the default rendering of an <input type="text"> , <input type="date"> , <select > , <textarea > , <input type="submit"> , and a <button > in Chrome on macOS, with the platform's default font style in use. On the right are the same elements, with our above style rule applied.

Form controls with default and inherited font families. By default, some types are serif and others are sans serif. Inheriting should change the fonts of all to the parent's font family - in this case a paragraph. Oddly, input of type submit does not inherit from the parent paragraph.

The defaults differed in a number of ways. Inheriting should change their fonts to that of the parent's font family — in this case, the default serif font of the parent container. They all do, with a strange exception — <input type="submit"> does not inherit from the parent paragraph in Chrome. Rather, it uses the font-family: system-ui . This is another reason to use <button > elements over their equivalent input types!

There's a lot of debate as to whether forms look better using the system default styles, or customized styles designed to match your content. This decision is yours to make, as the designer of your site, or web application.

Box sizing

All text fields have complete support for every property related to the CSS box model, such as width , height , padding , margin , and border . As before, however, browsers rely on the system default styles when displaying these widgets. It's up to you to define how you wish to blend them into your content. If you want to keep the native look and feel of the widgets, you'll face a little difficulty if you want to give them a consistent size.

This is because each widget has its own rules for border, padding, and margin. To give the same size to several different widgets, you can use the box-sizing property along with some consistent values for other properties:

css
                                        
                                            input,
textarea,
select,
button
                                            {
                                            width
                                            :
                                            150px;
                                            padding
                                            :
                                            0;
                                            margin
                                            :
                                            0;
                                            box-sizing
                                            :
                                            border-box;
                                            }
                                        
                                    

In the screenshot below, the left column shows the default rendering of an <input type="radio"> , <input type="checkbox"> , <input type="range"> , <input type="text"> , <input type="date"> , <select > , <textarea > , <input type="submit"> , and <button > . The right column on the other hand shows the same elements with our above rule applied to them. Notice how this lets us ensure that all of the elements occupy the same amount of space, despite the platform's default rules for each kind of widget.

box model properties effect most input types.

What may not be apparent via the screenshot is that the radio and checkbox controls still look the same, but they are centered in the 150px of horizontal space provided by the width property. Other browsers may not center the widgets, but they do adhere to the space allotted.

Legend placement

The <legend > element is okay to style, but it can be a bit tricky to control the placement of it. By default, it is always positioned over the top border of its <fieldset > parent, near the top left corner. To position it somewhere else, for example inside the fieldset somewhere, or near the bottom left corner, you need to rely on the positioning.

Take the following example:

To position the legend in this manner, we used the following CSS (other declarations removed for brevity):

css
                                        
                                            fieldset
                                            {
                                            position
                                            :
                                            relative;
                                            }
                                            legend
                                            {
                                            position
                                            :
                                            absolute;
                                            bottom
                                            :
                                            0;
                                            right
                                            :
                                            0;
                                            }
                                        
                                    

The <fieldset > needs to be positioned too, so that the <legend > is positioned relative to it (otherwise the <legend > would be positioned relative to the <body > ).

The <legend > element is very important for accessibility — it will be spoken by assistive technologies as part of the label of each form element inside the fieldset — but using a technique like the one above is fine. The legend contents will still be spoken in the same way; it is just the visual position that has changed.

Note: You could also use the transform property to help you with positioning your <legend > . However, when you position it with for example a transform: translateY(); , it moves but leaves an ugly gap in the <fieldset > border, which is not easy to get rid of.

A specific styling example

Let's look at a concrete example of how to style an HTML form. We will build a fancy-looking "postcard" contact form; see here for the finished version .

If you want to follow along with this example, make a local copy of our postcard-start.html file , and follow the below instructions.

The HTML

The HTML is only slightly more involved than the example we used in the first article of this guide ; it just has a few extra IDs and a heading.

html
                                        
                                            
                                                
                                                    <
                                                    form
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    h1
                                                
                                                >
                                            
                                            to: Mozilla
                                            
                                                
                                                    </
                                                    h1
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    from"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                >
                                            
                                            from:
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    name"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    user_name"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    reply"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    mail"
                                                
                                                >
                                            
                                            reply:
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    email"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    mail"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    user_email"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    message"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    msg"
                                                
                                                >
                                            
                                            Your message:
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    textarea
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    msg"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    user_message"
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    textarea
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                class
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    button
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    submit"
                                                
                                                >
                                            
                                            Send your message
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    form
                                                
                                                >
                                            
                                        
                                    

Add the above code into the body of your HTML.

Organizing your assets

This is where the fun begins! Before we start coding, we need three additional assets:

  1. The postcard background — download this image and save it in the same directory as your working HTML file.
  2. A typewriter font: The "Mom's Typewriter" font from dafont.com — download the TTF file into the same directory as above.
  3. A hand-drawn font: The "Journal" font from dafont.com — download the TTF file into the same directory as above.

Your fonts need some more processing before you start:

  1. Go to the fontsquirrel.com Webfont Generator .
  2. Using the form, upload both your font files and generate a webfont kit. Download the kit to your computer.
  3. Unzip the provided zip file.
  4. Inside the unzipped contents you will find some font files (at the time of writing, two .woff files and two .woff2 files; they might vary in the future.) Copy these files into a directory called fonts, in the same directory as before. We are using two different files for each font to maximize browser compatibility; see our Web fonts article for a lot more information.

The CSS

Now we can dig into the CSS for the example. Add all the code blocks shown below inside the <style > element, one after another.

Overall layout

First, we prepare by defining our @font-face rules, and all the basic styles set on the <body > and <form > elements. If the fontsquirrel output was different from what we described above, you can find the correct @font-face blocks inside your downloaded webfont kit, in the stylesheet.css file (you'll need to replace the below @font-face blocks with them, and update the paths to the font files):

css
                                        
                                            
                                                @font-face
                                            
                                            {
                                            font-family
                                            :
                                            "handwriting"
                                            ;
                                            src
                                            :
                                            
                                                url
                                                (
                                                "fonts/journal-webfont.woff2"
                                                )
                                            
                                            format
                                            (
                                            "woff2"
                                            )
                                            ,
                                            
                                                url
                                                (
                                                "fonts/journal-webfont.woff"
                                                )
                                            
                                            format
                                            (
                                            "woff"
                                            )
                                            ;
                                            font-weight
                                            :
                                            normal;
                                            font-style
                                            :
                                            normal;
                                            }
                                            
                                                @font-face
                                            
                                            {
                                            font-family
                                            :
                                            "typewriter"
                                            ;
                                            src
                                            :
                                            
                                                url
                                                (
                                                "fonts/momot___-webfont.woff2"
                                                )
                                            
                                            format
                                            (
                                            "woff2"
                                            )
                                            ,
                                            
                                                url
                                                (
                                                "fonts/momot___-webfont.woff"
                                                )
                                            
                                            format
                                            (
                                            "woff"
                                            )
                                            ;
                                            font-weight
                                            :
                                            normal;
                                            font-style
                                            :
                                            normal;
                                            }
                                            body
                                            {
                                            font
                                            :
                                            1.3rem sans-serif;
                                            padding
                                            :
                                            0.5em;
                                            margin
                                            :
                                            0;
                                            background
                                            :
                                            #222;
                                            }
                                            form
                                            {
                                            position
                                            :
                                            relative;
                                            width
                                            :
                                            740px;
                                            height
                                            :
                                            498px;
                                            margin
                                            :
                                            0 auto;
                                            padding
                                            :
                                            1em;
                                            box-sizing
                                            :
                                            border-box;
                                            background
                                            :
                                            #fff 
                                            
                                                url
                                                (
                                                background.jpg)
                                            
                                            ;
                                            /* we create our grid */
                                            display
                                            :
                                            grid;
                                            grid-gap
                                            :
                                            20px;
                                            grid-template-columns
                                            :
                                            repeat
                                            (
                                            2,
                                            1fr)
                                            ;
                                            grid-template-rows
                                            :
                                            10em 1em 1em 1em;
                                            }
                                        
                                    

Notice that we've used some CSS Grid and Flexbox to lay out the form. Using this we can easily position our elements, including the title and all the form elements:

css
                                        
                                            h1
                                            {
                                            font
                                            :
                                            1em "typewriter"
                                            ,
                                            monospace;
                                            align-self
                                            :
                                            end;
                                            }
                                            #message
                                            {
                                            grid-row
                                            :
                                            1 / 5;
                                            }
                                            #from,
#reply
                                            {
                                            display
                                            :
                                            flex;
                                            }
                                        
                                    

Labels and controls

Now we can start working on the form elements themselves. First, let's ensure that the <label > s are given the right font:

css
                                        
                                            label
                                            {
                                            font
                                            :
                                            0.8em "typewriter"
                                            ,
                                            sans-serif;
                                            }
                                        
                                    

The text fields require some common rules. In other words, we remove their borders and backgrounds , and redefine their padding and margin :

css
                                        
                                            input,
textarea
                                            {
                                            font
                                            :
                                            1.4em/1.5em "handwriting"
                                            ,
                                            cursive,
                                            sans-serif;
                                            border
                                            :
                                            none;
                                            padding
                                            :
                                            0 10px;
                                            margin
                                            :
                                            0;
                                            width
                                            :
                                            80%;
                                            background
                                            :
                                            none;
                                            }
                                        
                                    

When one of these fields gains focus, we highlight them with a light grey, transparent, background (it is always important to have focus style, for usability and keyboard accessibility):

css
                                        
                                            input:focus,
textarea:focus
                                            {
                                            background
                                            :
                                            rgb
                                            (
                                            0 0 0 / 10%)
                                            ;
                                            border-radius
                                            :
                                            5px;
                                            }
                                        
                                    

Now that our text fields are complete, we need to adjust the display of the single and multiple-line text fields to match, since they won't typically look the same using the defaults.

Tweaking the textareas

<textarea > elements default to being rendered as an inline-block element. The two important things here are the resize and overflow properties. While our design is a fixed-size design, and we could use the resize property to prevent users from resizing our multi-line text field, it is best to not prevent users from resizing a textarea if they so choose. The overflow property is used to make the field render more consistently across browsers. Some browsers default to the value auto , while some default to the value scroll . In our case, it's better to be sure everyone will use auto :

css
                                        
                                            textarea
                                            {
                                            display
                                            :
                                            block;
                                            padding
                                            :
                                            10px;
                                            margin
                                            :
                                            10px 0 0 -10px;
                                            width
                                            :
                                            100%;
                                            height
                                            :
                                            90%;
                                            border-right
                                            :
                                            1px solid;
                                            /* resize  : none; */
                                            overflow
                                            :
                                            auto;
                                            }
                                        
                                    

Styling the submit button

The <button > element is really convenient to style with CSS; you can do whatever you want, even using pseudo-elements :

css
                                        
                                            button
                                            {
                                            padding
                                            :
                                            5px;
                                            font
                                            :
                                            bold 0.6em sans-serif;
                                            border
                                            :
                                            2px solid #333;
                                            border-radius
                                            :
                                            5px;
                                            background
                                            :
                                            none;
                                            cursor
                                            :
                                            pointer;
                                            transform
                                            :
                                            rotate
                                            (
                                            -1.5deg)
                                            ;
                                            }
                                            button:after
                                            {
                                            content
                                            :
                                            " >>>"
                                            ;
                                            }
                                            button:hover,
button:focus
                                            {
                                            background
                                            :
                                            #000;
                                            color
                                            :
                                            #fff;
                                            }
                                        
                                    

The final result

And voilà! Your form should now look like this:

The final look and layout of the form after applying all styling and tweaking to it as described above

Note: If your example does not work quite as you expected and you want to check it against our version, you can find it on GitHub — see it running live (also see the source code ).

Test your skills

You've reached the end of this article, but can you remember the most important information? You can find some further tests to verify that you've retained this information before you move on — see Test your skills: Styling basics .

Summary

As you can see, as long as we want to build forms with just text fields and buttons, it's easy to style them using CSS. In the next article , we will see how to handle form widgets which fall into the "bad" and "ugly" categories.

Advanced Topics

Advanced form styling

In this article, we will see what can be done with CSS to style the types of form control that are more difficult to style — the "bad" and "ugly" categories. As we saw in the previous article , text fields and buttons are perfectly easy to style; now we will dig into styling the more problematic bits.

Prerequisites: A basic understanding of HTML and CSS .
Objective: To understand what parts of forms are hard to style, and why; to learn what can be done to customize them.

To recap what we said in the previous article, we have:

The bad : Some elements are more difficult to style, requiring more complex CSS or some more specific tricks:

The ugly : Some elements can't be styled thoroughly using CSS. These include:

Let's first talk about the appearance property, which is pretty useful for making all of the above more stylable.

appearance: controlling OS-level styling

In the previous article we said that historically, the styling of web form controls was largely taken from the underlying operating system, which is part of the problem with customizing the look of these controls.

The appearance property was created as a way to control what OS- or system-level styling was applied to web form controls. By far the most helpful value, and probably the only one you'll use, is none . This stops any control you apply it to from using system-level styling, as much as possible, and lets you build up the styles yourself using CSS.

For example, let's take the following controls:

html
                                        
                                            
                                                
                                                    <
                                                    form
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    search"
                                                
                                                >
                                            
                                            search: 
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    search"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    search"
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    search"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                >
                                            
                                            text: 
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    date"
                                                
                                                >
                                            
                                            date: 
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    date"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    date"
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    datetime-local"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    radio"
                                                
                                                >
                                            
                                            radio: 
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    radio"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    radio"
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    radio"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                >
                                            
                                            checkbox: 
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    submit"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    submit"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    form
                                                
                                                >
                                            
                                        
                                    

Applying the following CSS to them removes system-level styling.

css
                                        
                                            input
                                            {
                                            appearance
                                            :
                                            none;
                                            }
                                        
                                    

The following live example shows you what they look like in your system — default on the left, and with the above CSS applied on the right (find it here also if you want to test it on other systems).

In most cases, the effect is to remove the stylized border, which makes CSS styling a bit easier, but isn't really essential. In a couple of cases — search and radio buttons/checkboxes, it becomes way more useful. We'll look at those now.

Taming search boxes

<input type="search"> is basically just a text input, so why is appearance: none; useful here? The answer is that Safari search boxes have some styling restrictions — you can't adjust their height or font-size freely, for example.

This can be fixed using our friend appearance: none; , which disables the default appearance:

css
                                        
                                            input[type="search"]
                                            {
                                            appearance
                                            :
                                            none;
                                            }
                                        
                                    

In the example below, you can see two identical styled search boxes. The right one has appearance: none; applied, and the left one doesn't. If you look at it in Safari on macOS you'll see that the left one isn't sized properly.

Interestingly, setting border/background on the search field also fixes this problem. The following styled search doesn't have appearance: none; applied, but it doesn't suffer from the same problem in Safari as the previous example.

Note: You may have noticed that in the search field, the "x" delete icon, which appears when the value of the search is not null, disappears when the input loses focus in Edge and Chrome, but stays put in Safari. To remove via CSS, you can use input[type="search"]:not(:focus, :active)::-webkit-search-cancel-button { display: none; } .

Styling checkboxes and radio buttons

Styling a checkbox or a radio button is tricky by default. The sizes of checkboxes and radio buttons are not meant to be changed with their default designs, and browsers react very differently when you try.

For example, consider this simple test case:

html
                                        
                                            
                                                
                                                    <
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    span
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    q5"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    true"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    span
                                                
                                                >
                                            
                                            True
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    span
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    q5"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    false"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    span
                                                
                                                >
                                            
                                            False
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                        
                                    
css
                                        
                                            span
                                            {
                                            display
                                            :
                                            inline-block;
                                            background
                                            :
                                            red;
                                            }
                                            input[type="checkbox"]
                                            {
                                            width
                                            :
                                            100px;
                                            height
                                            :
                                            100px;
                                            }
                                        
                                    

Different browsers handle the checkbox and span differently, often ugly ways:

Browser Rendering
Firefox 71 (macOS) Rounded corners and 1px light grey border
Firefox 57 (Windows 10) Rectangular corners with 1px medium grey border
Chrome 77 (macOS), Safari 13, Opera Rounded corner with 1px medium grey border
Chrome 63 (Windows 10) Rectangular borders with slightly greyish background instead of white.
Edge 16 (Windows 10) Rectangular borders with slightly greyish background instead of white.

Using appearance: none on radios/checkboxes

As we showed before, you can remove the default appearance of a checkbox or radio button altogether with appearance :none; . Let's take this example HTML:

html
                                        
                                            
                                                
                                                    <
                                                    form
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    fieldset
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    legend
                                                
                                                >
                                            
                                            Fruit preferences
                                            
                                                
                                                    </
                                                    legend
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    fruit"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    cherry"
                                                
                                                />
                                            
                                            I like cherry
  
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    fruit"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    banana"
                                                
                                                disabled
                                                />
                                            
                                            I can't like banana
  
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    fruit"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    strawberry"
                                                
                                                />
                                            
                                            I like strawberry
  
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    p
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    fieldset
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    form
                                                
                                                >
                                            
                                        
                                    

Now, let's style these with a custom checkbox design. Let's start by unstyling the original check boxes:

css
                                        
                                            input[type="checkbox"]
                                            {
                                            appearance
                                            :
                                            none;
                                            }
                                        
                                    

We can use the :checked and :disabled pseudo-classes to change the appearance of our custom checkbox as its state changes:

css
                                        
                                            input[type="checkbox"]
                                            {
                                            position
                                            :
                                            relative;
                                            width
                                            :
                                            1em;
                                            height
                                            :
                                            1em;
                                            border
                                            :
                                            1px solid gray;
                                            /* Adjusts the position of the checkboxes on the text baseline */
                                            vertical-align
                                            :
                                            -2px;
                                            /* Set here so that Windows' High-Contrast Mode can override */
                                            color
                                            :
                                            green;
                                            }
                                            input[type="checkbox"]::before
                                            {
                                            content
                                            :
                                            "✔"
                                            ;
                                            position
                                            :
                                            absolute;
                                            font-size
                                            :
                                            1.2em;
                                            right
                                            :
                                            -1px;
                                            top
                                            :
                                            -0.3em;
                                            visibility
                                            :
                                            hidden;
                                            }
                                            input[type="checkbox"]:checked::before
                                            {
                                            /* Use `visibility` instead of `display` to avoid recalculating layout */
                                            visibility
                                            :
                                            visible;
                                            }
                                            input[type="checkbox"]:disabled
                                            {
                                            border-color
                                            :
                                            black;
                                            background
                                            :
                                            #ddd;
                                            color
                                            :
                                            gray;
                                            }
                                        
                                    

You'll find out more about such pseudo-classes and more in the next article ; the above ones do the following:

  • :checked — the checkbox (or radio button) is in a checked state — the user has clicked/activated it.
  • :disabled — the checkbox (or radio button) is in a disabled state — it cannot be interacted with.

You can see the live result:

We've also created a couple of other examples to give you more ideas:

If you view these checkboxes in a browser that doesn't support appearance , your custom design will be lost, but they will still look like checkboxes and be usable.

What can be done about the "ugly" elements?

Now let's turn our attention to the "ugly" controls — the ones that are really hard to thoroughly style. In short, these are drop-down boxes, complex control types like color and datetime-local , and feedback—oriented controls like <progress > and <meter > .

The problem is that these elements have very different default looks across browsers, and while you can style them in some ways, some parts of their internals are literally impossible to style.

If you are prepared to live with some differences in look and feel, you can get away with some simple styling to make sizing consistent, uniform styling of things like background-colors, and usage of appearance to get rid of some system-level styling.

Take the following example, which shows a number of the "ugly" form features in action:

This example has the following CSS applied to it:

css
                                        
                                            body
                                            {
                                            font-family
                                            :
                                            "Josefin Sans"
                                            ,
                                            sans-serif;
                                            margin
                                            :
                                            20px auto;
                                            max-width
                                            :
                                            400px;
                                            }
                                            form >div
                                            {
                                            margin-bottom
                                            :
                                            20px;
                                            }
                                            select
                                            {
                                            appearance
                                            :
                                            none;
                                            width
                                            :
                                            100%;
                                            height
                                            :
                                            100%;
                                            }
                                            .select-wrapper
                                            {
                                            position
                                            :
                                            relative;
                                            }
                                            .select-wrapper::after
                                            {
                                            content
                                            :
                                            "▼"
                                            ;
                                            font-size
                                            :
                                            1rem;
                                            top
                                            :
                                            3px;
                                            right
                                            :
                                            10px;
                                            position
                                            :
                                            absolute;
                                            }
                                            button,
label,
input,
select,
progress,
meter
                                            {
                                            display
                                            :
                                            block;
                                            font-family
                                            :
                                            inherit;
                                            font-size
                                            :
                                            100%;
                                            margin
                                            :
                                            0;
                                            box-sizing
                                            :
                                            border-box;
                                            width
                                            :
                                            100%;
                                            padding
                                            :
                                            5px;
                                            height
                                            :
                                            30px;
                                            }
                                            input[type="text"],
input[type="datetime-local"],
input[type="color"],
select
                                            {
                                            box-shadow
                                            :
                                            inset 1px 1px 3px #ccc;
                                            border-radius
                                            :
                                            5px;
                                            }
                                            label
                                            {
                                            margin-bottom
                                            :
                                            5px;
                                            }
                                            button
                                            {
                                            width
                                            :
                                            60%;
                                            margin
                                            :
                                            0 auto;
                                            }
                                        
                                    

Note: If you want to test these examples across a number of browsers simultaneously, you can find it live here (also see here for the source code ).

Also bear in mind that we've added some JavaScript to the page that lists the files selected by the file picker, below the control itself. This is a simplified version of the example found on the <input type="file"> reference page.

As you can see, we've done fairly well at getting these to look uniform across modern browsers.

We've applied some global normalizing CSS to all the controls and their labels, to get them to size in the same way, adopt their parent font, etc., as mentioned in the previous article:

css
                                        
                                            button,
label,
input,
select,
progress,
meter
                                            {
                                            display
                                            :
                                            block;
                                            font-family
                                            :
                                            inherit;
                                            font-size
                                            :
                                            100%;
                                            margin
                                            :
                                            0;
                                            box-sizing
                                            :
                                            border-box;
                                            width
                                            :
                                            100%;
                                            padding
                                            :
                                            5px;
                                            height
                                            :
                                            30px;
                                            }
                                        
                                    

We also added some uniform shadow and rounded corners to the controls on which it made sense:

css
                                        
                                            input[type="text"],
input[type="datetime-local"],
input[type="color"],
select
                                            {
                                            box-shadow
                                            :
                                            inset 1px 1px 3px #ccc;
                                            border-radius
                                            :
                                            5px;
                                            }
                                        
                                    

On other controls like range types, progress bars, and meters they just add an ugly box around the control area, so it doesn't make sense.

Let's talk about some specifics of each of these types of control, highlighting difficulties along the way.

Selects and datalists

In modern browsers, selects and datalists are generally not too bad to style provided you don't want to vary the look and feel too much from the defaults.

We've managed to get the basic look of the boxes looking pretty uniform and consistent. The datalist control is <input type="text"> anyway, so we knew this wouldn't be a problem.

Two things are slightly more problematic. First of all, the select's "arrow" icon that indicates it is a dropdown differs across browsers. It also tends to change if you increase the size of the select box, or resize in an ugly fashion. To fix this in our example we first used our old friend appearance: none to get rid of the icon altogether:

css
                                        
                                            select
                                            {
                                            appearance
                                            :
                                            none;
                                            }
                                        
                                    

We then created our own icon using generated content. We put an extra wrapper around the control, because ::before / ::after don't work on <select > elements (this is because generated content is placed relative to an element's formatting box, but form inputs work more like replaced elements — their display is generated by the browser and put in place — and therefore don't have one):

html
                                        
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    select"
                                                
                                                >
                                            
                                            Select a fruit
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    div
                                                
                                                class
                                                
                                                    =
                                                    "
                                                    select-wrapper"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    select
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    select"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    select"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Banana
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Cherry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Lemon
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    select
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    div
                                                
                                                >
                                            
                                        
                                    

We then use generated content to generate a little down arrow, and put it in the right place using positioning:

css
                                        
                                            .select-wrapper
                                            {
                                            position
                                            :
                                            relative;
                                            }
                                            .select-wrapper::after
                                            {
                                            content
                                            :
                                            "▼"
                                            ;
                                            font-size
                                            :
                                            1rem;
                                            top
                                            :
                                            6px;
                                            right
                                            :
                                            10px;
                                            position
                                            :
                                            absolute;
                                            }
                                        
                                    

The second, slightly more important issue is that you don't have control over the box that appears containing the options when you click on the <select > box to open it. You can inherit the font set on the parent, but you won't be able to set things like spacing and colors. The same is true for the autocomplete list that appears with <datalist > .

If you really need full control over the option styling, you'll have to either use some kind of library to generate a custom control, or build your own custom control, or in the case of select use the multiple attribute, which makes all the options appear on the page, sidestepping this particular problem:

html
                                        
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    select"
                                                
                                                >
                                            
                                            Select fruits
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    select
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    select"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    select"
                                                
                                                multiple
                                                >
                                            
                                                
                                                    </
                                                    select
                                                
                                                >
                                            
                                        
                                    

Of course, this might also not fit in with the design you are going for, but it's worth noting!

Date input types

The date/time input types ( datetime-local , time , week , month ) all have the same major associated issue. The actual containing box is as easy to style as any text input, and what we've got in this demo looks fine.

However, the internal parts of the control (e.g. the popup calendar that you use to pick a date, the spinner that you can use to increment/decrement values) are not stylable at all, and you can't get rid of them using appearance: none; . If you really need full control over the styling, you'll have to either use some kind of library to generate a custom control, or build your own.

Note: It is worth mentioning <input type="number"> here too — this also has a spinner that you can use to increment/decrement values, so potentially suffers from the same problem. However, in the case of the number type the data being collected is simpler, and it is easy to just use a tel input type instead which has the appearance of text , but displays the numeric keypad in devices with touch keyboards.

Range input types

<input type="range"> is annoying to style. You can use something like the following to remove the default slider track completely and replace it with a custom style (a thin red track, in this case):

css
                                        
                                            input[type="range"]
                                            {
                                            appearance
                                            :
                                            none;
                                            background
                                            :
                                            red;
                                            height
                                            :
                                            2px;
                                            padding
                                            :
                                            0;
                                            outline
                                            :
                                            1px solid transparent;
                                            }
                                        
                                    

However, it is very difficult to customize the style of the range control's drag handle — to get full control over range styling you'll need to use a whole bunch of complex CSS code, including multiple non-standard, browser-specific pseudo-elements. Check out Styling Cross-Browser Compatible Range Inputs with CSS on CSS tricks for a detailed write-up of what's needed.

Color input types

Input controls of type color are not too bad. In supporting browsers, they tend to just give you a block of solid color with a small border.

You can remove the border, just leaving the block of color, using something like this:

css
                                        
                                            input[type="color"]
                                            {
                                            border
                                            :
                                            0;
                                            padding
                                            :
                                            0;
                                            }
                                        
                                    

However, a custom solution is the only way to get anything significantly different.

File input types

Inputs of type file are generally OK — as you saw in our example, it is fairly easy to create something that fits in OK with the rest of the page — the output line that is part of the control will inherit the parent font if you tell the input to do so, and you can style the custom list of file names and sizes in any way you want; we created it after all.

The only problem with file pickers is that the button provided that you press to open the file picker is completely unstylable — it can't be sized or colored, and it won't even accept a different font.

One way around this is to take advantage of the fact that if you have a label associated with a form control, clicking the label will activate the control. So you could hide the actual form input using something like this:

css
                                        
                                            input[type="file"]
                                            {
                                            height
                                            :
                                            0;
                                            padding
                                            :
                                            0;
                                            opacity
                                            :
                                            0;
                                            }
                                        
                                    

And then style the label to act like a button, which when pressed will open the file picker as expected:

css
                                        
                                            label[for="file"]
                                            {
                                            box-shadow
                                            :
                                            1px 1px 3px #ccc;
                                            background
                                            :
                                            linear-gradient
                                            (
                                            to bottom,
                                            #eee,
                                            #ccc)
                                            ;
                                            border
                                            :
                                            1px solid rgb
                                            (
                                            169,
                                            169,
                                            169)
                                            ;
                                            border-radius
                                            :
                                            5px;
                                            text-align
                                            :
                                            center;
                                            line-height
                                            :
                                            1.5;
                                            }
                                            label[for="file"]:hover
                                            {
                                            background
                                            :
                                            linear-gradient
                                            (
                                            to bottom,
                                            #fff,
                                            #ddd)
                                            ;
                                            }
                                            label[for="file"]:active
                                            {
                                            box-shadow
                                            :
                                            inset 1px 1px 3px #ccc;
                                            }
                                        
                                    

You can see the result of the above CSS styling in the below live example (see also styled-file-picker.html live, and the source code ).

Meters and progress bars

<meter > and <progress > are possibly the worst of the lot. As you saw in the earlier example, we can set them to the desired width relatively accurately. But beyond that, they are really difficult to style in any way. They don't handle height settings consistently between each other and between browsers, you can color the background, but not the foreground bar, and setting appearance: none on them makes things worse, not better.

It is easier to just create your own custom solution for these features, if you want to be able to control the styling, or use a third-party solution such as progressbar.js .

The article How to build custom form controls provides an example of how to build a custom designed select with HTML, CSS, and JavaScript.

Summary

While there are still difficulties using CSS with HTML forms, there are ways to get around many of the problems. There are no clean, universal solutions, but modern browsers offer new possibilities. For now, the best solution is to learn more about the way the different browsers support CSS when applied to HTML form controls.

In the next article of this module, we will explore the different UI pseudo-classes available to us in modern browsers for styling forms in different states.

Advanced Topics

Updated on April 20, 2024 by Datarist.