Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CSS div transparent background with shadow

I have this simple code, it makes the div to be transparent

<!DOCTYPE html>
<html>
  <body style="background-color: aqua">
    <div style="background-color: rgb(255,255,255,0); border: solid 3px #000; width: 50px; height: 50px; box-shadow: 5px 5px 0 rgb(0, 0, 255);">
      <div style="color: #fff; text-align: center; margin-top: 15px;">
        Hello
      </div>
    </div>    
  </body>
</html>

but what I need to achieve is to make the div transparent but the shadow to appear entire, that is to say this

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any way to achieve this only using a single div and not dying in the try?

>Solution :

You may use a pseudo element and offset it .

idea below

body {
background:aqua;
}

div {
box-sizing:border-box;
margin: 2em;
position:relative;
aspect-ratio:1/1;
height:100px;
display:grid;
align-items:center;
text-align:center;
background:rgb(0, 0, 255);
padding:5px  10px 10px 5px;/* tune it to your needs */
color:white;
}
div:before {
content:'';
position:absolute;
border:solid 6px black;
inset: -16px 10px 10px -16px;/* tune it to your needs */
}
<div>
        Hello
      </div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading