in css by fyd on 10 Apr 2008
Tired of those normal old text field boxes? Well, we are going to create our own custom text field. You can use it on a form or for your search field on your blog. We will start out by simply using CSS to customize the color of the text field's background and border. It's simple, but it can make a big difference and help your search field or form fit in with the design of your site.
Simple Text Field Styling with CSS
We will start with the HTML. Setup you basic shell for a HTML file and add the form and input tags in between the body tags.
<html> <head> <title>CSS Custom Text Field</title> <link href="style.css" type="text/css" rel="stylesheet" /> </head> <body> <form> <input type="text" name="field" class="textInput" /> </form> </body> </html>
Notice that in the input tag we are using a type of field - which creates a text field. The name can be whatever you would like. Then we add class="textInput". This connects us to the class .textInput in our CSS file. The name of the class can be whatever you want.
Now for the CSS.
/* CSS Document */
.textInput
{
border: 1px solid #ff0000;
background: #555555;
color: #ffffff;
font-size: 1.1em;
}
border - used to change the style of the border around the text field. Here I changed it to a red color. You can use border to change the thickness, border style (like dashed or dotted), and the color.
background - changes the background color of the text field (we will use an image later). The default background is white.
color - changes the color of the font. The default is black. I changed it to white.
font-size - changes the size of the font which will increase/decrease the size of the text field.
Obviously, you can use other properties to customize the text field. These are some of the more commonly used.
CSS with Image Text Field Customization
For even more customization you can add a background image behind the text field. We need to add a div and class around the input tag in the HTML.
<form> <div class="fieldHolder"> <input type="text" name="field" class="textInput" /> </div> </form>
This new class fieldHolder will hold the text field and the background image we want to use. We will also be changing up textInput.
.fieldHolder
{
width: 182px;
height: 27px;
background: url(tfbg2.gif) no-repeat;
float: left;
}
.textInput2
{
width: 170px;
height: 22px;
background: none;
border: none;
color: #000000;
margin-top: 5px;
margin-left: 5px;
}
First off, here is the image I am using - it needs to be saved to the same folder as you HTML and CSS file.

(right-click save as/save image as)
Back to the CSS.
.fieldHolder
width - this is the width of the image we are using.
height - this is the height of the image we are using.
background - linking to the background image and no-repeat.
float - left, this is optional. It is needed if you want to line up forms side by side (if you do this you might need to add margin-right to space out the fields).
.textInput - styles for the text field
width - width of our text field.
height - height of our text field.
background - no background - we want to use the .fileHolder background.
border - no border.
color - color of our font/text.
margin-top - this is important and deals with getting the cursor to show up inside the 'text field' area in our background image. Because we just made an image we need to tell the cursor were to go. This could be different depending on the image you use.
margin-left - same as above - only position the cursor left or right.
The good thing about encasing the text field inside of fieldHolder is that you can adjust the position of the text field by changing the margins inside of fieldHolder and not have to worry about the cursor position. Once it is set within .textInput you shouldn't have to mess with it.
You can pretty much use whatever background image you can come up with. You will just have to adjust the margin-top and margin-left inside of the .textInput class to position the cursor correctly.
Popularity: 89% [?]



April 23rd, 2008 at 7:08 pm
Will not work in IE7, sorry… looking for a fix but haven't found any yet.
April 24th, 2008 at 5:55 am
you are right - thanks - I forgot to check it in IE7 the problem is going to be with the margin-left. Here is a quick fix
*+html .textInput2
{
margin-left: -50px;
}
this will only be used by ie7
August 4th, 2008 at 2:27 pm
Weird, it worked for me. Awesome tutorial!! Came in very useful, thanks.
October 7th, 2008 at 5:57 am
nice tut! thx man
December 17th, 2008 at 4:39 am
Thanks soo much… Worked for me…
February 15th, 2009 at 12:30 pm
Hey,
Great tut, only some things don't work in IE for me. I don't have a margin problem, the only problem is the border that still displays in IE. Works fine in FF though. This is the code:
HTML:
CSS:
#searchMenu
{
left: 50%;
width: 197px;
height: 26px;
margin-left: 240px;
margin-top: 110px;
position: absolute;
border-style:none;
background-image: url(images/search.png);
}
.textInput
{
left: 50%;
width: 150px;
height: 15px;
position: absolute;
color: #000000;
border-style:none;
margin-left: -90px;
margin-top: 6px;
}
Hope you can help, tnx!
February 15th, 2009 at 12:31 pm
And the HTML
February 15th, 2009 at 12:31 pm
Oke, HTML doesn't work, but it's the same as in the tut!
February 19th, 2009 at 3:03 pm
@Mark
I tried your code with my own textbox image and I had no border in IE7. It's hard to help you with your problem without seeing it. I'll keep looking at it though.
- Thanks
February 22nd, 2009 at 9:28 am
@Mark,
Have you tried
border:none;
instead of border-style:none;
??
March 4th, 2009 at 2:38 am
Hey, I'm trying to make this work while also wrapping the form fields in other classes/divs and now I can't retrieve the data.. the array returns as blank.. When I remove the div classes wrapping the fields this works.. I tried changing the document.getElementbyId to the specific fields but to no avail.. any ideas?? Thanks!!!!
the code:
Name
Email
March 4th, 2009 at 2:39 am
[form action="javascript:get(document.getElementById('myform'));" name="myform" id="myform">
[label]Name[/label]
[div class="fieldHolder"]
[input type="text" name="myfield1″ class="textInput" id="myfield1″]
[/div]
[label]Email [/label]
[div class="fieldHolder"]
[input type="text" name="myfield1232″ class="textInput"]
[/div]
[input type="button" name="button" value="Submit"
onclick="javascript:get(this.parentNode);"]
replaced the with [ ]
March 4th, 2009 at 2:29 pm
So i figured it out by changing the get() function to specifically target the two fields… pretty easy to fix after a crash course on js
March 4th, 2009 at 2:33 pm
Okay, I just realized that my messages aren't even on the right blog..
This script functions great!!!
My difficulty was trying to get some jquery to work with this and now you can simply delete my posts if you like
April 8th, 2009 at 4:03 am
Hello, in Firefox the textfield appears for some reason not transparent but yellow. IE does it (but is positioning the div on top an not in the middle though.) Does anyone have an idea, what might be causing this?
Thank you.
April 20th, 2009 at 12:07 am
Loved it was great help =) thanks!
June 25th, 2009 at 10:27 pm
Thanks for the tutorial, it was very helpful. I am looking forward to creating my own custom search boxes.
September 7th, 2009 at 3:31 pm
Thanks for the tutorial, it was very helpful. I am looking forward to creating my own custom search boxes….
September 14th, 2009 at 9:41 am
very!! Thenksssss!!!
November 27th, 2009 at 12:02 am
Thanks for your customize text box code…This is what exactly iam looking for…..Its working fine in all browsers….great help……!!!!!
January 2nd, 2010 at 11:06 am
Not a bad tutorial, but by wrapping and adjusting the input field inside a div it causes the focus to look bad in Safari and Chrome.
January 8th, 2010 at 2:32 pm
Thank you for this, it will really help in customizing my website. Bookmarked.
January 20th, 2010 at 11:01 am
nice
thanksss..
February 4th, 2010 at 11:12 am
Thanks, this was a great tutorial.