【问题标题】:How to Add Box Shadow Effect To Pseudo After Content如何在内容后向伪添加框阴影效果
【发布时间】:2018-01-27 10:18:14
【问题描述】:

您能否看一下这个演示并告诉我如何将 Box-shadow 添加到 Pseudo After Content 中?如您所见,我尝试添加喜欢

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

但它不是添加阴影而是创建一个盒子

body {
  padding: 50px;
  background: #eee;
 }

.hero {
  position: relative;
  background-color: #fff;
  height: 320px !important;
  width: 100% !important;
  -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
  box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
}

.hero:after {
  content: '';
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin: 0 auto;
  width: 0;
  height: 0;
  border-top: solid 40px #fff;
  border-left: solid 40px transparent;
  border-right: solid 40px transparent;
  -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
  box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
}
<div class="hero"></div>

【问题讨论】:

  • 这是预期的行为,盒子阴影是矩形的。你可以做的是画一个正方形,然后旋转它。

标签: css


【解决方案1】:

我认为本准则会对您有所帮助。

我添加了属性:transform-originbox-sizing

供参考: transform-originbox-sizing

body {
  background-color: #888;
}

.hero {
  position: relative;
  margin: 3em;
  padding: 1em;
  box-sizing: border-box;
  background: #bada55;
  box-shadow: 0px 3px 3px 0 rgba(0, 0, 0, 0.4);
}

.hero::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  margin-left: -0.5em;
  bottom: -2em;
  left: 50%;
  box-sizing: border-box;
  border: 1em solid black;
  border-color: transparent transparent #bada55 #bada55;
  transform-origin: 0 0;
  transform: rotate(-45deg);
  box-shadow: -3px 3px 3px 0 rgba(0, 0, 0, 0.4);
}
<div class="hero"></div>

【讨论】:

  • 感谢您的支持 :) 。我将添加所需的解释。
【解决方案2】:

删除所有阴影并添加具有属性filter: drop-shadow(0 5px 10px rgba(0, 0, 0, .2)); 的容器,该容器按形状创建阴影。

body {
  padding: 50px;
  background: #eee;
 }
 
.container {
  filter: drop-shadow(0 5px 10px rgba(0, 0, 0, .2));
}

.hero {
  position: relative;
  background-color: #fff;
  height: 320px !important;
  width: 100% !important;
}

.hero:after {
  content: '';
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin: 0 auto;
  width: 0;
  filter: drop-shadow;
  height: 0;
  border-top: solid 40px #fff;
  border-left: solid 40px transparent;
  border-right: solid 40px transparent;
}
<div class="container">
<div class="hero"></div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 2015-01-09
    相关资源
    最近更新 更多