Home

How to Create a Drop-Down List for Your HTML5 and CSS3 Based Web Pages

|
Updated:  
2016-03-26 13:18:08
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

The drop-down list is a favorite selection tool of HTML5 and CSS3 web developers. The drop-down list has some very useful elements that make it a huge web developing crowd pleaser:

  • It saves screen space. Only the current selection is showing. When the user clicks the list, a series of choices drop down and then disappear again after the selection is made.

  • It limits input. The only things the user can choose are things you've put in the list. This makes it much easier to handle the potential inputs because you don't have to worry about typing errors.

  • The value can be different from what the user sees. This seems like an odd advantage, but it does turn out to be very useful sometimes.

    image0.jpg

The code for this simple drop-down list follows:

<!DOCTYPE html>
<html lang = "en-US">
 <head>
 <meta charset = "UTF-8">
 <title>basicSelect.html</title>
 </head>
 <body>
 <form action = ">
  <p>
  <label>What is your favorite color?</label>
  <select id = "selColor">
   <option value = "#ff0000">Red</option>
   <option value = "#00ff00">Green</option>
   <option value = "#0000ff">Blue</option>
   <option value = "#00ffff">Cyan</option>
   <option value = "#ff00ff">Magenta</option>
   <option value = "#ffff00">Yellow</option>
   <option value = "#000000">Black</option>
   <option value = "#ffffff">White</option>
  </select>
  </p>
 </form>
 </body>
</html>

The object is a bit different from some of the other input elements you're used to, such as

  • It's surrounded by apair. These tags indicate the entire list.

  • Theobject has anattribute. Although the object has many other tags inside, typically only the object itself has an attribute.

  • It contains a series ofpairs. Each individual selection is housed in an set.

  • Eachtag has a value associated with it. The value is used by code. The value isn't necessarily what the user sees.

  • The content betweenis visible to the user. The content is what the user actually sees.

Select boxes don't require the drop-down behavior. If you want, you can specify the number of rows to display with the attribute. In this case, the number of rows you specify will always be visible on the screen.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].