iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 15, 2008
Reader Question - Would you host your client's work on your website?
About
 
May 15, 2008
How to Create an Ajax Autocomplete Text Field: Part 6
WebReference.com
 
May 14, 2008
Poll: Are the browser safe colors still needed?
About
 
May 14, 2008
Google Doctype launched
About
 
May 14, 2008
Web Editor Reviews - 6 New Reviews
About
 
May 14, 2008
Build Beautiful Buttons in Photoshop, Part I
SitePoint
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /CSS

Create a box with a drop shadow using CSS 

  Views:    10208
  Votes:    2
by Dan Bailey 12/04/03 Rating: 

Synopsis:

A really simple way to add a drop shadow effect to a box using nothing more than Cascading Style Sheets.
Pages: 
The Article

Firstly, lets create our box. We'll make a div and give it the imaginative name "box".

<div class="box">This is a box</div>

Now we need to give it some style using CSS. In your stylesheet add the following:

.box {
  width:150px;
  height:150px;
  background:#f60;
  }

If you view your page now then you should have a 150 pixel by 150 pixel orange square.

Now we need to add another div which will serve as the drop shadow. Put a second box around the orange one by changing your HTML to the following:

<div class="box-outer">
  <div class="box">This is a box with a drop shadow!</div>
</div>

In your stylesheet add the following:

.box-outer {
  width:150px;
  height:150px;
  background:#333;
  }

This gives the outer box exactly the same shape and size as the orange one, but with a different color.

If you view your page now, you'll see that the outer box doesn't appear. This is because the orange one is inside it, and as the size is the same it fills the box-outer div entirely.

What we need to do is move the outer div slightly to create the effect of a drop shadow. In your CSS change the orange box style to:

.box {
  position:relative;
  top:-3px;
  left:-3px;
  width:150px;
  height:150px;
  background:#f60;
  }
 
This tells the browser to position it relative to the div that contains it (box-outer), and move it -3 pixels to the top and -3 pixels to the left.

Take a look at it now, and you'll see that you have an orange box with a drop shadow to the left and bottom of it.

You can change any of the style rules to however you please, you could even get adventurous and use background images to create some great effects!

Pages: 

Similar/related articles:


 
  Sponsors