Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Design / CSS Opacity – 5 Faces of Transparency

CSS Opacity – 5 Faces of Transparency

by ShibaShake 6 Comments

Opacity –

seems like it should be an easy task, but unfortunately that is not so.

There are at least five ways to change the opacity or transparency of regions within a web-page. What is most difficult is to make opacity work well on Internet Explorer; which often has aliasing issues, and sometimes does not conform to CSS standards.

Two of the most common reasons for changing opacity are –

  1. Text on images – To clearly display text on an image background, while still having part of the background image show through.
  2. Hover effect – To indicate that a region is selectable. When a user hovers his mouse pointer over a selectable region, that region is made less opaque or more transparent.

5 Faces of Transparency

Below we present 5 different ways to change opacity. If you are running on Firefox, these different options may all look similar to you, however, the results will be different on Internet Explorer, including Internet Explorer 8.

Ultimately, the method that gets the best results is to use transparent PNGs. You can just skip to that section if you are not interested in looking at the other less effective alternatives.

CSS Opacity 1 –
Regular CSS Opacity Commands

CSS Opacity 2 –
New CSS Opacity commands for modern browsers

CSS Opacity 3 –
Using Javascript to Set Opacity for Mouse Events

CSS Opacity 4 –
Split the text into a separate, non-child container

CSS Opacity 5 –
Transparent PNG Overlay

Internet Explorer Results

Screen-shot of opacity results on Internet Explorer 8

CSS Opacity 1

In the first example, we use the standard CSS-2 opacity commands.

.css-opacity1 {
	background-color:#ffffff;

	-moz-opacity:0.5;
	-khtml-opacity: 0.5;	
	opacity:0.5; 
	filter:alpha(opacity=50);
	zoom:1; /* hasLayout for IE6 and IE7 */
}

.css-opacity1:hover {
	-moz-opacity:1.0;
	-khtml-opacity: 1.0;	

	opacity:1;
	filter:alpha(opacity=100);
}

The filter statement is for IE compatibility, while opacity is the CSS standard which is used by all other browsers (FireFox, Opera, Safari).

moz-opacity is only used in really old Mozilla browsers such as Netscape Navigator. There are few such users today, thus this statement is probably no longer necessary. The same is true of khtml-opacity which is only used in old versions of Safari (1.x).

The zoom statement is important for hasLayout issues in IE-7 and IE-6.

This standard method of opacity works well on most browsers except for Internet Explorer. If you look at the Internet Explorer results, you will see that the transparency gets applied to the text as well. This gives it a jagged and distorted appearance (aliasing).

Screen-shot of opacity results on Internet Explorer 8

CSS Opacity 2

In this second step, we experiment with the new CSS-3 standard, which includes an alpha channel.

.css-opacity2 {
	background: rgba(255, 255, 255, 0.5); 
}

.css-opacity2:hover {
	background: rgba(255, 255, 255, 1.0); 
}

This rgba method works well, but is not supported by Internet Explorer (not even Internet Explorer 8).

Further research shows that there is a new ms-filter method for opacity in IE-8.

With hopeful anticipation we add in the ms-filter statement.

.css-opacity2 {
	background-color:#ffffff;

	background: rgba(255, 255, 255, 0.5); 
	-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(opacity=50)"; /* IE 8 */
	zoom:1; /* so the element "hasLayout"
	/* or, to trigger "hasLayout" set a width or height */

}

.css-opacity2:hover {
	background: rgba(255, 255, 255, 1.0); 
	-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
	zoom:1;
}

Unfortunately, the transparency still gets applied to the text. Separating the text into a child div that is fully opaque does not resolve the issue.

Screen-shot of opacity results on Internet Explorer 8

CSS Opacity 3

In the third example, we use JavaScript to change the opacity values during run-time.

.container-pos {
	margin-bottom:20px;
	padding:20px;
	}

.css-opacity3 {
	background-color:#ffffff;
	zoom:1; /* hasLayout for IE6 and IE7 */
}

.opacity50 {
    opacity:0.5; 
    filter:alpha(opacity=50); 
}

.opacity100 {
    opacity:1.0; 
    filter:alpha(opacity=100); 
}

This is the HTML/Javascript code used on the text container

<div class="container-pos css-opacity3 opacity50" onmouseover="this.className='container-pos css-opacity3 opacity100'" onmouseout="this.className='container-pos css-opacity3 opacity50'">
<strong>CSS Opacity 3</strong> - <br/>
Using Javascript to Set Opacity for Mouse Events
</div>

Not too surprisingly, the text still looks jagged on IE.

Screen-shot of opacity results on Internet Explorer 8

CSS Opacity 4

In the fourth example, we divide the text into a separate non-child, div container, as is shown below –

<div class="container-pos css-opacity4"></div>
<div class="css-opacity4b"><strong>CSS Opacity 4</strong> - <br/>
Split the text into a separate, non-child container</div>

The first container (css-opacity4) draws a transparent background. The second (non-child) container renders the text.

.css-opacity4 {
	height:80px;
	background-color:#ffffff;
	
	opacity:0.5; 
	filter:alpha(opacity=50);
}

.css-opacity4b {
	position:relative;
	top:-100px;
	left:20px;
	width: 440px;
}

.css-opacity4:hover {
    opacity:1;
    filter:alpha(opacity=100)
}

This removes the text transparency and aliasing in the IE results, however, it is not a very viable option because the background container is decoupled from its contents, making layout extremely rigid and difficult to maintain.

Screen-shot of opacity results on Internet Explorer 8

For example, we have to manually set the width and position of the overlay text, as well as the height of the background region. In the screenshot above, you can see that we have set the width of the overlay text to be too wide.

In addition, the hover functionality does not work properly because the text container is blocking the background container. To enable flexible hover functionality, you would need to include the Javascript mouse functions in the previous section, and combine them with jQuery.

CSS Opacity 5

The generally preferred way to set opacity is through the use of transparent PNGs. First, create a transparent PNG in the background color of your choice. This can be done through an image editing program such as Photoshop or GIMP. Here is a 2px by 2px, 50% transparent, white PNG.

We apply this transparent PNG like so –

.css-opacity5 {	
	background: url("https://shibashake.com/wordpress-theme/wp-content/uploads/2009/10/overlay-sm.png") top left repeat;  
	border: none; 
}

.css-opacity5:hover {
	background-color:#ffffff;
	background-image:none;
}

This method gives us good results for most browser types including Internet Explorer. Transparency just gets applied to the background, and the text stays sharp and clear.

Screen-shot of opacity results on Internet Explorer 8

The only weakness here, is you must create the transparent PNG beforehand, using your background-color of choice. This may limit the range of transparent background colors you can use during run-time.

However, by using multiple layers, together with a white and black PNG transparent mask, it is possible to simulate different transparent background colors.

Finally, note that IE-6 only supports linked hover (a:hover), so you will need to add that to your hover statements for IE-6 compatibility.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments

  1. Eric says

    January 24, 2013 at 1:20 pm

    is there a plugin that does this?

    Reply
    • ShibaShake says

      January 26, 2013 at 10:26 am

      Not sure – not something that I have looked for.

      Reply
  2. Benxamin says

    December 13, 2009 at 8:22 pm

    Thankfully, they have such a thing as modernizer.js and eCSStender.js that solve CSS3 compatibility problems.

    Reply
  3. ShibaShake says

    November 5, 2009 at 8:48 am

    Hello Aakash,

    Opacity only works for FireFox and other browsers that are CSS compliant. Sadly it does not work for IE.

    IE has its own opacity command –

    filter:alpha(opacity=50);

    Just add those into your CSS definitions. Also your hover should look like this –

    .image:hover
    {
        opacity:1.0;
        filter:alpha(opacity=100);
    }
    

    However, using opacity may get you aliasing issues with IE. I highly recommend using transparent PNGs as is shown in step 5 above. It’s only weakness is that IE-6 does not support repeating transparent PNGs so it will not work there, but it works very well on IE-7 and IE-8.

    As a side note – hover only works on links in IE-6 so you will need to use –

    a:hover .image { blah }
    Reply
  4. aakash says

    November 5, 2009 at 4:00 am

    .image
    {
    background-image: url(anu.png);
    background-repeat: no-repeat;
    height:200px;
    width: 250px;
    position:fixed;
    left: 80%;
    opacity:0.5;

    }
    .image:hover
    {
    opacity:100;

    }

    i followed and the instructions given above and wrote this code but only my image apocity is getting change but no hover effect is taking place. can you plz tell me whats wrong with this….

    thanks

    Reply

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS) (1)
    • Erik
      - Great article. All worked great except for this step:apt install php-mysqlChanging to this fixed it:apt install ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (125)
    • Anthony
      - Where does this code go? Like, what exact .php file please?
  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text (1)
    • tom
      - hi,my experience was like the same, but for me as theme developer the "lazy blocks"(https://wordpress.org/plugins/lazy-blocks/) ...
  • WordPress Custom Taxonomy Input Panels (106)
    • Phil T
      - This is unnecessarily confusing. Why declare a variable with the same name as your taxonomy? Why did you choose a taxonomy ...
  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...

Copyright © 2025 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·