Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Rotated drop shadow on right side of box

Tags:

html

css

shadow

I have a fixed, 100% height menu on the left and I need to create a shadow effect on its right side that would disappear after while.

See the figure that illustrates this.

enter image description here

How to create this effect?

Here is a fiddle: http://jsfiddle.net/52VtD/7787/

HTML:

<nav id="main-menu">
  <h1>Hello</h1>
  <a href="#">A</a>
  <a href="#">B</a>
  <a href="#">C</a>
  <a href="#">D</a>
</nav>

CSS:

#main-menu {
    width: 320px;
    height: 100%;
    top: 0;
    z-index: 1000;
    position: fixed;
    background-color: #b4bac1;
}
like image 281
Very Curious Avatar asked Dec 25 '22 04:12

Very Curious


2 Answers

You can achieve this with CSS3: box-shadow and transform.

In the example below the box-shadow is applied to a pseudo element of .menuContainer which sits underneath the .menu element and is rotated by using CSS3s rotate() transform property.

html,body {  /* This is only here to allow the menu to stretch to 100% */
    height: 100%;
    margin: 0;
}
.menuContainer {
    position: relative;    
    height: 100%;
    width: 100px;
}
.menuContainer::after {
    content: "";
    position: absolute;   
    z-index: 1; 
    top: -10px;
    bottom: 0;
    left: -7px;
    background: rgba(0,0,0,0.3);
    box-shadow: 10px 0 10px 0 rgba(0,0,0,0.3);
    width: 100px;    
    transform: rotate(1deg);
}
.menu {
    background: #f00;
    height: 100%;
    width: 100px;
    position: relative;
    z-index: 2;
}
<div class="menuContainer">
    <div class="menu"></div>
</div>

JSFiddle

like image 134
Chris Spittles Avatar answered Dec 28 '22 07:12

Chris Spittles


You could fake it with a pseudo-element rather than using a box-shadow as follows

JSfiddle Demo

CSS

#main-menu {
    width: 50px;
    height: 100%;
    top: 0;
    z-index: 1000;
    position: fixed;
    background-color: pink;
}

#main-menu:after {
    content:"";
    position: absolute;
    top:0;
    left:100%;
    height:100%;
    width:5%;
    background: linear-gradient(to bottom, rgba(0,0,0,0.15) 0%,rgba(0,0,0,0) 100%);
}
like image 34
Paulie_D Avatar answered Dec 28 '22 08:12

Paulie_D