如何在内容后添加框阴影效果到伪

时间:2018-01-27 10:18:14

标签: css css3

你能看一下这个演示,让我知道如何将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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

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

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

供参考: transform-originbox-sizing

&#13;
&#13;
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);
}
&#13;
<div class="hero"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

删除了所有阴影并添加了具有属性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>