
- CSS - Home
- CSS - Roadmap
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Types
- CSS - Measurement Units
- CSS - Selectors
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS - Cascading
- CSS - Universal Selectors
- CSS - ID Selectors
- CSS - Group Selectors
- CSS - Class Selectors
- CSS - Child Selectors
- CSS - Element Selectors
- CSS - Descendant Selectors
- CSS - General Sibling Selectors
- CSS - Adjacent Sibling Selectors
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Advanced Features
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS Interview Questions
- CSS Online Quiz
- CSS Online Test
- CSS Mock Test
- CSS - Quick Guide
- CSS - Cheatsheet
- CSS - Properties References
- CSS - Functions References
- CSS - Color References
- CSS - Web Browser References
- CSS - Web Safe Fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - opacity Property
The opacity Property
CSS opacity property controls the transparency of an element. It determines how much of a hidden element's content is visible. The property can be used on various elements, whether they contain text, images, or serve as backgrounds.
CSS opacity Example
Here is an example of the opacity property. You can change the slider and see the change in opacity and the value of opacity.
Syntax
The syntax for the CSS opacity property is as follows:
opacity: number | percentage | initial | inherit;
Property Values
Value | Description |
---|---|
number | It specifies the opacity level of elements using numeric values between 0 (fully transparent) and 1 (Visible). |
percentage | It specifies the opacity level of elements using percent values between 0% (fully transparent) and 100% (Visible). |
initial | It sets the property to its default value. |
inherit | It inherits the property from the parent element. |
CSS opacity Property with Numeric Values
To set the transparency level of an element, we can specify numeric values between 0 and 1 including them with the opacity property.
Example
In this example, we have used numeric values to specify the opacity level of div elements.
<!DOCTYPE html> <html> <head> <style> .props { height: 50px; padding: 20px; text-align: center; font-weight: bold; font-size: 20px; background-color: lightgreen; } .first { opacity: 0.3; } .second { opacity: 0.6; } .third { opacity: 1; } </style> </head> <body> <h2> CSS opacity property </h2> <h4> opacity: 0.3 </h4> <div class="props first"> This div has opacity: 0.3 </div> <h4> opacity: 0.6 </h4> <div class="props second"> This div has opacity: 0.6 </div> <h4> opacity: 1 </h4> <div class="props third"> This div has opacity: 1 </div> </body> </html>
CSS opacity Property with Percentage Values
To set the transparency level of an element, we can specify percentage values between 0% and 100% including them with the opacity property.
Example
In this example, we have used the percent value to specify the opacity level of div elements.
<!DOCTYPE html> <html> <head> <style> .props { height: 50px; padding: 20px; text-align: center; font-weight: bold; font-size: 20px; background-color: lightgreen; } .first { opacity: 30%; } .second { opacity: 60%; } .third { opacity: 100%; } </style> </head> <body> <h2> CSS opacity property </h2> <h4> opacity: 30% </h4> <div class="props first"> This div has opacity: 30% </div> <h4> opacity: 60% </h4> <div class="props second"> This div has opacity: 60% </div> <h4> opacity: 100% </h4> <div class="props third"> This div has opacity: 100% </div> </body> </html>
CSS opacity for Images
You can use the opacity property to set the transparency level of images. Setting the transparency level of an image can help in creating an overlay.
Example
In this example, we have used the opacity property to set the transparency level of the logo at different levels.
<!DOCTYPE html> <html> <head> <style> .props { width: 300px; height: 150px; } .first-img { opacity: 0.1; } .second-img { opacity: 50%; } .third-img { opacity: 1; } </style> </head> <body> <h2> CSS opacity property </h2> <h4> opacity: 0.1 </h4> <img class="first-img props" src="/https/www.tutorialspoint.com/css/images/logo.png" alt="Tutorials point"> <h4> opacity: 50% </h4> <img class="second-img props" src="/https/www.tutorialspoint.com/css/images/logo.png" alt="Tutorials point"> <h4> opacity: 1 </h4> <img class="third-img props" src="/https/www.tutorialspoint.com/css/images/logo.png" alt="Tutorials point"> </body> </html>
CSS opacity Property with RGBA Color Values
The opacity property applies transparency to an element and its child elements. To avoid this and still achieve transparency, we can use the rgba() values. These values specify colors along with opacity only to the desired element.
Example
In this example, we have used the opacity and rgba() functions to highlight the difference between these two.
<!DOCTYPE html> <html> <head> <style> div { width: 200px; padding: 10px; text-align: center; } .first-color { background-color: rgba(77, 77, 255); } .decimal-opacity1 { opacity: 0.1; } .decimal-opacity2 { opacity: 0.3; } .decimal-opacity3 { opacity: 0.6; } .decimal-opacity4 { opacity: 0.9; } .rgba-opacity1 { background-color: rgba(77, 77, 255, 0.1); } .rgba-opacity2 { background-color: rgba(77, 77, 255, 0.3); } .rgba-opacity3 { background-color: rgba(77, 77, 255, 0.6); } .rgba-opacity4 { background-color: rgba(77, 77, 255, 0.9); } .container { font-weight: bolder; margin-left: 50px; float: left; } </style> </head> <body> <h2> CSS opacity property </h2> <p> It is clear from the <strong> transparent element </strong> portion below, that the opacity property is applied to all other elements present within the element that uses opacity property. </p> <p> To prevent this, we can make use of the <strong> rgba values </strong> which define the color along with opacity. The opacity is applied only to the particular element and not to the elements present within it. </p> <div class="container"> <h4> Transparent element </h4> <div class=" first-color decimal-opacity1"> CSS Opacity 0.1 </div> <div class="first-color decimal-opacity2"> CSS Opacity 0.3 </div> <div class="first-color decimal-opacity3"> CSS Opacity 0.6 </div> <div class="first-color decimal-opacity4"> CSS Opacity 0.9 </div> </div> <div class="container"> <h4> With RGBA color values </h4> <div class="rgba-opacity1"> CSS Opacity 10% </div> <div class="rgba-opacity2"> CSS Opacity 30% </div> <div class="rgba-opacity3"> CSS Opacity 60% </div> <div class="rgba-opacity4"> CSS Opacity 90% </div> </div> </body> </html>
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
opacity | 4.0 | 9.0 | 2.0 | 3.1 | 9.0 |