Saturday, August 25, 2012

[TIPS] HTML transparent background

12 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.

12 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. we are provide a better travel sevices to book our travel tour train tickets,air tickets,bus tickets: Railway Station ticket counter or through online train ticket booking with cheap rate price Call @ 9990756667.

    train ticket agents in delhi

    railway ticket agent in delhi

    tatkal railway ticket agent delhi

    online railway ticket booking

    ReplyDelete
  4. Hello guys | 9837940964 | are you looking for fresh and lovely Rozy Call Girls. Delhi Escorts Service | Contact Us Without any hesitation Book here Fresh Call girls who can fulfill your all burning desire. Plz Vzt our site http://shanvibhardwaj.in/gallery.html
    Delhi Escorts

    Delhi Call Girls

    Call Girls in Delhi

    ReplyDelete
  5. http://minakshikumari1.strikingly.com/ Hi Friends I Am ghaziabad call Girl. I am local girl From Ghaziabad | I Provide You Best And Cheap Ghaziabad Call Girls | I am Linked to an Escorts agency in Ghaziabad | These Escort Girls Spend Dazzling Moments With You. Contact Mo. 9899900591
    vaishali escorts

    gaur city escorts

    sahibabad escorts

    kaushambi escorts

    indirapuram escorts

    ahinsa khand escorts

    greater noida escorts

    crossings republik escorts

    raj nagar extension escorts

    ReplyDelete
  6. Trusted and affordable Escort Service in Delhi. " http://sweetgirl.strikingly.com/ "dial 9711199171 to have the best n good looking Call Girls in Delhi. satisfy your every wild desire with our lovely, Rozy and pretty call girls. all these call girls are available in delhi or nearby delhi areas location 24 hours.
    Delhi Escorts

    Russian Escorts Delhi

    Russian Call Girls in Delhi

    Housewife Escorts Delhi

    Delhi independent Escorts

    Female Escorts delhi

    Housewife Escorts Delhi

    Delhi independent Escorts

    Female Escorts delhi

    ReplyDelete
  7. Hello friends, I am aliya Visvas | Visit my Site | http://aliyavishvas.strikingly.com/ | find best faridabad Escorts. Spend some time with me and Forget all worries forever. Call at 9999965857 for more details and fix a lovely meeting with me | and make your every moment unforgettable
    Faridabad escorts

    Vaishali Escorts

    Saket Escorts

    Nehru Place Escorts

    Aerocity Escorts

    Connaught Place Escorts

    Independent Escorts Faridabad

    Faridabad Call Girls

    ReplyDelete
  8. Hi Friends | i am anushka | i live in Mahipalpur | http://anushkaverma.com/ +91-9999965857 - i offer hot Mahipalpur Escorts | Experience a different ways of love and Suck housewife escort service in your nearest location | incall and outcall service available.
    Vasant Vihar Call Girls

    Mayur Vihar Call Girls

    Vasant Kunj Call Girls

    greater kailash Call Girls

    Indirapuram Call Girls

    ReplyDelete
  9. Mumbai Escorts |call @8652582954 Provides You Best Escorts Services



    borivali escorts %%%%%%
    chembur escorts %%%%%%
    churchgate escorts %%%%%%
    colaba escorts %%%%%%
    dadar escorts %%%%%%
    malad escorts %%%%%%

    ReplyDelete