break free by learning
  • Home
  • About
  • Free Stuff

CSS Custom Search Field or Textfield

Apr 10, 2008 | css | fyd

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.

The HTML
The CSS

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.
bg
(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 HTML
The CSS

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: 88% [?]

Related Post:

  • CSS Gradient Background Fade (19)
  • CSS Horizontal Menu (0)
  • CSS Two Classes for one Item (0)

38 Responses to “CSS Custom Search Field or Textfield”

  1. Yan says:
    April 23, 2008 at 7:08 pm

    Will not work in IE7, sorry… looking for a fix but haven't found any yet.

    Reply
  2. fyd says:
    April 24, 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

    Reply
  3. Web Design says:
    August 4, 2008 at 2:27 pm

    Weird, it worked for me. Awesome tutorial!! Came in very useful, thanks.

    Reply
  4. Pos says:
    October 7, 2008 at 5:57 am

    nice tut! thx man

    Reply
  5. Swanky says:
    December 17, 2008 at 4:39 am

    Thanks soo much… Worked for me… :D

    Reply
  6. Mark says:
    February 15, 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!

    Reply
  7. Mark says:
    February 15, 2009 at 12:31 pm

    And the HTML :P

    Reply
  8. Mark says:
    February 15, 2009 at 12:31 pm

    Oke, HTML doesn't work, but it's the same as in the tut!

    Reply
  9. fyd says:
    February 19, 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

    Reply
  10. BBBBgx says:
    February 22, 2009 at 9:28 am

    @Mark,
    Have you tried
    border:none;
    instead of border-style:none;
    ??

    Reply
  11. Ryan says:
    March 4, 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

    Reply
  12. Ryan says:
    March 4, 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 [ ]

    Reply
  13. Ryan says:
    March 4, 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

    Reply
  14. Ryan says:
    March 4, 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

    Reply
  15. Peter says:
    April 8, 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.

    Reply
  16. Arfan Chaudhry says:
    April 20, 2009 at 12:07 am

    Loved it was great help =) thanks!

    Reply
  17. Find Online Schools says:
    June 25, 2009 at 10:27 pm

    Thanks for the tutorial, it was very helpful. I am looking forward to creating my own custom search boxes.

    Reply
  18. Ave says:
    September 7, 2009 at 3:31 pm

    Thanks for the tutorial, it was very helpful. I am looking forward to creating my own custom search boxes….

    Reply
  19. Sfenksmmy says:
    September 14, 2009 at 9:41 am

    very!! Thenksssss!!!

    Reply
  20. boon says:
    November 27, 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……!!!!!

    Reply
  21. Brad says:
    January 2, 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.

    Reply
  22. SJ says:
    January 8, 2010 at 2:32 pm

    Thank you for this, it will really help in customizing my website. Bookmarked.

    Reply
  23. wiyono says:
    January 20, 2010 at 11:01 am

    nice
    thanksss..

    Reply
  24. grafxguy says:
    February 4, 2010 at 11:12 am

    Thanks, this was a great tutorial.

    Reply
  25. abigail says:
    May 3, 2010 at 12:17 pm

    well written thanks

    Reply
  26. fyd says:
    May 19, 2010 at 1:56 pm

    css
    :focus { outline: 0; }
    gets rid of the text field outline (the yellowish box/outline in chrome for example)

    Reply
  27. marvin says:
    July 15, 2010 at 11:22 pm

    how do you control that oversized cursor on firefox? it touched the bottom border on the textbox

    Reply
  28. Andrew says:
    July 21, 2010 at 9:16 pm

    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.

    Reply
  29. dotiv says:
    September 2, 2010 at 3:47 am

    very nice search field i like it..
    Thanks

    Reply
  30. jakel says:
    September 11, 2010 at 9:14 am

    Thanksfor your post! I learn a lot from your post. Have a great day!

    Reply
  31. shah says:
    November 2, 2010 at 11:38 am

    ha plz i want to change input field box background color when click in css … any one tell me plz through mail ……….. its urgent…

    Reply
  32. website design progression « Imratzio's Blog says:
    December 13, 2010 at 8:31 am

    [...] http://freeyourdesign.com/css/css-custom-search-field-or-textfield/ [...]

    Reply
  33. Claus says:
    June 30, 2011 at 12:17 am

    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?

    Reply
  34. Grullo says:
    August 22, 2011 at 6:42 pm

    Marvelous, thank you!

    Reply
  35. Mobile chat says:
    August 24, 2011 at 10:27 am

    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

    Reply
  36. Marcel Bank says:
    November 15, 2011 at 5:39 pm

    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;
    }

    Reply
  37. Justin says:
    November 16, 2011 at 7:06 pm

    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?

    Reply
  38. Marcel Bank says:
    December 7, 2011 at 5:21 pm

    @ Justin, try background:inherit;

    Reply

Leave a Reply

Click here to cancel reply.

Categories

  • css (8)
  • freelance (2)
  • illustrator (1)
  • inspiration (4)
  • jquery (1)
  • photoshop (9)
  • resources (8)
  • site news (6)
  • the basics (4)
  • web design (1)
  • whats good (1)
  • wordpress (1)

Recent Posts

    Archives

    • April 2010 (1)
    • April 2009 (1)
    • May 2008 (3)
    • April 2008 (14)
    • March 2008 (8)
    • February 2008 (8)
    • January 2008 (11)

    ©2013 freeyourdesign | Designed by Michael Hargis | | twitter icons