Styling input based on their type with CSS


A few weeks ago I posted how I style forms with CSS. Here's another trick that could be used with that technique.

Suppose your creating a form with a couple text fields and a submit button. Now you want to set the width of the text fields, but you want your submit button to be set to the default size. If you did something like this:

input { width: 200px; }

All input tags would be 200px; we only want input tags with type="text" to be 200px; you can use attribute selectors:

input[type="text"] { width: 200px; }

Attribute selectors are super handy but they don't work in IE6! You may still find them useful, just make sure you test on IE to make sure things still look ok. Just a note to always check your styles work on multiple browsers and resolutions - your visitor may be viewing on Mozilla on a 24" monitor or a Lenovo netbook with Chrome, and you want your site to look good in all cases.

Related Entries

51 people found this page useful, what do you think?


Trackback Address: 548/A6AA54B19EACB8FEDECA17816A426F05


On 03/06/2006 at 4:20:30 AM UTC Tony Petruzzi wrote:
"Attribute selectors are super handy but they don't work in IE6!"

That is why CSS has this thing called classes. Designate a class for the text inputs and one for the button inputs.

On 03/06/2006 at 4:50:34 AM UTC Pete Freitag wrote:
Tony, I'm aware of classes, but attribute selectors allow you to style specific elements without designating a class. The saved typing is what makes them handy!

On 03/06/2006 at 6:02:57 AM UTC Dan G. Switzer, II wrote:
Dean Edwards has created a solution for using CSS Selectors in IE6 that he called "IE7" (which isn't the greatest name.)

This library also fixes other IE6 issues, such as:

* Pure CSS Menus * Complexspiral * Box-Model * Fixed Positioning * Dynamic Pseudo Classes * Rounded Corners * XML * PNG Transparency

On 03/06/2006 at 10:07:13 AM UTC Richard@Home wrote:
you can use attribute selectors:*

*which doesn't work for 89%-90% for your potential user-base.

Roll on IE7 and automatic update, I say!

On 03/06/2006 at 10:08:15 AM UTC Richard@Home wrote:
grrr... didn't spot the comments on daughters titchy screen... time to upgrade, I say!

On 02/01/2007 at 9:42:33 AM UTC beth wrote:
This came in super handy. As for Tony, I don't have access to all the code I'm styling. Our forms are generated dynamically from code by a separate dev team, so this is the only option I have for controlling how a group checkboxes are styled.

On 01/07/2008 at 2:36:49 PM UTC Andy wrote:
This works in IE7 and Firefox:

<style type="text/css"> INPUT.text { /* text style */ }

INPUT.file { /* file style */ }

INPUT.button { /* button style */ } </style>

I'm under the impression it works under IE6, but not 100% sure as I don't have access to IE6 any more since upgrading.

On 04/02/2008 at 12:21:38 AM UTC Rambabu wrote:
This not working in IE7 and Firefox:

<style type="text/css"> INPUT.text { /* text style */ }

INPUT.file { /* file style */ }

INPUT.button { /* button style */ } </style>

On 01/19/2009 at 9:55:38 AM UTC Claudiu wrote:
that styling works. once you add that style to your css, make sure you add the class="button" to the input button.

On 06/21/2009 at 12:02:56 PM UTC brandon wrote:
I am using IE 8.0.6001.18783

I created a style that used your input.text and put the class reference. That worked. However, I did the same (copy paste) to input.button and that did not work. In the html, I used 'class="text"' and that worked. but 'class="button"' did not work. I also added input:button, input#button, input->button and anything else I could think of for future parsing approaches to the CSS file. Help?

On 08/18/2009 at 10:05:50 PM UTC Hanan wrote:
Very useful tips indeed. Thanks

On 12/10/2009 at 5:00:54 PM UTC Erik wrote:
Just what I needed! Thanks, Pete.

On 07/26/2010 at 1:22:49 AM UTC Waleed Mohamed wrote:
Create a #registration #form using #HTML5 and #CSS3

On 09/15/2010 at 10:20:47 AM UTC Syed Javed Iqubakl wrote:
This works on all browsers perfectly (even in i.e. 6) you just need to add a jquery

<script language="javascript" src="jquery-1.4.2.js"></script> <script language="javascript" type="text/javascript"> $(document).ready(function(){ $('.testclass input[type=button]').css({ 'border':'1px solid red', 'background':'#ddd', 'width':'200px' }); $('.testclass input[type=text]').css({ 'border':'1px solid blue', 'background':'#ccc' }); }) </script> </head>

<body> <div class="testclass"> <input name="" type="text" /> <textarea name="" cols="" rows=""></textarea> <input name="" type="button" value="submit" /> </div> </body> </html> Plz do comment...

On 02/16/2011 at 7:58:01 AM UTC Internetagentur Ravensburg wrote:
This not working in IE7 and Firefox

On 11/11/2011 at 2:31:38 PM UTC Gunther wrote:
Great tip, thanks for sharing it in this post.

Was exactly what I needed!

Gunther from Brazil

Post a Comment


Spell Checker by Foundeo

Recent Entries


did you hack my cf?