Home arrow Style Sheets arrow Page 4 - Using PNG Images to Build Background Effects
STYLE SHEETS

Using PNG Images to Build Background Effects


Cascading style sheets are great for building all sorts of eye-catching background effects for web pages. CSS allows the utilization of JPG, PNG and GIF images, which can be easily repeated along the X and Y coordinates. This facilitates the creation of professional-looking web documents by using only a few tiny background graphics that can be downloaded quickly by way of a browser.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 9
May 13, 2008
TABLE OF CONTENTS:
  1. · Using PNG Images to Build Background Effects
  2. · Building background gradients using a simple JPG image
  3. · Building a basic background gradient using a GIF image
  4. · Building a fancy web page background with a PNG graphic: introduction

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using PNG Images to Build Background Effects - Building a fancy web page background with a PNG graphic: introduction
(Page 4 of 4 )

In accordance with the concepts I deployed in the prior section, it’s perfectly feasible to build fancy web page backgrounds by utilizing a single, partially-transparent PNG image along with a solid color. However, to achieve the correct color fading effect, a browser must natively support alpha-based transparency with PNG graphics, which, as I stated earlier, is not a strong point of Internet Explorer 6 and below.

So I’m going to show you how to build the same gray-colored web page background that you saw in the previous section, but in this particular case, by using a partially-transparent PNG graphic. Of course, you should be aware that this approach will work only in most non IE-based browsers.

Having said that, here’s the corresponding definition of the sample (X)HTML file that displays the aforementioned web page background:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Sample web page with PNG-based background (Does not work in IE)</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">

body{

padding: 0;

margin: 0;

background: #fff url(bg.png) left top repeat-x;

}

h1{

padding: 5px;

margin: 0;

font: bold 12px Arial, Helvetica, sans-serif;

color: #000;

}

h2{

padding: 7px 5px 8px 5px;

margin: 0;

background: #fc0;

font: bold 12px Arial, Helvetica, sans-serif;

color: #000;

}

p{

font: normal 12px Arial, Helvetica, sans-serif;

color: #000;

}

#header{

width: 780px;

padding: 10px;

margin-left: auto;

margin-right: auto;

background: #fc0;

}

#navbar{

width: 780px;

padding: 10px;

margin-left: auto;

margin-right: auto;

background: #ffc;

}

#navbar ul{

list-style: none;

}

#navbar li{

display: inline;

padding-right: 4%;

}

#navbar a:link,#navbar a:visited{

font: normal 12px Arial, Helvetica, sans-serif;

color: #039;

text-decoration: none;

}

#navbar a:hover{

text-decoration: underline;

}

#mainwrapper{

clear: both;

width: 800px;

height: 100%;

margin-left: auto;

margin-right: auto;

overflow: hidden;

background: #fff;

}

#mainwrapper .leftcol{

position: relative;

float: left;

}

#mainwrapper .rightcol{

position: relative;

float: right;

}

#leftbar{

width: 180px;

padding: 10px;

}

#centerbar{

float: left;

width: 380px;

padding: 10px;

background: #fff;

}

#rightbar{

width: 180px;

padding: 10px;

}

#footer{

clear: both;

width: 780px;

padding: 10px;

margin-left: auto;

margin-right: auto;

background: #fc0;

}

.contbox{

padding: 10px;

margin-bottom: 10px;

background: #eee;

border: 1px solid #ccc;

}

</style>

</head>

<body>

<div id="header">

<h1>This is the header section of the web page</h1>

<p>Contents for header section go here. Contents for header section go here. Contents for header section go here. Contents for header section go here.</p>

</div>

<div id="navbar">

<h1>This is the navigation bar of the web page</h1>

<ul>

<li><a href="#">Link 1</a></li>

<li><a href="#">Link 2</a></li>

<li><a href="#">Link 3</a></li>

<li><a href="#">Link 4</a></li>

<li><a href="#">Link 5</a></li>

<li><a href="#">Link 6</a></li>

</ul>

</div>

<div id="mainwrapper">

<div id="leftbar" class="leftcol">

<h2>Section title</h2>

<div class="contbox">

<p>Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here.</p>

</div>

<h2>Section title</h2>

<div class="contbox">

<p>Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here.</p>

</div>

</div>

<div id="centerbar" class="leftcol">

<h2>Section title</h2>

<div class="contbox">

<p>Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here.</p>

<p>Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here.</p>

<p>Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here. Contents for center column go here.</p>

</div>

</div>

<div id="rightbar" class="rightcol">

<h2>Section title</h2>

<div class="contbox">

<p>Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here.</p>

</div>

<h2>Section title</h2>

<div class="contbox">

<p>Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here. Contents for left column go here.</p>

</div>

</div>

</div>

<div id="footer">

<h1>This is the footer section of the web page</h1>

<p>Contents for footer section go here. Contents for footer section go here. Contents for footer section go here. Contents for footer section go here. Contents for footer section go here.</p>

</div>

</body>

</html>


As you can see in the above example, the pertinent web page background is built by utilizing a tiny PNG image that fades progressively to transparency. In this way, it blends with the solid white color specified for the “body” selector.

Finally, to complement the previous hands-on example, below I included an image that shows the visual appearance of this PNG-based web page background. Here it is:



Indeed, this is a technique that permits you to create fancy web page gradients very easily, even though it’s not going to work with IE-based browsers. It was originally introduced by Mathew O’ Neil in the following article: http://www.alistapart.com/articles/supereasyblendys. If you’re interested in learning a bit more about this approach, it’s really worth reading.

Final thoughts

In this first chapter of the series, you learned a few straightforward approaches for building fancy web page gradients using partially-transparent PNG images. However, as I explained earlier, IE 6 and below do not display graphics that use alpha-based transparency.

Thus, considering this, in the next part, I’m going to implement a simple CSS workaround in order to develop a cross-browser solution that allows you to build consistent web page gradients using transparent PNG graphics.

Don’t miss the next article!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
STYLE SHEETS ARTICLES

- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...
- CSS3 Animated Image Gallery: Transitions
- CSS3 Properties: Fixed Heights with box-sizi...
- CSS3 Properties: Altering Strokes and 3D Eff...
- CSS3 Properties: Text-Stroke
- CSS3 Transitions: Width and Height Properties
- Creating a Drop Down Menu in CSS3
- Intro to CSS Transitions

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials