Html form select button

Select Form Buttons in CSS – Techniques with Examples

This article covers two ways, the easy and the advanced way to select a button or buttons in a form with CSS. We give example HTML and CSS for each technique to select a form button or form buttons. The advanced way uses a CSS attribute selector.

The Easy Way to Select a Form Button in CSS

The simplest way to select a form button is by id. Consider the following HTML, a form with a “Date” text field, and button labelled “Check”:

The easy way to select the “Check” form button in CSS is using the “#id” CSS selector syntax. The button is selected in CSS using the unique id “ check-button ” we gave it in the HTML:

Advanced: Using a CSS Attribute Selector to Select Form Buttons in CSS

While using an id on the button is easy, using lots of ids can make HTML markup for more complex pages with many elements dense and harder to read.

Читайте также:  Php image to str

A cleaner, more elegant way to specify the button is to use a CSS attribute selector:

The attribute selector here is [type=»button»] . This selector as a whole says “for all forms, select the input elements whose type attribute is button“. In other words, select all the form buttons in the page.

Using a CSS attribute selector, the id on the form button isn’t needed, so id=»check-button» can be dropped from HTML markup above. Here’s the simpler HTML:

Submit buttons will not be selected by this method. If you want to selected a submit button, see our coming article.

Select a Particular Form Button with CSS Attribute Selectors

To select a particular buttons, the button can be specified by its label text. Here we have a form with two buttons, “Check” and “Download”:

To just select the button labelled “Check”, use a selector with [value=»Check»] :

form#check-form input[type=»button»][value=»Check»]

Submit buttons will not be selected by this method. If you want to selected a submit button, see our coming article.

Select All Form Buttons in a Particular Form with a CSS Attribute Selector

To select the buttons in a particular form, one way is to put an id on the form:

After adding an id to the form, add the form’s id to the CSS selector using ‘#’, like this:

form#check-form input[type=»button»]

This selector specifies all the buttons in the “check-form” form. Submit buttons will not be selected. If you want to selected a submit button, see our next article.

Which Browsers Support CSS Attribute Selectors?

Attribute selectors are supported by standards based browsers.

I’ve tested these techniques in Firefox 4 (FF4, 4.0, 4.0.1), Chrome 11 (11.0), Safari 5 (5.0.5) and Opera 11 (11.10).

With Opera there’s a caveat: one attribute selector worked input[value=»Check»] , but two input[type=»button»][value=»Check»] did not. I didn’t investigate Opera further.

Internet Explorer 6 (IE6) does not support attribute selectors.

Attribute selectors are supported by Internet Explorer 7 (IE7) and Internet Explorer 8 (IE8) only if the !DOCTYPE has been specified at the top of the webpage. Attribute selectors should work fine in IE9; I haven’t tested if a DOCTYPE is required.

I have tested these techniques on Safari on the iPhone, iPad and iPod running iOS 4.3 (4.3.2). They work great!

References

You can read more about CSS attribute selectors at W3C or w3schools.

Questions?

Did you want to select form buttons in a way that wasn’t covered here? If so, write a comment below, and I’ll do my best to answer your query.

Источник

HTML Form Elements

This chapter describes all the different HTML form elements.

The HTML Elements

The HTML element can contain one or more of the following form elements:

The Element

One of the most used form element is the element.

The element can be displayed in several ways, depending on the type attribute.

Example

All the different values of the type attribute are covered in the next chapter: HTML Input Types.

The Element

The element defines a label for several form elements.

The element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focus on the input element.

The element also help users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) — because when the user clicks the text within the element, it toggles the radio button/checkbox.

The for attribute of the tag should be equal to the id attribute of the element to bind them together.

The Element

The element defines a drop-down list:

Example

The elements defines an option that can be selected.

By default, the first item in the drop-down list is selected.

To define a pre-selected option, add the selected attribute to the option:

Example

Visible Values:

Use the size attribute to specify the number of visible values:

Example

Allow Multiple Selections:

Use the multiple attribute to allow the user to select more than one value:

Example

The Element

The element defines a multi-line input field (a text area):

Example

The rows attribute specifies the visible number of lines in a text area.

The cols attribute specifies the visible width of a text area.

This is how the HTML code above will be displayed in a browser:

You can also define the size of the text area by using CSS:

Example

The Element

The element defines a clickable button:

Example

This is how the HTML code above will be displayed in a browser:

Note: Always specify the type attribute for the button element. Different browsers may use different default types for the button element.

The and Elements

The element is used to group related data in a form.

The element defines a caption for the element.

Example

This is how the HTML code above will be displayed in a browser:

The Element

The element specifies a list of pre-defined options for an element.

Users will see a drop-down list of the pre-defined options as they input data.

The list attribute of the element, must refer to the id attribute of the element.

Example

The Element

The element represents the result of a calculation (like one performed by a script).

Example

Perform a calculation and show the result in an element:

HTML Exercises

HTML Form Elements

Tag Description
Defines an HTML form for user input
Defines an input control
Defines a multiline input control (text area)
Defines a label for an element
Groups related elements in a form
Defines a caption for a element
Defines a drop-down list
Defines a group of related options in a drop-down list
Defines an option in a drop-down list
Defines a clickable button
Specifies a list of pre-defined options for input controls
Defines the result of a calculation

For a complete list of all available HTML tags, visit our HTML Tag Reference.

Источник

Оцените статью