Boxes with Shadows

Posted by RobElliott on 30 Jun 2009 17:39, last edited by GoVegan on 09 May 2010 05:33

: boxes css shadows

rating: +12

What we are trying to achieve

Following on from the how-to on boxes and tables with rounded corners, the release this week of Firefox 3.5 has meant that we are now able to have boxes with shadows like this:

Simple box with shadow



Adding a blur to the shadow



Spreading the shadow round the box




Adding multiple effects




Creating a "negative" shadow



Creating an "inset" shadow

How to do this

CSS

The box-shadow has a number of values. You don't need to use all of them. In order they are:

1. horizontal length of the shadow
2. vertical length of the shadow
3. the blur radius of the shadow
4. the spread radius of the shadow
5. the colour of the shadow

By using these values in different ways a number of effects can be implemented.

simple shadow -moz-box-shadow: 3px 3px gray;
add a blur -moz-box-shadow: 3px 3px 5px gray;
spread the shadow round the whole box -moz-box-shadow: 0 0 5px 5px aqua;
multiple shadows -moz-box-shadow: 0 0 20px black, 20px 15px 30px yellow, -20px 15px 30px lime, -20px -15px 30px blue, 20px -15px 30px red;
negative shadows -moz-box-shadow: 0 5px 5px -3px black;
inset shadows -moz-box-shadow: inset 1px 1px 10px #888;

Note that the inset shadow requires the word inset to be used as the first value.

In your custom CSS add selectors, properties and values. I have called the selectors .shbox- followed by the type of shadow. In the example below I have given each box the same basic values and just changed the shadow.

.shbox-simple, .shbox-blur, .shbox-spread, .shbox-multiple, .shbox-negative, .shbox-inset {
 background: #DDDDDD;
 border: 1px solid #627369;
 text-align: center;
 color: #468259;
 width: 20%;
 margin-left: 5%;
 -moz-border-radius:25px;
}
.shbox-simple {
 -moz-box-shadow: 3px 3px gray;
 -webkit-box-shadow: 3px 3px gray;
}
.shbox-blur {
 -moz-box-shadow: 3px 3px 5px gray;
 -webkit-box-shadow: 3px 3px 5px gray;
}
.shbox-spread {
 -moz-box-shadow: 0 0 5px 5px aqua;
 -webkit-box-shadow: 0 0 5px 5px aqua;
}
.shbox-multiple {
 -moz-box-shadow: 0 0 20px black, 20px 15px 30px yellow, -20px 15px 30px lime, -20px -15px 30px blue, 20px -15px 30px red;
 -webkit-box-shadow: 0 0 20px black, 20px 15px 30px yellow, -20px 15px 30px lime, -20px -15px 30px blue, 20px -15px 30px red;
}
.shbox-negative {
 -moz-box-shadow: 0 5px 5px -3px black;
 -webkit-box-shadow: 0 5px 5px -3px black;
}
.shbox-inset {
 -moz-box-shadow: inset 1px 1px 10px #888;
 -webkit-box-shadow: inset 1px 1px 10px #888;
}

Wiki syntax

To use this on your page, just add a div using the relevant selector, for example:

[[div class="shbox-multiple"]]
 text inside the div will use the multiple box shadow
[[/div]]


Text inside the div will use the multiple box shadow



It would be possible to specify the box shadow values in your div using style =. This is useful for occasional use or where the box will differ from the default you have set in your CSS.

Browser Support

This requires Firefox 3.5+ or Safari 3.0+.

It will not work yet in Opera, Internet Explorer or Chrome. So for those of you looking at this page with either of those browsers the image below shows the effect:

boxshadows.png

Comment

  • These box shadows are a good way of creating interesting-looking buttons, particularly if you combine the box shadow with the rounded corners.
  • You do not have to have just text inside the div but can have, for example, a table or an image:
aleksandr_sm.jpg

Author

RobElliott RobElliott . Please visit his/her userPage.

Rate this solution

If you think this solution is useful, please increase the rating.

rating: +12

I don't see the effects and I use FF
Brunhilda Brunhilda 21 Aug 2009 09:31

???

I only see the effects in the photo….


If slaughterhouses had glass walls, everyone would be vegan. - Paul McCartney

by Brunhilda Brunhilda , 21 Aug 2009 09:31
Re: I don't see the effects and I use FF
RobElliott RobElliott 21 Aug 2009 09:59

Which version of FF are you running? It needs 3.5 or higher.

Rob


Rob Elliott - Strathpeffer, Scotland - Wikidot first line support & community admin team.

by RobElliott RobElliott , 21 Aug 2009 09:59
Re: I don't see the effects and I use FF
Brunhilda Brunhilda 21 Aug 2009 10:25

Sorry, Rob. I thought it updates automatically. It seems that I had 3.0. Now I downloaded 3.5 and I see it.


If slaughterhouses had glass walls, everyone would be vegan. - Paul McCartney

by Brunhilda Brunhilda , 21 Aug 2009 10:25
Adjustment questions
jjs jjs 31 Dec 2009 12:00

Hi Rob!

What if I don't want the text to be centered? I removed the "text-align: center;" and it did the trick but now the text starts right from the left border. How could I get a little margin there? I played with "margin:??px;", but it moved the whole box instead of the text.

Btw, wouldn't "width: auto;" work better than "width: 20%;"?


___TTT___/ http://www.trumpetexercises.net
(_|||_) \ - Janne

by jjs jjs , 31 Dec 2009 12:00
Re: Adjustment questions
RobElliott RobElliott 31 Dec 2009 16:22

It's not margin you want here, it's padding. So add padding-left:5px and adjust to taste.

wouldn't "width: auto;" work better than "width: 20%;"

It depends how wide you want the box. Width:auto will made it as wide as its containing box which I definitely didn't want so the width:20% was deliberate. There's no rule that says you have to use the properties and values I used in the example.


Rob Elliott - Strathpeffer, Scotland - Wikidot first line support & community admin team.

by RobElliott RobElliott , 31 Dec 2009 16:22
Re: Adjustment questions
jjs jjs 04 Jan 2010 05:39

Width:auto will made it as wide as its containing box which I definitely didn't want so the width:20% was deliberate.

Ok, I got its function wrong then. I thought that it would adjust to the text width.


___TTT___/ http://www.trumpetexercises.net
(_|||_) \ - Janne

by jjs jjs , 04 Jan 2010 05:39

Related articles

Comments

I don't see the effects and I use FF
Brunhilda Brunhilda 21 Aug 2009 09:31

???

I only see the effects in the photo….


If slaughterhouses had glass walls, everyone would be vegan. - Paul McCartney

by Brunhilda Brunhilda , 21 Aug 2009 09:31
Re: I don't see the effects and I use FF
RobElliott RobElliott 21 Aug 2009 09:59

Which version of FF are you running? It needs 3.5 or higher.

Rob


Rob Elliott - Strathpeffer, Scotland - Wikidot first line support & community admin team.

by RobElliott RobElliott , 21 Aug 2009 09:59
Re: I don't see the effects and I use FF
Brunhilda Brunhilda 21 Aug 2009 10:25

Sorry, Rob. I thought it updates automatically. It seems that I had 3.0. Now I downloaded 3.5 and I see it.


If slaughterhouses had glass walls, everyone would be vegan. - Paul McCartney

by Brunhilda Brunhilda , 21 Aug 2009 10:25
Adjustment questions
jjs jjs 31 Dec 2009 12:00

Hi Rob!

What if I don't want the text to be centered? I removed the "text-align: center;" and it did the trick but now the text starts right from the left border. How could I get a little margin there? I played with "margin:??px;", but it moved the whole box instead of the text.

Btw, wouldn't "width: auto;" work better than "width: 20%;"?


___TTT___/ http://www.trumpetexercises.net
(_|||_) \ - Janne

by jjs jjs , 31 Dec 2009 12:00
Re: Adjustment questions
RobElliott RobElliott 31 Dec 2009 16:22

It's not margin you want here, it's padding. So add padding-left:5px and adjust to taste.

wouldn't "width: auto;" work better than "width: 20%;"

It depends how wide you want the box. Width:auto will made it as wide as its containing box which I definitely didn't want so the width:20% was deliberate. There's no rule that says you have to use the properties and values I used in the example.


Rob Elliott - Strathpeffer, Scotland - Wikidot first line support & community admin team.

by RobElliott RobElliott , 31 Dec 2009 16:22
Re: Adjustment questions
jjs jjs 04 Jan 2010 05:39

Width:auto will made it as wide as its containing box which I definitely didn't want so the width:20% was deliberate.

Ok, I got its function wrong then. I thought that it would adjust to the text width.


___TTT___/ http://www.trumpetexercises.net
(_|||_) \ - Janne

by jjs jjs , 04 Jan 2010 05:39
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
General Wikidot.com documentation and help section.
Wikidot.com Terms of Service - what you can, what you should not etc.
Wikidot.com Privacy Policy.

AltStyle によって変換されたページ (->オリジナル) /