CSS Custom Search Field or Textfield
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: 90% [?]
Will not work in IE7, sorry… looking for a fix but haven't found any yet.
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
Weird, it worked for me. Awesome tutorial!! Came in very useful, thanks.
nice tut! thx man
Thanks soo much… Worked for me…
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!
And the HTML
Oke, HTML doesn't work, but it's the same as in the tut!
@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
@Mark,
Have you tried
border:none;
instead of border-style:none;
??
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
[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 [ ]
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
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
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.
Loved it was great help =) thanks!
Thanks for the tutorial, it was very helpful. I am looking forward to creating my own custom search boxes.
Thanks for the tutorial, it was very helpful. I am looking forward to creating my own custom search boxes….
very!! Thenksssss!!!
Thanks for your customize text box code…This is what exactly iam looking for…..Its working fine in all browsers….great help……!!!!!
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.
Thank you for this, it will really help in customizing my website. Bookmarked.
nice
thanksss..
Thanks, this was a great tutorial.
well written thanks
css
:focus { outline: 0; }
gets rid of the text field outline (the yellowish box/outline in chrome for example)
how do you control that oversized cursor on firefox? it touched the bottom border on the textbox
I've noticed that the when you first click into the field (before typing anything) the cursor/carat is the same height as the background image in FF 3.6. After you have typed something in the field the cursor returns to the correct size. Any suggestions on how to get around it? I've tried setting font-size on the containing div and the input but no success.
very nice search field i like it..
Thanks
Thanksfor your post! I learn a lot from your post. Have a great day!
ha plz i want to change input field box background color when click in css … any one tell me plz through mail ……….. its urgent…
[...] http://freeyourdesign.com/css/css-custom-search-field-or-textfield/ [...]
Wonderful tutorial, just what I was looking for.
How about an additional chapter on how to change the design of the button, including how to use rolloverimages?
Marvelous, thank you!
Great work.i am developin a site http://www.chaso.tk which required a great deal of coding css (as i didnt found a good ready made css for wap chat site).thanks to you by creating ease in my work as i didnt requires to edit all the input fields independently.putting that snippet of code saved me great work.thanks again
Like Fyd said, you might want to add something that removes border highlighting in Chrome & Safari which in this case scenario is not in sync with the background.
I use:
Input.:focus {
outline-width: 0;
}
Mine seems to work correctly however the white text box still appears inside of the image… the textInput2 background is set to none… any suggestions?
@ Justin, try background:inherit;