How to Merge to div with each other as shadow effect?

Joined
Nov 15, 2024
Messages
1
Reaction score
0
Hi,
Myself Treek, I'm a web developer, I was working on a website where my client asked me about this special effect in CSS. If any one my friends now about it please give me the source code.
 
Joined
Jul 4, 2023
Messages
481
Reaction score
60
Merge to div with each other as shadow effect
IMO it can be written like this:
HTML:
<div class="container">
  <div class="box"></div>
  <div class="shadow-box"></div>
</div>

<style>
  .container {
    --border-radius: .5rem;
    --box-width: 100px;
    --box-height: 100px;
    position: relative;
    width: 300px;
    height: 150px;
  }
  .box {
    position: absolute;
    width: var(--box-width);
    height: var(--box-height);
    background-color: orangered;
    border-radius: var(--border-radius);
    z-index: 2;
    left: 0;
    top: 0;
  }
  .shadow-box {
    position: absolute;
    width: var(--box-width);
    height: var(--box-height);
    background-color: gray;
    border-radius: var(--border-radius);
    z-index: 1;
    left: .5rem;
    top: .5rem;
    opacity: .85;
  }
</style>
1731659744282.png


but IMO, a more natural and easier to achieve shadow can be achieved by using the css box-shadow property, which adds this effect around the border of an element.
CSS:
box-shadow: [horizontal offset] [vertical offset] [blur radius] [optional spread radius] [color];
  1. The horizontal offset (required) of the shadow, positive means the shadow will be on the right of the box, a negative offset will put the shadow on the left of the box.
  2. The vertical offset (required) of the shadow, a negative one means the box-shadow will be above the box, a positive one means the shadow will be below the box.
  3. The blur radius (required), if set to 0 the shadow will be sharp, the higher the number, the more blurred it will be, and the further out the shadow will extend. For instance a shadow with 5px of horizontal offset that also has a 5px blur radius will be 10px of total shadow.
  4. The spread radius (optional), positive values increase the size of the shadow, negative values decrease the size. Default is 0 (the shadow is same size as blur).
  5. Color (required) – takes any color value, like hex, named, rgba or hsla. If the color value is omitted, box shadows are drawn in the foreground color (text color). But be aware, older WebKit browsers (pre Chrome 20 and Safari 6) ignore the rule when color is omitted.
1731660822298.png


HTML:
<div class="inner-shadow"></div>

<style>
  .inner-shadow {
    background-color: hsl(20, 100%, 85%);
    box-shadow: inset 0 0 10px 10px hsla(20, 50%, 60%, .8);
  }
</style>
1731662261853.png

HTML:
<div class="one-edge-shadow"></div>

<style>
  .one-edge-shadow {
    background-color: hsl(10, 100%, 80%);
    box-shadow: 0 20px 20px -10px hsla(10, 20%, 25%, .8);
  }
</style>
1731663209215.png

HTML:
<div class="one-edge-shadow">hover over me</div>

<style>
  .one-edge-shadow {
    background-color: hsl(10, 100%, 80%);
    box-shadow: none;
    transition: box-shadow 150ms;
  }
  .one-edge-shadow:hover {
    box-shadow: 0 20px 20px -10px hsla(10, 20%, 25%, .8);
  }
</style>

by applying the shadows to pseudo elements which you then manipulate, you can get some pretty fancy 3D looking box shadows:
1731663638744.png


snippet with vendor prefixes giving as deep of browser support as easily possible:
CSS:
  background-color: rgb(68,68,68); /* Needed for IEs */

  -moz-box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6); /* Firefox 3.5 - 3.6 */
  -webkit-box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6); /* Safari 3-4, iOS 4.0.2 - 4.2, Android 2.3+ */
  box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);  /* Opera 10.5, IE 9, Firefox 4+, Chrome 6+, iOS 5 */

  filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius=3,MakeShadow=true,ShadowOpacity=0.30);
  -ms-filter: "progid:DXImageTransform.Microsoft.Blur(PixelRadius=3,MakeShadow=true,ShadowOpacity=0.30)";
  zoom: 1;

BTW, check out also:
- multiple borders
- filter: drop-shadow [ 1 ] [ 2 ]
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top