Cross Browser Compatible CSS Opacity

This is something that i like to write down on my site; cross browser CSS opacity. CSS opacity is used on my daily web development almost every single time. Every time when i need a cross browser opacity solution, i will have to search on Google and try to remember which one is the one that i previously used, that worked for all browsers. There is never a permanent updated solution on Google that is constantly updated with the latest browser. Most of them are written centuries ago. Things have changed. Many new versions and browsers have also emerged. I need a cross browser compatible opacity solution that is updated and works.

IE 8 changes

IE 8 has changed CSS Opacity a little. The old CSS declaration might not cause your div block and images to fade like it used to do. In IE8, a new declaration will have to be formed.

.myclassname /* classname */
{ 
-moz-opacity:.70; 
-ms-filter:”alpha(opacity=70)”; 
filter:alpha(opacity=70); 
opacity:.70; 
}

Please visit the IEBlog for more information on IE 8 changes.

Cross Browser Opacity Declaration

The updated solution for cross browser compatible opacity solution that worked on older and newer browsers is:

.myclassname /* classname */
{ 
	filter: alpha(opacity:0.7);
	KHTMLOpacity: 0.7;
	MozOpacity: 0.7;
	-khtml-opacity:.70; 
	-ms-filter:”alpha(opacity=70)”; 
	-moz-opacity:.70; 
	filter:alpha(opacity=70); 
	opacity:.70;
} 

The above declaration support 1.x versions of Safari and both newer and older browser too. It has been tested on,

  • Standard: FF gt 1.5, Opera, Safari
  • IE lt 8
  • IE 8
  • Safari 1.x
  • FF lt 1.5, Netscape
  • Flock 1.2+
  • Iceweasel 2.x
  • Dillo 0.x
  • Minefield 3.x
  • Chrome 1.x
  • k-Melon 1.x
  • SeaMonkey 1.x
  • Shiretoko 3.x

Tutorial: Simple Cross-browser tooltip with CSS

We see tooltip almost everyday on the web. And most of us know there are a lot of way of doing this. Personally, i think the easily way of accomplished this is by using CSS. Although there are disadvantage and advantages of doing this which we will discuss later. Nonetheless, it is one of the most easiest way of accomplished a tooltip. (although the MOST easily way is to just use a title attribute in the tag without any additional code)

The Concept

This method neglect the use of 'title' and apply 'span' tag instead. We know that span tag won't add additional break to a text (which is what we want!) and is usually used to highlight text on a particular paragraph. Therefore, span tag can be used perfectly to substitute title attribute although it cannot totally replace it. So what we do here is by placing the span tag within the anchor tag and style the span tag with a absolute position while the anchor will have relative position. This way, the span tag can have any position away from the anchor tag which provides a tooltip like position. The last missing puzzle will be using an pseudo class to perform a CSS hover on the anchor for displaying and hiding the tooltip. That's it.

The Code

The concept illustrate the basic use of this method. We can further enhance it to cater for more visual effect such as adding additional image on the tooltip.

The HTML

	<a href="#"> Mouse over me to see what i have to say!
	<span class='tooltip'>
	<span class='top'></span>
	<span class='middle'>this is a testing site to demonstrate how simple a tooltip be build without affecting the efficiency of the overall site. Only pure CSS implementation is required. No additional JavaScript is needed actually. Well, unless you want to do something different like dragging your tooltip around a particular images, you may consider using a simple jQuery or JavaScript code to accomplish that.</span>
	<span class='bottom'></span>
	</span>
	</a>

We will have an anchor and inside the anchor we will have two things, the link message and the tooltip. The link message is 'Mouse over me to see what i have to say!' which is quite obvious and the span tag will locate next to it. Actually we only need one span tag if we are doing a simple border tooltip without images but we want to enhance overall look of the tooltip so we need a few nested span within the main span tag as shown above.

The CSS

a {
    position:relative;
    color:#3CA3FF;
    font-weight:bold;
    text-decoration:none;
}
a  span{ display: none; }
a:hover{ 
color: #aaaaff; 
background:;
}
a:hover span.tooltip{
    display:block;
    position:absolute;
    top:0px; left:0;
	padding: 15px 0 0 0;
	width:200px;
	color: #993300;
    text-align: center;
	filter: alpha(opacity:70);
	KHTMLOpacity: 0.70;
	MozOpacity: 0.70;
	opacity: 0.70;
}
a:hover span.top{
	display: block;
	padding: 55px 8px 0;
    background: url(../images/main_image.png) no-repeat top;
}
a:hover span.middle{ 
	display: block;
	padding: 0 8px; 
	background: url(../images/content_image.png) repeat bottom; 
}
a:hover span.bottom{
	display: block;
	padding:3px 8px 40px;
    background: url(../images/main_image.png) no-repeat bottom;
}

I will explain the CSS code above in point form

  • 'a {...': We declare the basic of the anchor tag. eg. color, decoration, bold, position but the key is position relative as stated on the concept section
  • 'a span{...': the tooltip should be hidden at first
  • 'a:hover{...': We need to set the hover z-index and a small hack 'background:;' for ie.
  • 'a:hover span.tooltip{..': this is the tooltip and the declaration of important are 'width', 'position' and 'display'. Width is the size of the image, position is describe on the concept section and display is block because we need to make the tooltip available when hover. Others should be simple to understand
  • 'a:hover span.top{..': the top part of the image to be display
  • 'a:hover span.middle{ ..': the image part where it will be scretched
  • 'a:hover span.bottom{..': the bottom part of the image to be display

The reason why it is 'a:hover span...' for the image declaration was because we want the hover to be maintained when our mouse move on top of the tooltip instead of just having the normal 'mouseover' on the anchor tag which will make our tooltip display whenever our mouse move away from the anchor on top of the tooltip. If you just want a simple tooltip, remove the 'top', 'bottom' and 'middle' from the above declaration and give border to 'tooltip' (you may want to remove the opacity declaration for close browser) which will easily give you a simple tooltip in a few second but the visual part will be lost.

The Advantages

Here are some of the advantages of using this method,

  1. Efficient compare to JavaScript method
  2. Independent. User can turn off JavaScript and it will still work
  3. Quick and dirty. A few lines of CSS and we have our tooltip
  4. No redundency. We will be using CSS even with JavaScript
  5. Etc.

The Disadvantages

Here are some of the disadvantages,

  1. Bad for SEO. search engine spider look at title and also the use of span within an anchor is not a standard practice
  2. Required certain CSS hack for it to work at lower browser (not really a disadvantage but consider as a problem)
  3. The tooltip cannot follow the mouse wherever it goes.(image tooltip)
  4. If this method has a title on it, it will display both the tooltip and the default title altogether. Bad
  5. Etc.

The Demo

Here are the files and demo for simple cross-browser tooltip with CSS

Tutorial: How to vertical align center on the screen while scrolling with CSS

I believe some of you out there also faced this problem where your container is align on the center of the screen but when the screen is too long, the scroll bar appeared and your box remained on the top of the screen and you say "oh shit!". Searching all over the internet and found only how container can be align on the center of the screen but not when it was scrolling? I think this article will solve your problem.

The Problem

We are assuming here that you have aligned your container at the center of the screen with position absolute. If you are still having this problem, you may want to visit How to align center on the screen when using position absolute with CSS. Once your container has aligned on the center of the screen. You meet the problem of having it follow you while you scroll down the screen. I like to say that the container is not persistence enough! Let's make it persistence to follow your scroller.

The Concept

Basically, you can solve your container not aligned on center while scrolling with the following concept:

  • You can create a code of jQuery/JavaScript to accomplish this by making your container chasing after your scroller. Thus, changing the container position on every scroller movement. (not good)
  • The other way is to just CSS (which we are doing) to cheat the above effect. Instead of chasing after the scroller, we fixed the position on the center of the screen while having absolute to cover all other elements!

We will be doing the second approach which is more efficient and is the more correct way of doing. The reason why it must be position fixed is that the container should not move! Thus, stay at the center of the screen when you scroll. As for the reason why it must also be position as absolute is because we need to cover up all element below to prevent any user from clicking on the elements!

The Solution

The Trick is fairly simple after you have accomplished your center alignment on the screen with absolute position. I take the following code from the article and change it so that the container will remain center while scrolling.

#box
{
	width: 150px;
	height: 150px;
	background: red;
	padding: 5px;
	border: #CCC solid 1px;
	text-align: center;
	position: fixed;
	margin-left: -75px;
	margin-top: -75px;
	left: 50%;
	top: 50%;
	
}

The above is the one was in the article given in the link. Now i will just change the styling a bit to red and size accordingly and the new code that will make it remain center will be as follow,

#box
{
	width: 150px;
	height: 150px;
	background: #000;
	padding: 5px;
	border: #CCC solid 5px;
	text-align: center;
	position: fixed;
	margin-left: -75px;
	margin-top: -75px;
	left: 50%;
	top: 50%;
}

The main changes done was 'position:fixed' which fixed your container at the center of the screen whenever it go! Persistence enough.

The Demo

The following links are the demo for persistence and non-persistence container:

Tutorial: How to create an image navigation bar with CSS

There are times when we want to build a navigation bar for ourselves in the simplest form. Especially when we want an image on top of an image or below an image but perform it with many many redundancy codes made in HTML, CSS or even involved JavaScript. What i am going to demonstrate here is a simple yet powerful method we can all use in CSS to create such navigation bar that attached an image on top of it.

The Problem

I wanted to create a navigation bar that will display an image when hover on it and display when it is not being hovered.

The Concept

The solution to this problem is fairly simple but usually over complicated. We want an image and a word to be placed together in the same row and column on the navigation bar. Some people will do this with the following methods:

  • Create a two row of div block align them perfectly top to be image, bottom to be word
  • Use jQuery or JavaScript to assist with the effect and display of the image
  • Use position absolute to align it on top of the image
  • The list goes on..

To be honest, what we really need is just an unordered list with some CSS. What we do is to stretch the list box so that it can contain an image and place a anchor as the child of the list. This way, the image can be display on the list box as background while the word will always be display as it is. Simple and sweet.

Solution

I will split this into two section as this is all we need to accomplish this effect, HTML and CSS.

HTML

			<ul id="content-nav">
				<li class="boxes" id="0"><a href="#">HOME</a></li>
				<li class="boxes" id="1"><a href="#">ROCK</a></li>
				<li class="boxes" id="2"><a href="#">BAD</a></li>
				<li class="boxes" id="3"><a href="#">TIME</a></li>
			</ul>

Assume we have an unordered list as shown above in HTML. Now we will use CSS to demonstrate how it can be done.

CSS

#content-nav
{	
	list-style: none; /*removes the underline below the link*/
	padding: 0; /*removes the padding*/
}
#content-nav li
{
	
	float: left; /*this makes the element displays in a horizontal line*/
	width: 50px; /*This allows the picture to be fit in the block*/
	height: 50px; /*This allows the picture to be fit in the block*/
	margin-left: 50px;
	cursor: pointer;
	
}
#content-nav li:hover
{
	background: transparent url(../images/1.png) no-repeat top center; /*this makes the picture appears in the top center without any repeats*/
}
#content-nav li a
{
	color: #777;
	display: block;  /*The element will be displayed as a block level element */
	width: 50px; /*This allows the picture to be fit in the block*/
	padding: 50px 0px 5px; /*There is 60px of padding on the top, 10px of padding on the right and left, and 5px of padding on the bottom*/
	text-decoration: none; /*removes all text decoration*/
}

What we do here is to remove all styling on the anchor so that it won't display any underline etc. Then, we set the anchor to display: block so that it will go to the next line after the image appeared. but we will have to provide a space for the image. Thus, padding is used instead. The description i am talking about is located at #content-nav li a. We set the background image when hover with this CSS declaration #content-nav li:hover. Others should be self explain stuff.

Demo

You can view the demo site at CSS image navigation bar with CSS. It will display the image when mouse hover. Cheers

Tutorial: How to align center on the screen when using position absolute with CSS

As a developer, i always have the chance to come across position: absolute when doing layout and dynamic animation such as pop out box and etc. But for many years it seems like until today then i realize how to correctly align an absolute position container on the center of the screen. Many tutorial uses many trick in order to position their container on the center of the screen for either alert or selective option but it is really REALLY easy to align your container on the center when using position absolute. I will try to show you some simple and easy way to do this with little complication (The reason why it is difficult are mostly because there are so much words written on some tutorial that make it hell confusing).

Align center of the screen

In order to align your container to the center of the screen, what you need is only the following code.

margin: auto;
left:0; right:0;
top:0; bottom:0;

That's all! Simple and easy to use. We usually use the following to align to center

margin: 0 auto;

This is when we are not using position absolute but this is actually the correct way to align horizontally when using position absolute too! But there is a criteria when we try to align it horizontally using the above code, left and right. We have to reset the right and left in order for the browser to render it to horizontal center with the above code. For vertical center alignment, we will have to reset the top and bottom part. And use the below code to render vertically but not horizontally.

margin: auto 0;

Sound logical right? To sum it up to align horizontally and vertically, we will use the first code that was presented in this article which reset four side and set auto for margin (so that it automatic align for the four side)

Demo

Simple and quick demo to show you that the above code work. Below is the CSS used in the demo

body
{
	text-align: center;
	margin: 0 auto;
}
#box
{
	margin: auto;
	left:0; right:0;
	top:0; bottom:0;
	position: absolute;
	width: 200px;
	height: 200px;
	border: #000 solid 1px;
	background: #82713F;
	text-align: center;

}

The HTML is just a div box

<div id="box"></div>

You can visit the demo at align center of the screen when using position absolute

P.S: This work in all browser except IE

Method that work for all browser

There is a simpler method that will work in ALL browser. But it will required you to know your own block size (the container width and height). It's really just maths, for example, you have a container with a width of 60% and height of 40%, you will want to move your container 20% from its position to the right (assuming it is at coordinate 0,0) and 30% from top to bottom. Why 20%? Because we must balance between the left and right side of your 60% width container, so 100-60=40/2=20%! Same thing goes for your height. With these condition applied, i can show you the demo with the following code.

body
{
	text-align: center;
	margin: 0 auto;
}
#box
{
	position: absolute;
	width: 60%;
	height: 40%;
	left: 20%;
	top: 30%;
	border: #000 solid 1px;
	background: #82713F;
}

And the HTML part will be the same as our previous demo, you can view the result with different browser at align center of the screen when using position absolute with all browser

Another method also similar to the above but it uses another concept. Instead of calculating the right and left side of the width required in percentage, we use the known width and height which usually locate at coordinate (0,0) and push it opposite direction after we request the CSS to move to center by left and top to 50%. Talk won't help explain as much as example, with the following code it will illustrate better,

body
{
	text-align: center;
	margin: 0 auto;
}
#box
{
	position: absolute;
	width: 500px;
	height: 500px;
	left: 50%;
	top: 50%;
	margin-left: -250px;
	margin-top: -250px;

	border: #000 solid 1px;
	background: #82713F;
}

What the above does is giving it a fixed width and height (500px), push it to 50% from left to right and top to bottom. It will not align on the center with just this, we will have to use margin to push in the opposite direction(-250px) by half in order for it to align to center. You can look at the result at align center of the screen when using position absolute with all browser compatibility