Sunday, February 26, 2012

Using styles for select boxes

Well, select box is one of those elements which cannot be styled reliably with css. The only things explorer supports are to change the background color of the select box and the font-size. The annoying down button and the border cannot be removed in IE. So, it is best not to style a select box for consistency across browsers. Don't worry, there is a work around for the same.

What we need to do is to keep the select box and a span in a div. Then we keep the select box over the span and make the opacity of the select box as 0. Span can be styled reliably and hence will give the user the feel that it is the select box that has been styled. Now, with jquery we need to write events which will populate the span with the values of the select box. The events will be for click and for key up(so that u can select with movement of keys in the keyboard).

Span Click Me

The code:
<div class="container">
<span style="background: none repeat scroll 0% 0% rgb(238, 238, 238);
display: block; width: 100px;">
Click me
</span>
<select id="selBox" style="margin-top: -20px; 
opacity: 0; position: absolute; width: 100px;">
 <option>first option</option> 
 <option value="second option">second option</option>
 <option value="third option">third option</option>
 </select>

</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
type="text/javascript">
</script>
<script type="text/javascript">
 $("#selBox").bind({
        "change": function() {
          $(this).prev().text($(this).find(":selected").html());
        },
        "keyup": function(){
          $(this).prev().text($(this).find(":selected").html());
        }
        });
</script>

Thursday, January 5, 2012

Overlay with transparent background across the whole page

I have written below a simple HTML page which demonstrates how to put an overlay and also how to use a transparent background for the same. You will need 2 divs. One for the transparent background and another for the overlay content. The overlay background is yellow which is made partially transparent.
The original image used for the background of the body is available in :
http://colorfulwallpaper.net/wp-content/uploads/2011/10/A_waterfall_s_in_elevation_.jpg

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
    <style type="text/css">
        #overlayBackground {filter:alpha(opacity=40); /* For IE8 and earlier */}
    </style>
</head>
<body style="background-color:#ccc;">
    <div style="width:1000px;height:1000px;">
        <img src="http://colorfulwallpaper.net/wp-content/uploads/2011/10/A_waterfall_s_in_elevation_.jpg" width="1000px" height="1000px">
    </div>
    <div id="overlay" style="position:fixed;width:500px;height:500px;background:#f00;z-index:1001;top:0;margin:0 100px;"></div>
    <div id="overlayBackground" style="position:fixed;width:100%;height:100%;background-color:#ff0;background:rgba(256,256,0,0.6);opacity:0.6;top:0;z-index:1000;"></div>
</body>
</html>