[英]div with triangle at the bottom with background image
I want to make a div
, with a triangle at the bottom .我想做一个
div
,底部有一个三角形。 But I need the background image on the triangle to appear, I've tried using a pseudo element ( :after
) but it doesn't work.但是我需要三角形上的背景图像出现,我尝试使用伪元素(
:after
)但它不起作用。
#homebg:after{
content:'';
position: absolute;
top: 100%;
left: 0;
right: 0;
margin: 0 auto;
width: 0;
height: 0;
border-top: solid 50px #fff;
border-left: solid 48vw transparent;
border-right: solid 48vw transparent;
}
I need to make the div
appear like in this image with the background
in the triangle :我需要使
div
看起来像这张图片中的三角形background
:
If the triangle is displayed over a plain color, you can use this approach with an absolutely positioned pseudo element :如果三角形显示在纯色上,则可以将此方法与绝对定位的伪元素一起使用:
div{ position:relative; background:url('http://i.imgur.com/W27LCzB.jpg'); background-size:cover; min-height:100px; padding-bottom:100px; overflow:hidden; } div:after{ content:''; position:absolute; bottom:0; left:0; border-left:50vw solid #fff; border-right:50vw solid #fff; border-top:100px solid transparent; }
<div></div>
The left and right parts of the triangle are hidden by the left and right borders of the pseudo element.三角形的左右部分被伪元素的左右边框隐藏。 That is why this approach won't work over a gradient or image.
这就是为什么这种方法不适用于渐变或图像的原因。
In these cases, you can use an inline svg with clipPath and a polygon element :在这些情况下,您可以使用带有clipPath和 多边形元素的内联 svg :
body, html{ height:100%; background:url('https://farm8.staticflickr.com/7187/6895047173_d4b1a0d798.jpg')no-repeat center center; background-size:cover; } svg{ display:block; width:100%; }
<svg viewbox="0 0 100 40"> <clipPath id="clip"> <polygon points="0 0 100 0 100 25 50 40 0 25" /> </clipPath> <image xlink:href="https://farm4.staticflickr.com/3165/5733278274_2626612c70.jpg" width="100" height="65" clip-path="url(#clip)"/> </svg>
There are other possible approaches for the same result.对于相同的结果,还有其他可能的方法。 You can find some here : CSS Transparent arrow/triangle
你可以在这里找到一些: CSS透明箭头/三角形
We can do this with only linear-gradient
and mask
.我们可以只用
linear-gradient
和mask
来做到这一点。 You can adjust the height you want.你可以调整你想要的高度。
div { --triangle-height: 100px; /* you can change this */ --mask: linear-gradient(#000, #000) 0 0/100% calc(100% - var(--triangle-height)) no-repeat, linear-gradient(to top right, transparent 49.5%, #000 50% 100%) 0 100%/50% var(--triangle-height) no-repeat, linear-gradient(to top left, transparent 49.5%, #000 50% 100%) 100% 100%/50% var(--triangle-height) no-repeat; -webkit-mask: var(--mask); mask: var(--mask); width: 500px; height: 400px; background: url(https://i.picsum.photos/id/1043/5184/3456.jpg?hmac=wsz2e0aFKEI0ij7mauIr2nFz2pzC8xNlgDHWHYi9qbc) 50% 50%/cover; background-repeat: no-repeat; }
<div></div>
By changing the variable and adjusting width: 100%
通过改变变量和调整
width: 100%
div { --triangle-height: 200px; /* you can change this */ --mask: linear-gradient(#000, #000) 0 0/100% calc(100% - var(--triangle-height)) no-repeat, linear-gradient(to top right, transparent 49.5%, #000 50% 100%) 0 100%/50% var(--triangle-height) no-repeat, linear-gradient(to top left, transparent 49.5%, #000 50% 100%) 100% 100%/50% var(--triangle-height) no-repeat; -webkit-mask: var(--mask); mask: var(--mask); width: 100%; height: 400px; background: url(https://i.picsum.photos/id/1043/5184/3456.jpg?hmac=wsz2e0aFKEI0ij7mauIr2nFz2pzC8xNlgDHWHYi9qbc) 50% 50%/cover; background-repeat: no-repeat; }
<div></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.