You are here: Reference > JavaScript > client-side > style handling > properties > opacity

opacity style property

Browser support:
9
Sets or retrieves the transparency level of an element. For Internet Explorer, use filter with 'Alpha(opacity=percent)'.
Note: Internet Explorer supports the opacity property from version 9.
In older Internet Explorer versions, use the filter.alpha property.
The opacity property works for all elements, as opposed to Internet Explorer's filter.alpha property that only works for absolute positioned elements.
The example below shows a cross-browser solution for creating the opacity effect from CSS.

Syntax:

object.opacity;
You can find the related objects in the Supported by objects section below.
This property is read/write.
CSS page for this property: opacity

Possible values:

The type of this property is string.
 One of the following values: 
inherit
Takes the value of this property from the computed style of the parent element.
opacity (floating-point [0-1])
Floating-point number [0-1] .
Default: 1.

Example HTML code 1:

This example illustrates the use of the opacity property:
<head>
    <style>
        .opacity {
            opacity: 0.5;
        }

        .IEopacity {
            filter:alpha(opacity=50);
            position:absolute;
            left:30px;
            top:100px;
        }
    </style>
</head>
<body>
    <a class="opacity IEopacity">This text has an opacity of 0.5</a>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the opacity property in JavaScript:
<head>
    <script type="text/javascript">
        function ChangeFloatEdge (selectTag) {
            // Returns the index of the selected option
            var whichSelected = selectTag.selectedIndex;

            // Returns the text of the selected option
            var opacity = selectTag.options[whichSelected].text;

            var anchor = document.getElementById ("myAnchor");
            if ('opacity' in anchor.style) {
                anchor.style.opacity = opacity;
            } else {
                alert ("Your browser doesn't support this example!");
            }
        }
    </script>
</head>
<body>
    <a id="myAnchor">Change this element's opacity!</a>

    <select onchange="ChangeFloatEdge (this);" size="7">
        <option />0
        <option />0.1
        <option />0.3
        <option />0.5
        <option />0.7
        <option />0.9
        <option selected="selected" />1
    </select>
</body>
Did you find this example helpful? yes no

Example HTML code 3:

This example shows a cross-browser opacity manipulation solution in JavaScript:
<head>
    <style>
        #myAnchor {
            position:absolute;
            left:60px;
            top:70px;
        }
    </style>
    <script type="text/javascript">
        function ChangeFloatEdge (selectTag) {
            // Returns the index of the selected option
            var whichSelected = selectTag.selectedIndex;

            // Returns the text of the selected option
            var opacity = selectTag.options[whichSelected].text;

            var anchor = document.getElementById ("myAnchor");
            if ('opacity' in anchor.style) {
                anchor.style.opacity = opacity;
            } else {
                anchor.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
            }
        }
    </script>
</head>
<body>
    <a id="myAnchor">Change this element opacity!</a>

    <select onchange="ChangeFloatEdge (this);" size="7">
        <option />0
        <option />0.1
        <option />0.3
        <option />0.5
        <option />0.7
        <option />0.9
        <option selected="selected" />1
    </select>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content