Saturday, August 25, 2012

[TIPS] HTML transparent background

18 comments
If you are a webdesigner there is a big chance you have encountered this problem before: transparent backgrounds. In this post I will describe three methods to create transparent backgrounds using images, HTML or simply by using CSS.


Let's start with this example: you have a div with the id #example and you want to add a transparent blue background behind the text.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a dolor dui. Sed arcu sapien, molestie quis euismod et, varius a ipsum.

This method is the oldest one, it works in all browsers but has some drawbacks: you have to use an image, thus forcing the client to make one more request to the server to get it and also, to change background's color you have to create another image.

First you have to create a 1px*1px image using an image editor (Adobe Photoshop, GIMP, etc...) and save it in the appropriate format (.png or .gif).
Now we only have to add that image as a background to our div using css
#example{
 background-image: url('images/bg.png');
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a dolor dui. Sed arcu sapien, molestie quis euismod et, varius a ipsum.

Creating a new div as a background is a method also used when you want to have full control over background's size, opacity and position. Add a new div having the class background inside your #example div before the content and style it as shown below. We need a new div because background-opacity rule does not exist cross-browser.
#example{
position:relative;
z-index:1; /* We also add a z-index to the div. Because z-indexes are relative this will make the {-1 indexed element} to appear on top of the wrap div (the div having the the space-background) */
}
#example .background{
background:#008fbb;
position:absolute;
width:100%;
height:100%;
opacity:0.5;
top:0px;
left:0px;
filter:alpha(opacity=50); /* For IE */
z-index:-1; /* Without it the background would appear over the p*/
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a dolor dui. Sed arcu sapien, molestie quis euismod et, varius a ipsum.

Same as before but this time we keep our HTML file clean by using CSS :before pseudo element to add an empty div inside our #example div, just before it's content.
#example{
position:relative;
z-index:1;
}
#example:before{ 
/* :before pseudo-element adds a new div having the styles declared below */
content: '';
background:#008fbb;
position:absolute;
width:100%;
height:100%;
opacity:0.5;
top:0px;
left:0px;
filter:alpha(opacity=50); /* For IE */
z-index:-1;
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a dolor dui. Sed arcu sapien, molestie quis euismod et, varius a ipsum.


This is the most straightforward method: use a color that also has trasnparency (alpha channel) as the background. The only drawback is that IE8 and under does not support RGBa.
#example{
background: rgb(0,30,200); /* Fallback for IE6-8 */
background: rgba(0,30,200,0.5);
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a dolor dui. Sed arcu sapien, molestie quis euismod et, varius a ipsum.

18 comments:

  1. Thanks for informative guide. These tips will be helpful for all graphic designers.
    I will mention this post on my blog soon.

    ReplyDelete
  2. Excellent tutorial thanks for share this valuable post. it will increase my designing knowledge.

    ReplyDelete
  3. Escort Service In Aerocity and book a Aerocity Independent escorts like as Indian and Russian EscortsHere you will get great and high class escort for service in escort service in Aerocity

    saket escorts $$
    mahipalpur escorts $$
    lajpat nagar escorts $$
    nehru place escorts $$
    connaught place escorts $$
    vasant kunj escorts $$

    ReplyDelete
  4. Elite Paharganj Escorts BookMe 09999965857 Paharganj Call Girls Service

    paharganj escorts $$$$
    rajouri garden escorts $$$$
    karol bagh escorts $$$$
    saket escorts $$$$
    rohini escorts $$$$
    udaipur escorts $$$$

    ReplyDelete
  5. Delhi Escorts Service available for bookings 9711199171 , Fulfill your sexual dream with Independent Call Girls Delhi, You can Hire Models vila Call Girls in Delhi for hotels and beaches. Fulfill your wish and find Independent Call Girls in Delhi.

    malviya nagar escorts $$$
    rohini escorts $$$
    greater kailash escorts $$$
    mehrauli escorts $$$
    vikaspuri escorts $$$
    kapashera escorts $$$

    ReplyDelete
  6. Enjoy Vip Model Girls - Delhi Escorts Directly to your room. We provides Hot service to our clients. independent Delhi Escort available 24*7 working in Escort in Delhi cities.

    saket escorts $$$
    karolbagh escorts $$$
    dwarka escorts $$$
    nehru place escorts $$$
    mahipalpur escorts $$$

    ReplyDelete
  7. We are providing packer and movers in chandigarh. We are provide services like door to door services, local shifting services, packers and movers services, transportation services at affordable price. To know more information contact us – 9001694151 .

    packer and movers in chandigarh $$$$
    packer and movers chandigarh $$$$
    movers and packers chandigarh $$$$
    movers and packers in chandigarh $$$$
    http://www.chennaicargopackers.com/about-us.html $$$$
    http://www.chennaicargopackers.com/household-relocation-services.html $$$$
    http://www.chennaicargopackers.com/local-shifting-delhi.html $$$$

    ReplyDelete