添加 box-shadow 到 :after 伪元素
Adding box-shadow to a :after pseudo element
我有一个名为 .testimonial-inner
的 div 并使用 :after
伪元素我有一个位于其下方的箭头指向下方。我遇到的问题是给它添加一个 box-shadow,这样它们看起来就像一个自然元素。
三角形上没有box-shadow
:
body {
background: #eee
}
.testimonial-inner {
background: #fff;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
padding: 30px;
display: block;
margin-bottom: 25px;
position: relative;
-webkit-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
-moz-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
.testimonial-inner:after {
top: 100%;
left: 48px;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(255, 255, 255, 0);
border-top-color: #fff;
border-width: 18px;
margin-left: -18px;
}
<div class="c-4 testimonial-wrap">
<div class="testimonial-inner">
<p>Using Facebook was unquestionably the best decision I could possibly have made at the point in my journalistic journey. It enabled me to share my fears, frustrations as well as successes.</p>
</div>
</div>
请注意,框阴影目前并未环绕箭头。
当我将它添加到 :after
声明时,我得到以下结果:
body {
background: #eee
}
.testimonial-inner {
background: #fff;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
padding: 30px;
display: block;
margin-bottom: 25px;
position: relative;
-webkit-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
-moz-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
.testimonial-inner:after {
top: 100%;
left: 48px;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(255, 255, 255, 0);
border-top-color: #fff;
border-width: 18px;
margin-left: -18px;
-webkit-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
-moz-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
<div class="c-4 testimonial-wrap">
<div class="testimonial-inner">
<p>Using Facebook was unquestionably the best decision I could possibly have made at the point in my journalistic journey. It enabled me to share my fears, frustrations as well as successes.</p>
</div>
</div>
您可以添加另一个 :pseudo-element,将其旋转 45deg
并向其添加 box-shadow
。
body {
background: #eee
}
.testimonial-inner {
background: #fff;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
padding: 30px;
display: block;
margin-bottom: 25px;
position: relative;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
.testimonial-inner:after {
top: 100%;
left: 48px;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(255, 255, 255, 0);
border-top-color: #fff;
border-width: 18px;
margin-left: -18px;
}
.testimonial-inner:before {
content: '';
position: absolute;
transform: rotate(45deg);
width: 36px;
height: 36px;
bottom: -12px;
z-index: -1;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
<div class="c-4 testimonial-wrap">
<div class="testimonial-inner">
<p>Using Facebook was unquestionably the best decision I could possibly have made at the point in my journalistic journey. It enabled me to share my fears, frustrations as well as successes.</p>
</div>
</div>
另一种使用 svg
作为三角形的方法。
body {
background: #eee
}
.testimonial-wrap {
position: relative;
}
.testimonial-inner {
background: #fff;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
padding: 30px;
display: block;
margin-bottom: 25px;
position: relative;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
#triangle {
position: absolute;
top: 100%;
margin-top: -1px;
left: 50px;
}
<div class="c-4 testimonial-wrap">
<div class="testimonial-inner">
<p>Using Facebook was unquestionably the best decision I could possibly have made at the point in my journalistic journey. It enabled me to share my fears, frustrations as well as successes.</p>
</div>
<svg id="triangle" width="40" height="26">
<defs>
<filter id="f" width="150%" height="130%">
<feGaussianBlur in="SourceAlpha" stdDeviation="2.5" />
<feComponentTransfer>
<feFuncA type="linear" slope="0.8" />
</feComponentTransfer>
<feMerge>
<feMergeNode/>
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</defs>
<path filter="url(#f)" d="M0,0 h40 l-20,20z" fill="white" />
</svg>
</div>
你不能在这里使用 box-shadow 做你想做的事。这是因为 "arrow" 效果是通过在除顶部以外的所有地方使用透明颜色创建的。这意味着该元素仍然是一个正方形,您的阴影将相应地围绕它呈现。
如果您想为图像的形状添加阴影,请尝试使用 SVG,或者只使用带有预渲染阴影的图像。
<polygon points="220, 150 350, 220" style="fill:#FFFFFF; stroke:#000000;stroke-width:1"/>
恕我直言,我认为这有点老套,但是使用纯 css 来做到这一点:
div{
height:200px;
width:100%;
border-radius:10px;
background:gray;
position:relative;
box-shadow:0 0px 10px black;
border:1px solid black;
}
div:before{
position:absolute;
bottom:-10px;
left:40px;
content:"";
background:gray;
height:20px;
width:20px;
transform: rotate(45deg);
border-bottom:1px solid black;
border-right:1px solid black;
box-shadow:0 0px 10px black;
}
div:after{
position:absolute;
bottom:0px;
left:30px;
content:"";
background:gray;
height:20px;
width:40px;
}
<div>test</div>
过滤器将起作用:
.shadowed {
-webkit-filter: drop-shadow(0px 2px 2px rgba(130,130,130,1));
filter : drop-shadow(0px 2px 2px rgba(130,130,130,1));
-ms-filter : "progid:DXImageTransform.Microsoft.Dropshadow(OffX=0, OffY=2, Color='#444')";
filter : "progid:DXImageTransform.Microsoft.Dropshadow(OffX=0, OffY=2, Color='#444')";
}
我有一个名为 .testimonial-inner
的 div 并使用 :after
伪元素我有一个位于其下方的箭头指向下方。我遇到的问题是给它添加一个 box-shadow,这样它们看起来就像一个自然元素。
三角形上没有box-shadow
:
body {
background: #eee
}
.testimonial-inner {
background: #fff;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
padding: 30px;
display: block;
margin-bottom: 25px;
position: relative;
-webkit-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
-moz-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
.testimonial-inner:after {
top: 100%;
left: 48px;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(255, 255, 255, 0);
border-top-color: #fff;
border-width: 18px;
margin-left: -18px;
}
<div class="c-4 testimonial-wrap">
<div class="testimonial-inner">
<p>Using Facebook was unquestionably the best decision I could possibly have made at the point in my journalistic journey. It enabled me to share my fears, frustrations as well as successes.</p>
</div>
</div>
请注意,框阴影目前并未环绕箭头。
当我将它添加到 :after
声明时,我得到以下结果:
body {
background: #eee
}
.testimonial-inner {
background: #fff;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
padding: 30px;
display: block;
margin-bottom: 25px;
position: relative;
-webkit-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
-moz-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
.testimonial-inner:after {
top: 100%;
left: 48px;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(255, 255, 255, 0);
border-top-color: #fff;
border-width: 18px;
margin-left: -18px;
-webkit-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
-moz-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.25);
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
<div class="c-4 testimonial-wrap">
<div class="testimonial-inner">
<p>Using Facebook was unquestionably the best decision I could possibly have made at the point in my journalistic journey. It enabled me to share my fears, frustrations as well as successes.</p>
</div>
</div>
您可以添加另一个 :pseudo-element,将其旋转 45deg
并向其添加 box-shadow
。
body {
background: #eee
}
.testimonial-inner {
background: #fff;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
padding: 30px;
display: block;
margin-bottom: 25px;
position: relative;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
.testimonial-inner:after {
top: 100%;
left: 48px;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(255, 255, 255, 0);
border-top-color: #fff;
border-width: 18px;
margin-left: -18px;
}
.testimonial-inner:before {
content: '';
position: absolute;
transform: rotate(45deg);
width: 36px;
height: 36px;
bottom: -12px;
z-index: -1;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
<div class="c-4 testimonial-wrap">
<div class="testimonial-inner">
<p>Using Facebook was unquestionably the best decision I could possibly have made at the point in my journalistic journey. It enabled me to share my fears, frustrations as well as successes.</p>
</div>
</div>
另一种使用 svg
作为三角形的方法。
body {
background: #eee
}
.testimonial-wrap {
position: relative;
}
.testimonial-inner {
background: #fff;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
padding: 30px;
display: block;
margin-bottom: 25px;
position: relative;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75);
}
#triangle {
position: absolute;
top: 100%;
margin-top: -1px;
left: 50px;
}
<div class="c-4 testimonial-wrap">
<div class="testimonial-inner">
<p>Using Facebook was unquestionably the best decision I could possibly have made at the point in my journalistic journey. It enabled me to share my fears, frustrations as well as successes.</p>
</div>
<svg id="triangle" width="40" height="26">
<defs>
<filter id="f" width="150%" height="130%">
<feGaussianBlur in="SourceAlpha" stdDeviation="2.5" />
<feComponentTransfer>
<feFuncA type="linear" slope="0.8" />
</feComponentTransfer>
<feMerge>
<feMergeNode/>
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</defs>
<path filter="url(#f)" d="M0,0 h40 l-20,20z" fill="white" />
</svg>
</div>
你不能在这里使用 box-shadow 做你想做的事。这是因为 "arrow" 效果是通过在除顶部以外的所有地方使用透明颜色创建的。这意味着该元素仍然是一个正方形,您的阴影将相应地围绕它呈现。
如果您想为图像的形状添加阴影,请尝试使用 SVG,或者只使用带有预渲染阴影的图像。
<polygon points="220, 150 350, 220" style="fill:#FFFFFF; stroke:#000000;stroke-width:1"/>
恕我直言,我认为这有点老套,但是使用纯 css 来做到这一点:
div{
height:200px;
width:100%;
border-radius:10px;
background:gray;
position:relative;
box-shadow:0 0px 10px black;
border:1px solid black;
}
div:before{
position:absolute;
bottom:-10px;
left:40px;
content:"";
background:gray;
height:20px;
width:20px;
transform: rotate(45deg);
border-bottom:1px solid black;
border-right:1px solid black;
box-shadow:0 0px 10px black;
}
div:after{
position:absolute;
bottom:0px;
left:30px;
content:"";
background:gray;
height:20px;
width:40px;
}
<div>test</div>
过滤器将起作用:
.shadowed {
-webkit-filter: drop-shadow(0px 2px 2px rgba(130,130,130,1));
filter : drop-shadow(0px 2px 2px rgba(130,130,130,1));
-ms-filter : "progid:DXImageTransform.Microsoft.Dropshadow(OffX=0, OffY=2, Color='#444')";
filter : "progid:DXImageTransform.Microsoft.Dropshadow(OffX=0, OffY=2, Color='#444')";
}