Forum Moderators: not2easy

Message Too Old, No Replies

css for textboxes

         

Ananthi

6:32 pm on Feb 3, 2007 (gmt 0)

10+ Year Member



How do I apply css for TextBoxes only but not for all the <input> types like CheckBoxes, etc.?

cmarshall

6:45 pm on Feb 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are attribute selectors [w3.org] for this, but I'm not sure how well they are supported. I never use them.

I always specify a class for my text items and style that class.

ericjust

3:14 am on Feb 4, 2007 (gmt 0)

10+ Year Member



Normally I would just add a class for the text box. If you wanted to add a little JavaScript to do this automatically you could do something like this (Although it's a little ridiculous :-) but fun and easy.):


<html>
<head>
<style>
.textbox {
color: red;
font-weight:bold;
text-align: center;
}
</style>

<script language="javascript">
<!--

function textBoxStyle() {
var inputs = document.getElementsByTagName('INPUT');
for (var i=0;i<inputs.length;i++) {
if (inputs[i].type.toLowerCase() == 'text') {
inputs[i].className += ' textbox';
}
}
}

//-->
</script>

</head>

<body onload="textBoxStyle()">
<input type="checkbox" />
<input type="button" value="hello" />
<input type="text" value="hello">
</body>
</html>

appi2

8:03 am on Feb 4, 2007 (gmt 0)

10+ Year Member



The best way (my opinion) is to assign an id to the text box.

Remember an id must be unique to the html element, meaning only one unique ID name per page. You can then pick it out with css.
Also if your using a text box then don't forget the label tag. Helps for accessablilty.

This is just an example of different ways to make a text box pretty using either an inline style, a class, an ID or an attribute.
note -> I've added an ID to each just to pass W3c html validation.

##################################

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>CSS Silliness</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
/*Puts each textfield on its own line*/
label {display:block;margin:3px 0;}
#fieldeg {width:340px;}
/*Use a class for one or more html objects */
.textclass {float:right;border:1px solid #ff0000; background-color:#ccc;color:#0000ff;}
/*Use an ID on only one html object */
#textid {float:right;border:1px solid #00ff00; background-color:#000;color:#fff;}
/*Use an attribute selector IE7 FF2 O9 - will apply to all html objects with value="Good eh?" */
[value="Good eh?"] {float:right;border:2px dashed #0000ff; background-color:#ff0000;color:#000;}
</style>
</head>

<body>
<form id="form1" method="post" action="">
<fieldset id="fieldeg"><legend>Text box CSS examples</legend>
<input type="text" id="firstname" style="float:right;border:1px solid #000; background-color:#f1f1f1;color:#0000ff;" value="Some value"/>
<label for="firstname">Using a style attribute</label>
<input type="text" class="textclass" id="lastname" value="Some value"/>
<label for="lastname">Using a class style </label>
<input type="text" id="textid" value="Some value"/>
<label for="textid">Using an ID style </label>
<input type="text" id="textatt" value="Good eh?" />
<label for="textatt">Using an atribute selector </label>
</fieldset>
</form>
</body>
</html>

Edit
Re-reads original post "apply css for TextBoxes only". Ahh yeah ... assign a class. DOH. Anyhoo bah!

Ananthi

2:52 pm on Feb 5, 2007 (gmt 0)

10+ Year Member



I appreciate your responses.

Fotiman

5:59 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I agree with ericjust's suggestion, but I don't find it rediculous. In fact, Dustin Diaz posted a great article "Styling Inputs" that does exactly what you're looking for. Here are some of the highlights of the article (since I can't link to it).


If all browsers supported attribute selectors, we could easily do the following:

input[type='text'] { font:bold 0.8em 'courier new',courier,monospace; }
input[type='radio'] { margin:0 20px; }
input[type='checkbox'] { border:2px solid red; }

Since not all browsers support attribute selectors (IE doesn't), we can simply use className's instead, and use JavaScript to automate the process.


function appendInputTypeClasses() {
if (!document.getElementsByTagName )
return;
var inputs = document.getElementsByTagName('input');
var inputLen = inputs.length;
for ( i=0;i<inputLen;i++ ) {
if ( inputs[i].getAttribute('type') )
inputs[i].className += ' '+inputs[i].getAttribute('type');
}
}

Finally, make sure your CSS for this handles both the type attribute and the type class. That way, when browser support for attribute selectors becomes more widespread, you can just remove the JavaScript for appending the classNames.


input[type='text'],input.text { font:bold 0.8em 'courier new',courier,monospace; }
input[type='radio'],input.radio { margin:0 20px; }
input[type='checkbox'],input.checkbox { border:2px solid red; }

appi2

6:40 pm on Feb 5, 2007 (gmt 0)

10+ Year Member




Since not all browsers support attribute selectors (IE doesn't),

It does. IE7.

Fotiman

6:51 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, I meant to clarify I meant IE6.

ericjust

6:57 pm on Feb 5, 2007 (gmt 0)

10+ Year Member



OK, so it's not ridiculous. :-)

It is a nice solution since it limits the amount of markup you have to include in your raw HTML. It makes it easier to change.