Mandarin Logo
MainBlogFAQsBox TipsText TricksColor ChartFree GalleryCSS Opacity
TWO CENTS CURRENT ISSUE 2006 WEST SACRAMENTO CALIFORNIA
Exploring OpacityStep-by-Step
"For People Who Make Mistakes" ...

Part 1 of 3Next CSS Opacity in Mozilla,IE, and Opera
By Michelle Goodrich -- Mandarin Design
(UPDATED MAY2006)

CSS3 Transparency: The Opacity Property

HTML Color Table40% Opacity Table
000000 330000 660000 990000 CC0000 FF0000
003300 333300 663300 993300 CC3300 FF3300
006600 336600 666600 996600 CC6600 FF6600
009900 339900 669900 999900 CC9900 FF9900
00CC00 33CC00 66CC00 99CC00 CCCC00 FFCC00
00FF00 33FF00 66FF00 99FF00 CCFF00 FFFF00
00FF33 33FF33 66FF33 99FF33 CCFF33 FFFF33
00FF66 33FF66 66FF66 99FF66 CCFF66 FFFF66
00FF99 33FF99 66FF99 99FF99 CCFF99 FFFF99
00FFCC 33FFCC 66FFCC 99FFCC CCFFCC FFFFCC
00FFFF 33FFFF 66FFFF 99FFFF CCFFFF FFFFFF
000000 330000 660000 990000 CC0000 FF0000
003300 333300 663300 993300 CC3300 FF3300
006600 336600 666600 996600 CC6600 FF6600
009900 339900 669900 999900 CC9900 FF9900
00CC00 33CC00 66CC00 99CC00 CCCC00 FFCC00
00FF00 33FF00 66FF00 99FF00 CCFF00 FFFF00
00FF33 33FF33 66FF33 99FF33 CCFF33 FFFF33
00FF66 33FF66 66FF66 99FF66 CCFF66 FFFF66
00FF99 33FF99 66FF99 99FF99 CCFF99 FFFF99
00FFCC 33FFCC 66FFCC 99FFCC CCFFCC FFFFCC
00FFFF 33FFFF 66FFFF 99FFFF CCFFFF FFFFFF
W3C SAYS
Opacity can be thought of conceptually as a postprocessing operation. Conceptually, after the element (including its children) is rendered into an RGBA offscreen image, the opacity setting specifies how to blend the offscreen rendering into the current composite rendering.

Name: 	        opacity
Value: 	        <alphavalue> | inherit
Initial: 	1
Applies to: 	all elements
Inherited: 	no
Percentages: 	N/A
Media: 	        visual
Computed value: The same as the specified 
value after clipping the <alphavalue> 
to the range [0.0,1.0].

<alphavalue>
Syntactically a <number>. The uniform opacity setting to be applied across an entire object. Any values outside the range 0.0 (fully transparent) to 1.0 (fully opaque) will be clamped to this range. If the object is a container element, then the effect is as if the contents of the container element were blended against the current background using a mask where the value of each pixel of the mask is <alphavalue>.

HTML and CSS Opacity 1.0

decorative image
Opacity 1.0

How much of the image do we want to see? All of it? That's opacity:1.0; Without any filters applied the opacity of an image is 1.0 (fully opaque). Just the words opacity and transparency are confusing. They are opposite ends of the spectrum as someone at Digg commented 10% opacity = 90% transparency. That's it in a nutshell. Opacity is a little ditty that goes something like this.

Opacity 25, 50, 75, and 100

Opacity .25 Opacity .50 Opacity .75 Opacity 1.0

For CSS opacity to work in the browser it must have some placement specified. We used float:left; and that worked. Now we are trying position:relative; and that works too, but only in Mozilla Firefox. The float:left CSS style works in both Internet Explorer and Mozilla Firefox. Surely the explanation from W3C makes sense but basically this was trial and error, mostly error to get the transparency opacity to apply to text. The opacity code for simply displaying an image is more straightforward.

Copy and paste this in your Blog or Web page.

It will look like this.

opacity 25 opacity 50 opacity 75 opacity 1.05

This example uses Mozilla proprietary code in conjunction with IE code. CSS3 is not fully defined as of today (August 2004). If you remove the -moz-opacity it will look like this.

opacity 25 opacity 50 opacity 75 opacity 1.0

Update May 2006
The abobe opacity examples look the same in this Firefox browser. The -moz-opacity is no longer needed. We used it in earlier versions of Mozilla and left it in for possible backward compatibility issues. Opacity works fine in our Mozilla Firefox browser with or without the proprietary code. Opacity also works in current versions of Opera.

Opacity with onmouseover and onmouseout

Hover here. The Filter Opacity works in Mozilla and IE. It also works in current versions of Opera.

The CSS for transOFF. transON, trans25, and trans75 looks like this.

Copy and paste this in your Blog or Web page.

It will look like this.

Hover here. The Filter Opacity works in Mozilla and IE. It also works in current versions of Opera.

Start with the style defined as transON for the oposite effect.

Hover here. The Filter Opacity works in Mozilla and IE. It also works in current versions of Opera.

Image Hover

Apply the CSS defined opacity styles trans25,trans75, transON, and transOFF, to an image. Hover to see the opacity effect.

trans25
transON
trans75

Text and Image Hover

Apply the CSS defined opacity styles trans25,trans75, transON, and transOFF, to an image and text. Hover to see the opacity effect.

Hover here. The Filter Opacity works in Mozilla and IE. It also works in current versions of Opera.This is opacity 25%

Hover here. The Filter Opacity works in Mozilla and IE. It also works in current versions of Opera.This is opacity 50%

Hover here. The Filter Opacity works in Mozilla and IE. It also works in current versions of Opera.This is opacity 75%

Aligned Images Hover


Opacity works in Mozilla and IE. It also works in current versions of Opera. The w3.org CSS3 specification for opacity opacity:.50; is included in the style we created along with filter: alpha(opacity=50); for IE and -moz-opacity: 0.5; for Mozilla. Note that -moz-opacity: 0.5; isn't needed in current versions of Mozilla Firefox.

Hover over the hyperlinked image.

CSS
Copy and paste this in your style sheet.

HTML
Copy and paste this in your Blog or Web page.

It will look like this.

CSS Opacity Hover, No Scripting

Opacity Over Easy

This is the easy way to hover with opacity. The opacity is all in CSS. The onmouseover isn't needed and there is only one image.

CSS

HTML
Copy and paste this in your Blog or Web page.

It will look like this.

Transparent Opacity Applied to Text

If the object is a container element, then the effect is as if the contents of the container element were blended against the current background using a mask where the value of each pixel of the mask is <alphavalue>.

Text Opacity

This black text has 50% opacity applied

Copy and paste this in your Blog or Web page.

It will look like this.

This black text has 50% opacity applied

Notice that we added width:100%. That's because opacity only works with placement or layout specified (in IE). A width and height, a float, or something that allows the browser to do positional processing will work.

CSS Opacity Background Defined

Black and White

THE BACKGROUND IS WHITE
The background is an important element when using opacity. If we don't use a background color (we used white) then the text edges may be jagged. Setting the background color to the keyword transparent doesn't always work.

...this will look like some of the pullquotes or subtitles we see in magazinelayout...

DIV
The text to the right is a div defined with text color black and a background color of white. The opacity on the div is 50. The width is defined as 150px.

SPAN
The span inside of the div is defined with an opacity of 25. The edges should not be jagged. The padding of 10 pixels on the span is simply so that we don't lose the bottom of the g or the y in the text magazinelayout.

TEXT SPILL
The text will spill outside of the defined width of 150px when forced, and naturally the browsers don't handle the spill exactly the same. We don't mind the spill and will continue here by running the words magazinelayout together to explore the spill effect.

Copy and paste this in your Blog or Web page.

It will look like this.

...this will look like some of the pullquotes or subtitles we see in magazinelayout...

DIV
The text to the right is a div defined as black with a background color of white. The opacity on the div is 50.

SPAN
The span inside of the div is defined with an opacity of 25. The edges should not be jagged. The padding of 10 pixels on the span is simply so that we don't lose the bottom of the text magazinelayout.

Applying Opacity to a Paragraph

Maroon and White

P
Here we are using the Paragraph <p> tag instead of the <div>. This is the same code as above. We replaced the the div with p and changed the color black to maroon.

...this will look like some of the pullquotes or subtitles we see in magazinelayout... this is 150 pixels wide

This is basically the same code as above but using the paragraph tag instead of the div. It may be easier to understand for those are not intimate with the intricacies and idiosyncrasies of CSS.

We put a dotted border around this paragraph and another border around the pullquote area defined as 150px.

The defined width of the pullquote transparency is 150px. But we intentionally ran the words magazinelayout together to show that the text will not necessarily remain confined to the defined 150 pixels.

The this is 150 pixels wide inside of the right side paragraph displays an image that is exactly 150 pixels wide. We can see where our paragraph would end if we left out the large transparent joined words magazinelayout....

Opacity in CSS Style Sheet

So how do we define the opacity pullquote in our style sheet?

CSS

...this is the style defined as p.pulltrans in the external style sheet. Our style sheet is here and may change over time..............

P.pulltrans {
color:black;
width:150px;
background:white;
filter:alpha(opacity=50);
-moz-opacity:.50;
opacity:.50;
float:right;
margin-top:10px;
margin-bottom:10px;
margin-left:10px;
font-size: 28px;
line-height:26px;  
text-align: right;
}

This is a simple flexible CSS style defined as p.pulltrans in the external style sheet. The external style sheet has to load each time a page loads so we do have to decide if we will use it often enough to be defined in our style sheet.

The copy-and-paste code for the transparent pullquote is easy and there isn't any overhead. We will likely be using Inline Style.

For this pullquote example we add the <small> tag, a <b> tag, and just for grins add the style="color:navy" to the paragraph tag to override the predefined black.

HTML
The code we type in our page looks like this.

CSS Transparency and Opacity Made Simple

Do you really need to know what happens behind the curtain? It depends. If you are developing then this information may be of some use. If you are a writer or a casual coder then you really don't have to know all of the details.

There isn't anyone who understands all of the rules and the actions of the various flavored browsers. For the most part we are exploring opacity not as a design tool but more as a toy. A plaything. Something that browsers can do but we can't. Yet.

The difficulty in making opacity look simple is fully understanding what is going on, understanding how each browser renders the image and the text. There are dotted borders on some of the code to help identify exactly what each browser is doing.

Now we can jump in head first and go through a few examples that we know work.

CSS Opacity Applied to Text Heading

Your Page Title

If your page has a background image you can apply opacity to the heading text. You don't need to alter the image.

Method 1

Method 1: Inline Style with heading on transparent background
We did it with inline style here but you can put it in your CSS style sheet if you want to reuse the code. The preferred method is Method 2 if you are applying opacity over an image.

Copy and paste this in your Blog or Web page (jagged edge).

It will look like this (in your browser).

Your Page Title

Do you see the jagged edge? There is a jagged edge in some browsers, including IE. It looks like this.

To remove the jagged edge in IE use a background color other than transparent. We are using the color white.

Copy and paste this in your Blog or Web page (remove the jagged edge).

It will look like this.

Your Page Title

Method 2

Method 2: Inline Style on Image Background
We did it with inline style here but you can put it in your style sheet if you want to reuse the code. Notice that we defined the background for our text as an image. It is the same image that is our background image. Here's the code for inline style.

Copy and paste this in your Blog or Web page.

The heading looks like this over our background image. http://www.mandarindesign.com/images/monday.jpg

Your Page Title

Method 3

Method 3: Style Sheet
You can define the H1 or other heading tag in your style sheet. The code goes in your external style sheet or between the <HEAD> and </HEAD> tags in your template.

CSS
h1  {
color:white;
width:100%;
background:transparent;
filter:alpha(opacity=50);
-moz-opacity:.50;
opacity:.50;
font-size:42px;
line-height:2em;
text-align:center;
}

The code to type in your Blog or Web page will look like this.

HTML
<h1>YOUR PAGE TITLE</h1> 

We used an opacity equivalent of 50% and an arbitrary font-size. Notice that the width is defined. The browser wants to know the layout (height and width) when applying opacity. The color white works well with most backgrounds.

Tips

  • Use Inline Style or define the H1 tag in the style sheet (you probably don't want to do both).
  • Review the HEAD section of your template. If the H1 tag is already defined use another heading tag for this example: h2, h3, h4, h5, or h6. Or, remove and replace the H1 that is already defined.

Jagged Text when using Opacity?
We used background:transparent; in the example. That will not work for everyone. On some background images we have to explicitly say that the background is an image. Otherwise the text is jagged.

Solution
Change background:transparent; to the name of the background image.


Background Image Opaque

You can apply opacity to all or part of the background image.

this text is in the second div with bold and plain text over the image with an opaque background

There are two div tags in action here. The first div is 400px wide and contains a background image that is 200 x 159. It is a simple div, used as a container for the rest of the code. The goal was to start the text below the image on the left. The image ends in the 159 position.

The second div is inside of the first div. For simplicity it is the same width as the image. Again, for simplicity we set the margin-top in the paragraph to 159px. The only thing magic about 159 is that it is the height of the image. That's all.

The second div is the workhorse. It's applying the opacity and floating the text. To force the appearance that we want we're defining the height on the right side opacity filtered text as 275px.

Copy and paste this in your page.

It will look like this.

this text is in the second div with bold and plain text over the image with an opaque background

There are two div tags in action here. The first div is 400px wide and contains a background image that is 200 x 159. It is a simple div, used as a container for the rest of the code.

The second div is inside of the first div. For simplicity it is the same width as the image. Again, for simplicity we set the margin-top in the paragraph to 159px. The only thing magic about 159 is that it is the height of the image. That's all.

The second div is the workhorse. It's applying the opacity and floating the text. To force the appearance that we want we're defining the height on the right side opacity filtered text as 275px. The goal was to start the text below the image on the left. The image ends in the 159 position.

Opacity Explained

DIV 400px Wide
The first div, where we put the image as a background, is defined as 400 pixels wide. Why? We just doubled the width of the image to make it simple. The width and the height are required for Mozilla (we haven't applied the opacity yet).

This is what the first div does. It is the container for the background image.
Background Image: 200 x 159
The image is 200 pixels wide.
The right side text is 200 pixels wide.
200 + 200 = 400

The image is automatically repeated with the repeat-x. If we use no-repeat it will look like this.

Same div with no-repeat

You can see that the first div simply contains the background and defines the width and height that we want.

DIV
The second div is the workhorse. It's applying the opacity and floating the text. To force the appearance that we want we're defining the height on the right side opacity filtered text as 275px.

Copy and paste this in your Blog or Web page.

It will look like this.

it's Wednesday now but why waste a perfectly good image... let's break out of the box and play with code...

Now that we have a spare image we can explore opacity filters with a background image. There are two divs in action here. The first div contains a background image that is 200 x 159. It is a simple div, used as a container for the rest of the code. The second div is inside of the first div. For simplicity it is the same width as the image.

The second div is the workhorse. It's applying the opacity and floating the text. To force the appearance that we want we're defining the height on the right side opacity filtered text as 275px.

The dotted borders are added so that we can see what the code is doing. Changing the color pink to transparent creates yet another effect. Opacity and transparency are fun to play with.

Background no-repeat

Change repeat-x to no-repeat for this effect.

it's Wednesday now but why waste a perfectly good image... let's break out of the box and play with code...

Now that we have a spare image we can explore opacity filters with a background image. There are two divs in action here. The first div contains a background image that is 200 x 159. It is a simple div, used as a container for the rest of the code. The second div is inside of the first div. For simplicity it is the same width as the image.

The second div is the workhorse. It's applying the opacity and floating the text. To force the appearance that we want we're defining the height on the right side opacity filtered text as 275px.

The dotted borders are added so that we can see what the code is doing. Changing the color pink to transparent creates yet another effect. Opacity and transparency are fun to play with.


Remove the Testing Borders

Copy and paste this in your Blog or Web page.

It will look like this.

it's Wednesday now but why waste a perfectly good image... let's break out of the box and play with code...

Now that we have a spare image we can explore opacity filters with a background image. There are two divs in action here. The first div contains a background image that is 200 x 159. It is a simple div, used as a container for the rest of the code. The second div is inside of the first div. For simplicity it is the same width as the image.

The second div is the workhorse. It's applying the opacity and floating the text. To force the appearance that we want we're defining the height on the right side opacity filtered text as 275px.

For some reason Mozilla Firefox is demanding that this have a border. Otherwise, it misplaces the paragraph text and lays it over the left side image. A cool effect, but not what we want. The border on this one is 1px solid white.

More about Opacity

There's more about opacity in the Text Tricks page and in the Mandarin Blog where we explore IE, Mozilla, and Opera opacity over images.

Opacity Color Tones

The Color Palette Picker creates ten tones from a single color. The opacity filter we are using in this post does the same thing but we don't know the color code without sampling the color. This is a quick cheat to the color codes. It creates 10 shades of the base color, at varying degrees of opacity. Included is opacity over a white background, opacity over black background and opacity over the color of your choice.

The Opacity Color Chart lets you see opacity in live color.

Opacity Resources

This page has a good opacity post that works in IE only. The same code can be modified to work in Mozilla by adding the Mozilla proprietary opacity code.

Opacity Tutorial Series

Part 1 of 3Next

Part 1: CSS Opacity in Mozilla and IE (this page)

  1. Introduction to CSS3 Transparency
    1. HTML and CSS Opacity 1.0
    2. Opacity 25,50,75, and 100
    3. CSS Image Opacity Code
  2. Transparent Opacity Applied to Text
    1. CSS Opacity Background Defined
      1. Black and White
    2. Applying Opacity to a Paragraph
      1. Maroon and White
    3. Opacity in CSS Style Sheet
  3. CSS Transparency and Opacity Made Simple
    1. Example: CSS Opacity Applied to Text Heading
      1. Method 1: Inline Style (transparent background)
      2. Method 2: Inline Style (Image background)
      3. Method 3: Style Sheet
    2. Example: Background Image Opaque
      1. Opacity Explained
      2. Background no-repeat
    3. Example: CSS Onmouseover Hover Opacity
    4. Example: CSS Opacity Hover, No Scripting
    5. Example: Opacity with onmouseover and onmouseout
    6. More About Opacity
    7. Opacity Color Tones
    8. Opacity Resources
    Part 2: Opacity Background Blending Effects
    Part 3: CSS Opacity Color Tips and Tricks
last updated April 2005
MAIN BLOG FAQs CSS:Boxes TRICKS About CSS Style Color Chart GALLERY PureText
Validate HTML Validate Style Sheet Elements of Style Yahoo Buzz Google Zeitgeist Lycos 50 TP TP2 TF Technorati Sitemeter
Mandarin Archives 2005: J F M A M J J A S O N D   2004: J F M A M J J A S O N D   2003: J F M A M J J A S O N D   2002: F M A M J J A S O N D
Clean PC Banners Free Banners Tag Board Gator ColdFusion Fade Images Refer Grouping Web Help Drop Caps
This work is licensed under a Creative Commons License