Code Snippets

  

HTML/CSS Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 148,867 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,218 people online right now. Registration is fast and FREE... Join Now!





Example: Transparent box backgrounds

Ever wonder how you could get partially transparent backgrounds for div boxes with only HTML/CSS? Well here's how.

Submitted By: snoj
Actions:
Rating:
Views: 153,818

Language: HTML/CSS

Last Modified: March 17, 2008
Instructions: I've made some small changes to the code and it now works in both IE and Firefox. Though to get it to work in IE I had to resort to some javascript. Which you can find in the code.

I've also recoded the height and width changing functions into just two instead of the ungodly four.

You can find a live version here (http://josherickson.org/miscfiles/transhtmlcssbackgroundbox.html).

Snippet


  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
  2.         "http://www.w3.org/TR/REC-html40/transitional.dtd">
  3. <title></title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
  5. <style type="text/css">
  6. body {
  7.      background: url(./water_lilies.jpg);
  8.      background-repeat: no-repeat;
  9. }
  10.  
  11. /*This is where the magic happens!*/
  12. div.box {
  13.      /*Any properties you'd want the box to have.*/
  14.      /*Would probably be position, dimension type stuff.*/
  15.      /*Though personally I would have a div outside this
  16.        to control the dimensions.*/
  17.      border: 1px solid #000000;
  18.      position: relative;
  19.      width: 100%;
  20. }
  21. div.box_contents {
  22.      background-color:transparent;
  23.      height: 100%;
  24.      position: relative;
  25.      width: 100%;
  26.      z-index: 101;
  27. }
  28. div.box_background {
  29.      background-color: white;
  30.      height: 100%;
  31.      filter:alpha(opacity=75); /* IE's opacity*/
  32.      left: 0px;
  33.      opacity: 0.75;
  34.      position: absolute;
  35.      top: 0px;
  36.      width: 100%;
  37.      z-index: 99;
  38. }
  39. </style>
  40.  
  41. <script type="text/javascript">
  42. function change_w(id, pixels) {
  43.      var x = document.getElementById(id);
  44.      x.style.width = x.offsetWidth + pixels + "px";
  45. }
  46. function change_h(id, pixels) {
  47.      var x = document.getElementById(id);
  48.      x.style.height = x.offsetHeight + pixels + "px";
  49. }
  50.  
  51. /*This function is meant to be used when you are needing
  52. faux getElementsByName() in IE. IE seems so use the 'id'
  53. attribute instead of 'name' when you use getElementsByName().
  54.  
  55. tag = This tag name that the 'name' attribute you want to
  56.       get is attached to. Like if you called getElementsByTagName().
  57.      
  58. name = The value of the 'name' attribute you want.
  59. */
  60. function getElementsByName_iefix(tag, name) {
  61.      var elem = document.getElementsByTagName(tag);
  62.      var arr = new Array();
  63.      for(i = 0, iarr = 0; i < elem.length; i++) {
  64.           att = elem[i].getAttribute("name");
  65.           if(att == name) {
  66.                arr[iarr] = elem[i];
  67.                iarr++;
  68.           }
  69.      }
  70.      return arr;
  71. }
  72.  
  73. /*This function resets the height and width of the background
  74. to that of it's parent element's height and width.
  75.  
  76. tbg_id = This is the value of the name attribute you named all your
  77.          backgrounds.
  78. */
  79. function transparentbg(tbg_id) {
  80.      var bak = getElementsByName_iefix("div", tbg_id);
  81.      for(i = 0; i < bak.length; i++) {
  82.           bak[i].style.height = bak[i].parentNode.clientHeight + "px";
  83.           bak[i].style.width  = bak[i].parentNode.clientWidth + "px";
  84.      }
  85. }
  86. </script>
  87. </head>
  88. <body onload="transparentbg('bg_name')" onclick="transparentbg('bg_name')">
  89. <div style="width: 500px;">
  90.      
  91.      
  92.      <div class="box" id="box">
  93.           <div class="box_background" name="bg_name"> </div>
  94.           <div class="box_contents">
  95.                Width: <input type="button" value="+" onclick="change_w('box', 30);" /> / <input type="button" value="-" onClick="change_w('box', -30);" /><br />
  96.                Height: <input type="button" value="+" onclick="change_h('box', 30);" /> / <input type="button" value="-" onClick="change_h('box', -30);" /><br />
  97.                <!-- This is a fix that is mainly for IE. Otherwise you'll get white space where you don't want it.-->
  98.                <div style="padding: 2px; font-weight: bolder; font-size: 14px;">
  99.                     This is the box contents! :O <br />
  100.                     You see, the background will cover the whole div block no matter how wide or tall!
  101.                     <br />
  102.                     <br />
  103.                     <br />
  104.                     Look at me! More content!
  105.                </div>
  106.           </div>
  107.      </div>
  108. </div>
  109. </body>
  110. </html>

Copy & Paste


Comments


jkq 2008-04-04 02:41:31

hi there looks good - now can I add this in css for the links..? i.e. having it tranparant for link and visited but having a solid colour for hover and active.. (or reverse..) body { color: #06075a; font-size: 12px; font-family: "Comic Sans MS"; background-color: #06075a; } p { } td { } a:link { color: #fff; font-size: 12px; font-weight: bold; background-color: #06075a; text-decoration: none; } a:visited { color: #fff; font-size: 12px; font-weight: bold; text-decoration: none; } a:hover { color: #06075a; font-size: 12px; font-weight: bold; background-color: #eee; text-decoration: none; } a:active { color: #06075a; font-size: 12px; font-weight: bold; background-color: #eee; text-decoration: none; } .heading { color: #06075a; font-size: 14px; text-decoration: none; } .text10 { font-size: 10px; font-weight: bold; } .boxtext { color: #333; font-weight: bold; background-color: transparent; }


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month