Saturday, July 28, 2012

CSS Background transparency without affecting child elements, through RGBa and filters

The solution!

.alpha60 {
/* Fallback for web browsers that doesn't support RGBa */
background: rgb(0, 0, 0);
/* RGBa with 0.6 opacity */
background: rgba(0, 0, 0, 0.6);
}


A little caveat

Shockingly enough (erm), no version of Internet Explorer supports RGBa colors (i.e. not IE 6, IE 7 or IE 8 at the time of this writing). However, and lucky for us, in year 2000 Microsoft went crazy with implementing various filters in IE. One of them are the gradient filter, and what we can do is use that and just define the same start and end color. “Ok, but how do I get the transparency”, you might be thinking now. The answer to that is that you will declare that as part of the color hex value. A CSS gradient filter achieving the same effect as the CSS code above would look like this:



.alpha60 {
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);
}



Source From: http://robertnyman.com/2010/01/11/css-background-transparency-without-affecting-child-elements-through-rgba-and-filters/


---------------------------------------
Sample Code::::

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>www.fouressinsurance.com</title>
<style>
    .main{
        height:300px;
        width:100px;
        background: rgba(0, 0, 0, 0.5);
        filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);
}
    .inner{ color:#ff0000; opacity:100%; }
</style>
</head>

<body style="background:#fff">
<div class="main">
    <div class="inner">Hyderabad Web Designer www.hydwebdesigner.com</div>
</div>
 </body>
</html>
 

---------------------------------------

jQuery and JavaScript CSS !important

jQuery CSS !important

One thing that everyone must have wonder (those that haven’t tried yet) is whether jQuery support declaration of !important for their css() method such as this.
1    jQuery('#id').css('height', '200px !important');

You will find that nothing will happen with a declaration of such manipulation. If we were to try out on pure JavaScript with the above declaration, it will be something like this.
1    document.getElementById('id').style.height = "200px !important";

Having a pure JavaScript instruction such as this will not work in most browser other than safari 3++. Hence, jQuery will definitely won’t work too. This is not a bug but something that most browser doesn’t acknowledge a need since inline style already have the highest priority other than user defined ones. (unless you do not want your user to change how they view your website).

Solution to jQuery CSS !important

Well, the typical way of getting !important will not work. Rather, you might want to try CSS DOM cssText. Hence, you will force !important to your jQuery statement by doing this.
1    jQuery('#id').css('cssText', 'height: 200px !important');

However, most people will be unfamiliar with cssText. cssText will overwrites the css of that particular element. Hence, you will have to redeclare the rule for that particular element all over again. On the other hand, if you are using JavaScript cssText, different browser treat cssText different such as ie you will define this way,
1    document.getElementById('id').style.cssText = 'height: 200px !important'

While normally you will use it this way.
1    document.getElementById('id').cssText = 'height: 200px !important'

Definitely, there is a better alternative such as using the cssRules/rules array which can specify the rule in that element to be modify.

The other more practical way of doing it is by introducing new class for your element. Instead of styling through jQuery or DOM object which is applying inline styling (means highest priority already), you should leave every majority styling or best all of the styling to an external style sheet that help you reduce some bytes on the script and also improve maintainability for both your code and style. You can do this using the toggleClass which adds a new class to that element automatically.
1    jQuery('#id').toggleClass('classname');

or you can do this in JavaScript by replacing the class (this is more efficient than the jQuery one since there isn’t any additional checking and stuff)
1    document.getElementById('id').className = 'classname';

Removing inline CSS styles using Jquery?

You can remove that inline style with this:

$(document).ready(function(){
  $('.myheader').each(function(idx,el){
    el.style.marginTop='';
  });
});

Thursday, July 5, 2012

IE9 - IE10pp4 CSS Hack


#element {
    color:orange;
}
#element {
    *color: white;    /* IE6 + 7, doesn't work in IE8/9 as IE7 */
}
#element {
    _color: red;     /* IE6 */
}
#element {
    color: green\0/IE8+9; /* IE8 + 9 + IE10pp4  */
}
:root #element { color:pink \0/IE9; }  /* IE9 + IE10pp4 */

Clearfix - Force Element To Self-Clear its Children

.clearfix:after {
     visibility: hidden;
     display: block;
     font-size: 0;
     content: " ";
     clear: both;
     height: 0;
     }
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */
 
 
Source from: http://css-tricks.com/snippets/css/clear-fix/ 

Wednesday, July 4, 2012

Stick div at top after scrolling


<style>
#sticky {
  background-color: #fff;
  -moz-border-radius: 0.5ex;
  -webkit-border-radius: 0.5ex;
  border-radius: 0.5ex;
  color: #000;
  font-size: 2em;
  padding: 0.5ex;
  width: 600px;
  }
#sticky.stick {
  -moz-border-radius: 0 0 0.5em 0.5em;
  -webkit-border-radius: 0 0 0.5em 0.5em;
  border-radius: 0 0 0.5em 0.5em;
  position: fixed;
  top: 0;
  z-index: 10000;
  }
</style>
</div>

<!-- If you have jQuery directly, then skip next line -->
<script src="http://www.google.com/jsapi"></script>

<script type="text/javascript">
// If you have jQuery directly, then skip next line
google.load("jquery", "1");

function sticky_relocate() {
  var window_top = $(window).scrollTop();
  var div_top = $('#sticky-anchor').offset().top;
  if (window_top > div_top)
    $('#sticky').addClass('stick')
  else
    $('#sticky').removeClass('stick');
  }
// If you have jQuery directly, use the following line, instead
// $(function() {
// If you have jQuery via Google AJAX Libraries
google.setOnLoadCallback(function() {
  $(window).scroll(sticky_relocate);
  sticky_relocate();
  });
</script>


Source From: http://blog.yjl.im/2010/01/stick-div-at-top-after-scrolling.html