css - 如何仅在左侧和右侧获得框阴影

标签 css

<分区>

我目前在网站上有全局框阴影 -

box-shadow: 0 0 5px rgba(0,0,0,.1);

但是我该如何修改上面的内容,让它只出现在左侧或右侧,或者同时出现在左侧和右侧呢?

最佳答案

请记住,您可以对 spread 使用负值,对 box-shadow 使用多个值。

.shadow {
  width: 30%;
  height: 40px;
  margin: 2rem;
  box-shadow:
  -5px 0px 5px -6px rgba(0,0,0,1),
  5px 0px 5px -6px rgba(0,0,0,1);
}
<div class="shadow"></div>

另一种解决方案可能是使用::before 和::after 以及 filter: blur。这里的好处是你可以变换:旋转阴影,让它看起来像父元素在轻微倾斜。

.shadow {
  position: relative;
  margin: 2rem;
  width: 30%;
  height: 30px;
  background-color: white;
}

.shadow::before,
.shadow::after {
  z-index: -1;
  content: '';
  position: absolute;
  top: 2px;
  bottom: 2px;
  background-color: #000;
  width: 2px;
  filter: blur(2px);
}

.shadow::after {
  right: 0px;
}

.tilting.shadow::before,
.tilting.shadow::after
{
  height: 4px;
  top: initial;
  bottom: 0px;
  width: initial;
}

.tilting.shadow::before {
  left: 0px;
  right: 10px;
  transform: rotate(-3deg);
}

.tilting.shadow::after {
  left: 10px;
  right: 0px;
  transform: rotate(3deg);
}
<div class="shadow"></div>

<div class="tilting shadow"></div>

关于css - 如何仅在左侧和右侧获得框阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59301727/

上一篇:html - 如何使用 HTML 将 CSS 中的 DIV 元素居中?

下一篇:html - 如何制作只影响文本而不影响图像的 CSS 规则?

相关文章:

css - 用类选择器覆盖 h4 的规则

html - 在模态中使用纯 HTML 和 CSS

css - 绝对位置,谁能解释一下

html - 如何让我的输入标签在调整大小时向下移动?

html - 为什么父类覆盖子类

html - 带有 img 元素的 Div 上的 Div

html - 关于 HTML/CSS 文本的问题

html - 位置属性的 CSS 动画不起作用

php - .WOFF字体未链接

javascript - 在动态大小的 DIV 内部垂直对齐 H2