Transparency in CSS

Problem

On a web page an HTML element is placed on another and the first one wish to look and feel transparent.

Solution

The CSS has a declaration called opacity.

Example

We have an example HTML page with solid background color and a button placed on that.

HTML

<html DOCTYPE!>
 <head>
     <style>
         #main {
             background-color: yellow;
             height: 100%;
         }
     </style>
 </head>
 <body>
     <div id="main">
         <input type="button" id="btnTransparent" value="Test"/>
     </div>
 </body>
 </html>

The code looks like this:

Now, let’s make the button transparent. We will select it by it’s id: btnTransparent and add some transparency.

#btnTransparent {
    opacity: .5;
}

Put the code together:

<html DOCTYPE!>
 <head>
     <style>
         #main {
             background-color: yellow;
             height: 100%;
         }
         #btnTransparent {
             opacity: .5;
         }
     </style>
 </head>
 <body>
     <div id="main">
         <input type="button" id="btnTransparent" value="Test"/>
     </div>
 </body>
 </html>

It looks like this:

The Test button appears half transparent as set by opacity: .5; It accepts a value from 0 to 1 meaning 0 is full transparent, not visible.

Let’s now add a pseudo element on the button, when the mouse hovers the button, the opacity returns to full.

This would do the trick

#btnTransparent:hover {
 opacity: 1;
}

And the complete code:

<html DOCTYPE!>
 <head>
     <style>
         #main {
             background-color: yellow;
             height: 100%;
         }
         #btnTransparent {
             opacity: .5;
         }
          #btnTransparent:hover{
             opacity: 1;  
         }
      </style>
 </head>

 <body>
     <div id="main">
         <input type="button" id="btnTransparent" value="Test"/>
     </div>
 </body>
 </html>

Result

Now, let’s put it all together with a nice background image as follows:

<html DOCTYPE!>
 <head>
     <style>
         * {
            margin: 0;
            padding: 0;
         }
         #title_area{
             position: absolute;
             top: 200px;
             left: 50px;
 
             width: 350px;
             height: 100px;
 
             display: flex;
             justify-content: center;
             align-items: center; 
             flex-direction: column; 
 
             background-color: sandybrown;
             opacity: .5;
         }
      </style>
 </head>
 <body>
    <div id="title_area"><h3>
        <h3>Transparent or not transparent<h3/>
    </div>
    <img src="background.jpg" width="500" height="600"/>
 </body>
 </html>
Result

Background image taken from here

Conclusion

Pretty neat. A simple CSS declaration changes the look and feel of the UI.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.