Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One pixel shadow on three sides using CSS3 box-shadow

Tags:

css

I need to create a one pixel shadow on three sides of an element using box shadow. I'm using the following code, except it's creating a two pixel border but I only need one.

   -moz-box-shadow: 0 1px 1px   #c00
-webkit-box-shadow: 0 0   1px 0 #c00
        box-shadow: 0 0   1px 0 #c00
like image 447
Cofey Avatar asked Mar 18 '11 14:03

Cofey


People also ask

How do you shadow a box with 3 sides?

The trick is to give the element with box-shadow and its previous sibling positioning, then give the previous sibling a background color and set it to have a higher z-index so that it's stacked on top of the element with box-shadow , in effect covering its top shadow.

How do you put a shadow on one side of a box?

To set a box-shadow on one side of the element, use the box-shadow property. This property has four length parameters and a color. box-shadow: h-offset v-offset blur spread color; h-offset sets the shadow horizontally.


2 Answers

Try 3 shadows, no blur. http://jsfiddle.net/leaverou/8tgAp/1/

body {
  width: 300px;
  height: 200px;
  margin: 20px auto;

  -moz-box-shadow: 0 1px 0 #c00, 1px 1px 0 #c00, -1px 1px 0 #c00;
  -webkit-box-shadow: 0 1px 0 #c00, 1px 1px 0 #c00, -1px 1px 0 #c00;
  box-shadow: 0 1px 0 #c00, 1px 1px 0 #c00, -1px 1px 0 #c00;
}
like image 74
Lea Verou Avatar answered Oct 19 '22 13:10

Lea Verou


Using the normal border declaration is the way to go, but if—for whatever reason—you're unable to use border, then you can hide one side of the shadow with the :before or :after pseudo-selector.

Example:

body {background-color: #000; color: #fff}

.module {
    height: 100px;
    width: 100px;
    background-color: #000;
    -moz-box-shadow: 0 0 2px #f00;
    -webkit-box-shadow: 0 0 2px #f00;
    box-shadow: 0 0 2px #f00;
}

.module:before {
  content: '';
  border-top: solid #000 1px;
  display: block;
  position: relative;
  top: -1px;
}

You can see it in action here: http://jsfiddle.net/3nspR/

like image 34
methodofaction Avatar answered Oct 19 '22 13:10

methodofaction