Max Glenister

Web Dev • 3 min read Plain text

Creating the moanlog search form with CSS

omgmog.net2009

Styling form elements with CSS is never a fun task. Cross browser differences in how form elements are rendered, coupled with changes in font size and browser capabilities, makes predicting how a form will look, after you’ve applied a style to it, a hard job.

When working on the design for moanlog (and the Wordpress theme for moanblog) the search bar was one of the areas that took the most deliberation.

Unable to recover the image :(

The search form as it looks in its finished style maintains the unified style of the soft salmon colours in the moanlog design.

The HTML markup

<form action="/search" method="get" id="searchform">
    <fieldset>
        <input id="search" name="q" type="text" value="Search" />
        <button>Search</button>
    </fieldset>
</form>

Pretty standard markup for a form.. You will have probably noticed that I’m using a button for the submit button, instead of another input, I’m doing this because the button element is easier to style, and for it’s purpose it makes more sense.

To distinguish this particular form from any other forms on the page, I gave the form tag the id “searchform”. Now that we’ve got the HTML done, we need to style the search form.

The CSS

#searchform, #searchform fieldset,
#searchform input, #searchforum button, .button {
     margin: 0; padding: 0; border: 0;/* restore sanity */
     }
#searchform {
     width: 160px;
     background: url(images/nav_images.png) -289px -4px;
     font-family: "Lucida Grande",
          "Lucida Sans", Arial, Verdana, sans-serif;
     }
     #searchform fieldset {
         padding: 8px 8px 0 8px;
         width: 100%;
         height: 30px;
         }
     #searchform #search {
         float: left;
         padding: 3px;
         width: 112px;
         height: 16px;
         border: 1px solid #efdfdb;
         border-right: 0;
         background: #fff;
         color: #ccc;
         }
             #searchform #search:focus {
                 color: #ab6661;
                 }
    #searchform button.button {
         float: left;
         width: 24px;
         height: 24px;
         background: #fff url(images/button.png) left top no-repeat;
         text-indent: -2000px;
         }
             #searchform .button:hover {
                 background-color: #f0ddd8;
                 }

It would take too long to explain the CSS in detail, but to summarise:

  • I’m styling the <form> and <fieldset> tags, instead of utilising extra useless <div>’s in the markup
  • The background image used is actually from a larger sprite that I made with multiple images in, this is to reduce server requests during page loads, but it required some fancy background positioning
  • The :hover state of the <button> is achieved with a transparent png and a change in background-color on hover. I should note that in IE8 (haven’t tested in versions prior but I’ll assume it applies to IE7), the :hover effect for the <button> doesn’t work unless a suitable DTD is specified

To further enhance the search form on moanlog we used javascript to automatically clear the box upon focus

window.onload=function()
     {
     var B=document.getElementById("searchform");
     if(B)
         {
         B.search.onfocus=function()
             {
             if(B.search.value=="Search")
                 {
                 B.search.value=""
                 }
             };
         B.search.onblur=function()
             {
             if(B.search.value=="")
                 {
                 B.search.value="Search"
                 }
             }
         }
     }

This javascript searches for elements with the “searchform” id within the page, when it finds one it checks if the input box has focus - if it does it clears the “Search” text of the input box. Likewise, when the input box hasn’t got focus and is empty it puts the “Search” text back into it.

  • Web Dev · shares #css
    Getting to know @for, @each and @while in Sass

    I use Sass every day, both professionally and in my side projects. One thing that never seems to stick in my mind though is the correct way to use @for, @each or @while. I’m writing this up here for some cathartic release, and hopefully to be of use to somebody else.

    May 2016 • 4 min read
  • Web Dev · shares #css
    Better link underlines with Sass

    There has been some talk lately about how iOS 8 has started to handle typography descenders when text is underlined. They’ve done away with the usual overlapping line intersect in favour of a more aesthetically pleasing line crop.

    November 2014 • 3 min read
  • Web Dev · shares #css
    Some approaches for creating diagonal section separators for your website

    In their simplest form, web pages are made of stacked rectangles. Every block-level element takes up as much vertical space as the content requires, and fills as much horizontal space as is available. Additionally, block-level elements can have their width/height specified. When you have absolutely positioned, or floated elements this changes a bit but the rule pretty much sticks.

    May 2014 • 5 min read

Discussion

No interactions yet

Join the conversation

Reply from your own site with a Webmention (how?).

Webmentions last fetched 24 November 2026, 17:28