在 CSS 中为使用边框制作的自定义形状添加阴影
Add shadow to custom shape made with border in CSS
好的,所以在 HTML 中我有一个带有前后伪元素的 div 标签。这是我的 CSS:
.divClass{
background: #41423D;
height:30px;
}
.divClass:before{
content: '';
line-height: 0;
font-size: 0;
width: 0;
height: 0;
border-width :15px 7px 15px 7px;
border-color: transparent #41423D #41423D transparent;
border-style:solid;
position: absolute;
top: 0;
left: -14px;
}
.divClass:after{
content: '';
line-height: 0;
font-size: 0;
width: 0;
height: 0;
border-width :15px 7px 15px 7px;
border-color: transparent transparent #41423D #41423D;
border-style:solid;
position: absolute;
top: 0;
right: -14px;
}
所以,在设计上就变成了这样:
___
/ \
Now all I need is a shadow on the before and after pseudo elements which are the 2 triangles on either side of the div. The pseudo elements have 0 width and height so using box-shadow is a little tricky.
I need the shadow along the triangle. So, what can I do?
您可以使用 unicode 字符 : ▲ 来制作三角形。
在上面应用文字阴影。
如果三角形的形状不是您想要的,您可以使用 transform: rotate();
或 transform: skewX();
或 both.
进行调整
这有点棘手而且不完美,但它可以工作:
span {
display: inline-block;
font-size: 70px;
transform: skewX(29.5deg);
color: red;
text-shadow: 2px 2px 4px gray;
}
<span>▲</span>
还有一些其他的可能性,都在 CSS 技巧 post 中描述,所以如果你想检查它:
https://css-tricks.com/triangle-with-shadow/
如果你认为你也可以检查 filter: drop-shadow()
。如果您不需要对所有浏览器的支持,它可能适合您...
编辑:
根据css技巧post,你做不到吗?
.triangle-with-shadow {
transform: rotate(-45deg);
width: 100px;
height: 100px;
position: relative;
overflow: hidden;
box-shadow: 0 20px 10px -17px rgba(0, 0, 0, 0.5);
}
.triangle-with-shadow:after {
content: "";
position: absolute;
width: 50px;
height: 50px;
background: #999;
transform: rotate(45deg); /* Prefixes... */
top: 75px;
left: 25px;
box-shadow: -1px -1px 10px -2px rgba(0, 0, 0, 0.5);
}
<div class="triangle-with-shadow"></div>
如果您只想要描述的形状,另一种可能性是使用透视图:
.shape {
background: #41423D;
width: 150px;
height: 150px;
margin: 20px auto;
transform-origin:50% 100%;
transform:perspective(100px) rotateX(30deg);
box-shadow: 2px 2px 15px #41423D;
}
<div class="shape"></div>
好的,所以在 HTML 中我有一个带有前后伪元素的 div 标签。这是我的 CSS:
.divClass{
background: #41423D;
height:30px;
}
.divClass:before{
content: '';
line-height: 0;
font-size: 0;
width: 0;
height: 0;
border-width :15px 7px 15px 7px;
border-color: transparent #41423D #41423D transparent;
border-style:solid;
position: absolute;
top: 0;
left: -14px;
}
.divClass:after{
content: '';
line-height: 0;
font-size: 0;
width: 0;
height: 0;
border-width :15px 7px 15px 7px;
border-color: transparent transparent #41423D #41423D;
border-style:solid;
position: absolute;
top: 0;
right: -14px;
}
所以,在设计上就变成了这样:
___
/ \
Now all I need is a shadow on the before and after pseudo elements which are the 2 triangles on either side of the div. The pseudo elements have 0 width and height so using box-shadow is a little tricky.
I need the shadow along the triangle. So, what can I do?
您可以使用 unicode 字符 : ▲ 来制作三角形。
在上面应用文字阴影。
如果三角形的形状不是您想要的,您可以使用 transform: rotate();
或 transform: skewX();
或 both.
这有点棘手而且不完美,但它可以工作:
span {
display: inline-block;
font-size: 70px;
transform: skewX(29.5deg);
color: red;
text-shadow: 2px 2px 4px gray;
}
<span>▲</span>
还有一些其他的可能性,都在 CSS 技巧 post 中描述,所以如果你想检查它:
https://css-tricks.com/triangle-with-shadow/
如果你认为你也可以检查 filter: drop-shadow()
。如果您不需要对所有浏览器的支持,它可能适合您...
编辑:
根据css技巧post,你做不到吗?
.triangle-with-shadow {
transform: rotate(-45deg);
width: 100px;
height: 100px;
position: relative;
overflow: hidden;
box-shadow: 0 20px 10px -17px rgba(0, 0, 0, 0.5);
}
.triangle-with-shadow:after {
content: "";
position: absolute;
width: 50px;
height: 50px;
background: #999;
transform: rotate(45deg); /* Prefixes... */
top: 75px;
left: 25px;
box-shadow: -1px -1px 10px -2px rgba(0, 0, 0, 0.5);
}
<div class="triangle-with-shadow"></div>
如果您只想要描述的形状,另一种可能性是使用透视图:
.shape {
background: #41423D;
width: 150px;
height: 150px;
margin: 20px auto;
transform-origin:50% 100%;
transform:perspective(100px) rotateX(30deg);
box-shadow: 2px 2px 15px #41423D;
}
<div class="shape"></div>