Way to Align Checkboxes and Their Labels Consistently (cross-browsers)

So, the problem is how to align checkbox and its label and looking the same in all browsers. You align them in one browser and then check with another they can be completely messed up.

Let’s have the standard code first:


<form>
    <div>
        <label><input type="checkbox" /> Label text</label>
    </div>
</form>

 

And is one solution that I find working perfect for my need:


label {
    display: block;
    padding-left: 15px;
    text-indent: -15px;
}
input {
    width: 13px;
    height: 13px;
    padding: 0;
    margin:0;
    vertical-align: bottom;
    position: relative;
    top: -1px;
    *overflow: hidden;
}

 

This code assumes that you’re using a reset like Eric Meyer’s that doesn’t override form input margins and padding (hence putting margin and padding resets in the input CSS).

Creating Fancy design forms using CSS

Designing a great looking website tailored to meet the present day admirable styles will add wonders to the website. Often you will find form elements that are derived from native operating system widgets that make it particularly difficult to style. The forms also get critical to the functioning of the website – as they are most often employed as search boxes, inquiry forms and shopping cart checkouts – and need to function as smoothly as possible in order to meet user expectations.

Forms are the most attractive part of the website where you must invest most of your time and energy to ensure perfect level of user accessibility and user friendliness. Forms are capable of representing one of the most complex interactions that can occur on a web page and most of the time these forms are represented visually – through the proximity of a form element to its label, or grouping by borders and also background color sets. User accessibility becomes highly essential as screen users might often fail to see these visual clues, unless user accessibility is perfect. The primary element behind providing an accessible form is to have descriptive labeling of all the sections and input elements. In other words, it means proper utilization of two important elements – label and legend.

Labeling form elements

All form elements and their labels conform to a general pattern:

  1. The form element itself
  2. A text label for the element
  3. A proper connection between the element and its textual description

This connection is made through visual grouping, visual alignment or some other visual indicator.

The main question that arises is how can a user, unable to see a web page, succeed in creating a connection between a form element and its text label, without the visual cues of grouping and proximity. The answer is simple – label element! Label is a special type of element that is applied to the form element to allow its textual description to be semantically linked to the element itself, so that any screen reader can read out the text while encountering its partner form elements.

To use a label, you need to wrap the textual description in a pair of label tags, add an attribute to the label. However, the value for attribute must be the id of the form element with which you want to create a connection.

    <label for="firstName">First name</label>
    <input id="firstName" name="firstName" type="text" />

When a screen reader encounters the firstName field, it will also read the text “Firstname” to the user, so he or she will know exactly what to type into that field.

However, a label should be applied to any form elements that does not automatically include descriptive texts like:

  • Checkboxes
  • Radio buttons
  • Text fields
  • Select boxes
  • Textareas

Legend – Grouping related elements

Legend is compatible with fieldset. A fieldset is a group of related form elements. For example, state, country, street address, zip code, and suburb can be grouped under “postal address”.

<form action="example.php">
    <fieldset>
        <legend>Postal Address</ legend>
            <label for="street">Street address</label>
            <input id= "street" name= "street" type= "text" />
            <label for = "suburb">Suburb</ label>
            <input id= "suburb" name= "suburb" type= "text" />
    </fieldset>
</form>

So with fieldset’s legend elements in place, it becomes quite easy to determine visually which field should fall under which group, even on an unstyled form.

So a proper combination of label and legend, gives visually impaired users, the perfect ability to navigate and fill in forms easily. Using this combination as the basic structure for forms, you will ensure that not only they look fantastic; they will be accessible as well.

Using CSS for Creating Two-column Layout

A close research on web design patterns show that accessibility is an aspect that is most underutilized in a good web page layout. So making accessible forms in web page is the best way to treat this problem. And when you create accessible forms, use CSS layout to create two-column layout forms without using tables, to save space and time. Although majority use block-level floats and margins to position form elements on the page, but the best accessible form can be created using IE5.X Mac as well as Firefox 1.07 Win/Mac, Opera, Camino and Safari 1.07 Mac.

How to build a CSS-based Form?

One of the best ways to build CSS-based form is to decode XHTML to bare structural elements, and then use CSS to create as much style and layout as possible using few embedded classes or IDs. Semantic elements provided by W3C in (X)HTML specifications, namely FIELDSET and LABEL, help in imparting logical order to FORMs. While FIELDSET groups’ like-minded form input fields, LABEL for links field labels with input fields through the ID attribute to make them highly accessible and of style.

To understand the difference created between table-based contact forms and CSS-based forms.

Table-based Contact Forms

Since tables use columns and rows to align labels and input fields, the codes would format the following layout:

Table based layouts lowers the content to markup ratio and reduces SEO speed and rankings. So there is a fortunate way – CSS Layout.

<form action="http://www.example.com/submit.cgi" method="post" name="f">
    <fieldset>
        <legend>Personal Information</legend>>
            <label for="firstname" accesskey="f">First name: </label>
                <input type="text" id="firstname" name="firstname" value="" tabindex="1">
            <label for="lastname" accesskey="l">Last name: </label>
                <input type="text" id="lastname" name="lastname" tabindex="2">
            <label for="email" accesskey="e">Email: </label>
                <input type="text" id="email" name="email" tabindex="3">
    </fieldset>
    <fieldset>
        <legend>Comments</legend>
            <label for="comments" accesskey="c">Your comments: </label>
                <textarea id="comments" rows="3" cols="20" name="comments"></textarea>
                <input type="submit" value="Submit" tabindex="4"> <input type="Reset" tabindex="5">
    </fieldset>
</form>

Set Form Width

Now let us set a width for the forms and style of the field sets. Since field sets are not rendered identical by different browsers, so backgrounds can overlap, and tend to take up the entire space of the screen on Internet Explorer. It is feasible to set the width using the form element:

form { /* set width in form, not fieldset (still takes up more room w/fieldset width */
    font:100% verdana,arial,sans-serif;
    margin: 0;
    padding: 0;
    min-width: 500px;
    max-width: 600px;
    width: 560px;
}

form fieldset { / * clear: both; note that this clear causes inputs to break to left in ie5.x mac, commented out */
    border-color: #000;
    border-width: 1px;
    border-style: solid;
    padding: 10px;        /* padding in fieldset support spotty in IE */
    margin: 0;
}

Complement layout with CSS Float

The main key towards successful lying of two-column form is to float the label element.

For example,

form label {
    display: block;  /* block float the labels to left column, set a width */
    float: left;
    width: 150px;
    padding: 0;
    margin: 5px 0 0; /* set top margin same as form input - textarea etc. elements */
    text-align: right;
}

This code above will create a two-column layout using input elements and label.

Jan 8, 2008 Linkage – Accessible Forms

Here are some tutorials and articles that can help you to build your forms to be more accessible. You can get the basic ideas out of the the links below.

Form Layouts

Laying out a form can be highly interesting. Specially with CSS formula applied to it, these forms can create a difference to the design of the website. For each of the form element in a left-to-right reading system, it becomes logical to position the corresponding label in one of the three ways:

  • In a separate left column, left aligned
  • In a separate left column, right-aligned
  • Directly above the form element

Labels that are positioned directly above a form element have shown to be processed most swiftly by the users. The compact grouping between label and element decreases eye movement thereby allowing the user to observe both of it simultaneously. Labels that are positioned in a column to the left of the elements look far more organized and neat. However, the way in which the texts in those labels are aligned also affects the usability of the form.

Following screenshot will show how it looks when labels are positioned directly above a form element:

clip_image002.gif

A far stronger grouping can be possible between label and element by right-aligning the text. The ragged left edge of the labels can make the form look disturbed and decrease the ability of users to scan the labels by themselves. But in a left-aligned column, the labels become instantly easier to scan, but the grouping with associated form elements become weaker. But users have to spend some more time correlating the labels with elements, resulting in slower form completion method.

clip_image003.gif

The right aligned column layout allows swifter association between label and element, so it becomes highly appropriate for forms that will be visited repeatedly by the users. However, both the layouts have the benefits of occupying minimum amount of vertical space.

clip_image001.gif

Applying CSS to Forms

Forms are an integral part of Internet but they can look uninteresting without the proper usage of CSS. With CSS, you can use position the forms perfectly as well as add a splash of color to jazz them up.

To make a form perfectly, firstly, we need to line it up nicely. Here are some of CSS codes to make the forms look perfect.

label {
    width: 4em;
    float: left;
    text-align: right;
    margin-right: 0.5em
    display: block
}

.submit input {
    margin-left: 4.5em;
}

Here we have used a label with fixed width of 4em and not px, because it will help the users to enhance the width with larger letters. A 0.5em margin on the right side CSS command means the labels will have a small amount of spacing after them, so that the text is not up against the input box.

The Submit button also has a left margin of 4.5em so that it gets align with the input boxes, which are 5em from the left side.

Formatting a whole form

To give a set of own title and border, we add <fieldset> and <legend> commands to HTML code:

<form action="#" method="post"$gt;
    <fieldset$gt;
        <legend$gt;This is my form</legend$gt;
        <p$gt;<label for= "name"$gt;Name</label$gt; <input type= "text" id="name" /$gt;</p$gt;
        <p$gt;<label for= "e-mail"$gt;Email</label$gt; <input type= "text" id= "e-mail" /$gt;<br /$gt;</p$gt;
        <p class="submit"$gt;<input type="submit" value="Submit" /$gt;</p$gt;
    </ fieldset$gt;
</ form$gt;

In order to impart a blue border and title with orange background, you need to apply some more CSS commands to the fieldset and legend:

fieldset {
    border: 1px solid #781351;
    width: 20em
}

legend {
    color: #fff;
    background: #ffa20c;
    border: 1px solid #781351
    padding: 2px 6px
}

Applying colors to forms

Now we will use a set of three CSS commands to make the forms look good along with a host of borders, backgrounds and colors.

Your requirements are as follows:

  1. Input boxes must have a dark blue text color and border and pale orange colored background.
  2. Submit button should have black text, an orange background and a dark blue border.

To get this set-up, you need to have the following CSS commands:

input {
    color: #781351;
    background: #fee3ad;
    border: 1px solid #781351
}

.submit input {
    color: #000;
    background: #ffa20f;
    border: 2px outset #d7b9c9
}

We use ‘outset’ for button’s border so that it looks like a button. If we had used ‘solid’, it would look more flat.

  Subscribe to our RSS Feed   Follow us on Twitter

You've arrived on the "Advanced CSS/HTML Explained" weblog. Here you will find how can you tackle the more difficult topics of Cascading Style Sheets: Floating theory, Forms, Tables, Navigation elements, CSS Positioning. Also you will find lots of Resources, Tutorials, Articles, and Tips, Tricks Tools, and Techniques on CSS.

Recent Posts

Categories

Twitter Updates

Bookmark and Share