Is it possible for the `drop-shadow` shadow to be applied only to certain elements?

In the top image below, you can see how the drop-shadow effect would be drawn if applied to the top element. I am exploring the possibility of having the shadow cast only on specific objects, as shown in the bottom image.

I am open to unconventional solutions for this issue. However, I would prefer to still use the drop-shadow filter, especially with non-rectangular shapes.

Update: Here is a fiddle link as requested: https://jsfiddle.net/fhrktawm/

Additional Update: I am adding a more complex example to demonstrate my scenario. My aim is to create a sense of depth by adjusting the length of drop shadows.

Answer №1

To achieve this, you can utilize the 4th value of box-shadow or drop-shadow. Simply position the shadow as shown in the provided screenshots.

Since the shadow of a square is always a square, it is essential to place it accurately where the intersecting part is also a square.

.box {
  width: 100px;
  height: 100px;
  background: white;
  border: 1px solid black;
  margin-left: auto;
  margin-right: auto;
}

.box-1 {
  z-index: 10;
  box-shadow: -36px 36px 0 -25px rgba(0, 0, 0, 0.25);
  transform: translate3d(60px, 42px, 0);
}
<div class="box box-1"></div>
<div class="box"></div>

Update

You can use the ::after property to achieve a second effect like this

.box {
  width: 100px;
  height: 100px;
  background: white;
  border: 1px solid black;
  margin-left: auto;
  margin-right: auto;
  transform-style: preserve-3d;
}

.box-1 {
  box-shadow: -36px 36px 0 -25px rgba(0, 0, 0, 0.25);
  transform: translate3d(60px, 42px, 100px);
    z-index: 100;
}

.box-1::after {
  content: "";
  width: 100px;
  height: 100px;
  position: absolute;
  box-shadow: -15px 15px 0 rgba(0, 0, 0, 0.1);
  z-index: -100;
  transform: translateZ(-100px)
}
<div class="box box-1"></div>
<div class="box"></div>

However, due to another box being above (caused by transform-style: preserve-3d;), the box shadow cannot be placed below the box. An alternative method was attempted.

A different approach involves using the box-shadow of another element (the second one) with variations in the shadow as demonstrated in the following snippet:

.box {
  width: 100px;
  height: 100px;
  background: white;
  border: 1px solid black;
  margin-left: auto;
  margin-right: auto;
}

.box-1 {
  z-index: 10;
  box-shadow: -36px 36px 0 -25px rgba(0, 0, 0, 0.25);
  transform: translate3d(60px, 42px, 0);
}

.box-2::after {
  content: "";
  width: 100px;
  height: 100px;
  position: absolute;
  box-shadow: 45px -45px 0 5px rgba(0, 0, 0, 0.1);
  z-index: -1;
}
<div class="box box-1"></div>
<div class="box box-2"></div>

The desired output can be achieved as follows :

.boxContainer {
  border: 1px solid black;
  width: 160px;
  padding: 20px;
}

.box {
  width: 100px;
  height: 100px;
  background: white;
  border: 1px solid black;
  margin-left: auto;
  margin-right: auto;
}

.box-1 {
  z-index: 10;
  box-shadow: -36px 36px 0 -25px rgba(0, 0, 0, 0.25);
  transform: translate3d(60px, 42px, 0);
}

.box-2 {
  box-shadow: -10px 10px 0 rgba(0, 0, 0, 0.25);
}

.box-2::after {
  content: "";
  width: 100px;
  height: 100px;
  position: absolute;
  box-shadow: 45px -45px 0 6px rgba(0, 0, 0, 0.1);
  z-index: -1;
}
<div class="boxContainer">
  <div class="box box-1"></div>
  <div class="box box-2"></div>
</div>

Answer №2

I'm currently exploring the idea of creating a shadow that specifically affects specific objects

In short, after some investigation, it turns out that achieving this effect is not possible with either the box-shadow property or the drop-shadow() filter.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is causing the additional space at the bottom?

Why does my centered black border triangle have an extra bottom margin causing a scroll bar to appear? * { margin: 0; padding: 0; box-sizing: border-box; } body { height: 100vh; width: 100%; background: blue; } .canvas { border: 10px s ...

Guide to aligning a fraction in the center of a percentage on a Materal Design progress bar

Greetings! My objective is to create a material progress bar with the fraction displayed at the top of the percentage. https://i.sstatic.net/GbphJ.png Currently, I have managed to show the fraction at the beginning of the percentage. Below is the code sn ...

Issues arise when attempting to insert data into an HTML file using PHP

Good evening, I am having a problem where when I attempt to use PHP to replace "hello world" in an HTML file, it ends up completely empty each time. Can anyone point out what mistake I might be making? Here are the HTML and PHP files: <!DOCTYPE html&g ...

What is the best way to include a background image in an Angular selector?

I'm having trouble setting a background image for an Angular selector. Here's the code I'm using: <app-highway-card [ngStyle]="{'background-image':'url(https://cdn.pixabay.com/photo/2021/09/04/09/32/road-6597404__340.j ...

I encountered an issue with my h3 element's margin not being applied properly when I attempted to add

I'm facing an issue with the margin for <h3> tag in my HTML code. I want to create some space between the form and the h3 element. Here is the CSS: *{ margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } body{ background-ima ...

What causes the iframe to protrude beyond the bottom of the page when set to 100% height?

I am facing an issue with an iframe on my website that is contained within a div. I have specified both to have a height of 100%, and I am using the blueprint framework, hence applying the class span-12 last. Despite these settings, a scroll bar still appe ...

Strange Behavior of SVG 'fill: url(#....)' in Firefox

I am struggling with an SVG graphic that I have created. Here is the code: <svg width='36' height='30'> <defs> <linearGradient id="normal-gradient" x1="0%" y1="0%" x2="0%" y2="100%"> <stop offset="0%" s ...

The preventDefault() function is not functioning properly on the <a> tag

As a JavaScript beginner, I decided to create an accordion menu using JavaScript. Although I was successful in implementing it, I encountered a bug in my program. In this scenario, uppercase letters represent first-level menus while lowercase letters repr ...

The alignment of flexbox within a list item is not positioned at the top as expected

When I place a flexbox inside a list item, the content is being pushed down by what seems to be a full line height. I have tried various CSS properties like setting margin-top: 0 for the flexbox, margin-top: 0 for its children, adding flex-wrap to the fle ...

Enhancing Material UI icons with a sleek linear gradient

Despite following the instructions in this post Attempting to incorporate a linear gradient into a MaterialUI icon, as per a comment's recommendation, I am unable to get it to work. I experimented with the idea that the icons could be considered text ...

Achieve perfect vertical alignment for a set of three identical boxes using CSS media queries

Bootstrap is being used and the goal is to align three boxes of the same height vertically. https://i.stack.imgur.com/8xg0g.png Currently, the three blocks are not of equal height and do not occupy maximum space. Some manual padding has been added to addr ...

Using Wordpress and JavaScript to dynamically hide a button if a product in the online store does not have an SKU

I'm encountering an issue on my Wordpress site where products with variations are not displaying the inner text on a certain element, despite the fact that the text is present when I inspect the element. Here's the code: const makerBtn = document ...

Visibility of the br tag is not detected

I need to keep one specific div visible on the page while hiding all other content. However, I am facing an issue where the <br> tag inside that div is not showing up. <div id='min320'>min 320px<br>screen width required</div ...

Strategies for avoiding unused style tags in React components

Expanding beyond React, I'm unsure if React itself is the culprit of this issue. In a React environment with TypeScript, I utilize CSS imports in component files to have specific stylesheets for each component. I assumed these styles would only be ad ...

Text encoded in UTF-8 emerges from the surrounding frame

As someone who is relatively new to frontend development, I recently created a blog using a template that utilizes Next.js, Tailwind, and React. However, when I input UTF-8 text in Korean, the text overflows outside of the parent frame on mobile devices. ...

How can I incorporate dynamic moving lines into my website's loading sequence?

Link to Gyazo image 1: https://gyazo.com/015425f0decb187e28b620c1e3206391 Issue with first image: https://gyazo.com/dfcd33e8a4fd5bea64e51fe40556f0bf I'm currently working on a website and came up with a cool concept of having two lines crossing the s ...

Tips for customizing the appearance of a child component using the parent component in Vue.js

I am working on creating a layout using Vuejs and flexbox that includes elements like 'header, sidebar, main-content, sidebar, footer'. I have separated each part of the layout into individual .vue files, such as header.vue and sidebar.vue. In ...

Looking for recommendations on effective jQuery plugins for drag and drop file uploading?

Can anyone recommend any drag-and-drop jQuery plugins that offer similar functionality to Gmail's attachment dragging and dropping feature? I would like the ability to track the upload progress. However, I am specifically looking for options without ...

How can you center the initial line of formatted text while keeping the rest left justified using just CSS?

I need help with formatting existing data without using JavaScript. The goal is to make the first line bold and centered, while the rest of the lines should be justify formatted. However, I'm having trouble achieving this as either the first line gets ...

How can I modify the appearance of a slider's range?

I'm struggling with customizing the style of a price slider input range. No matter what I do, the changes don't seem to take effect. Can anyone pinpoint where I may be going wrong? Appreciate any guidance on this! Thank you! <div class=" ...