简体   繁体   English

底部带有三角形的 div 带有背景图像

[英]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 带有 * 背景图像 * 和底部全宽三角形的 div

Triangle over a plain color纯色上的三角形

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.这就是为什么这种方法不适用于渐变或图像的原因。


Triangle over an image or gradient图像或渐变上的三角形

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透明箭头/三角形

You can use a clipping mask您可以使用剪贴蒙版

div {
    -webkit-clip-path: polygon(0% 0%, 100% 0, 100% 75%, 50% 100%, 0 75%);
    clip-path: polygon(0% 0%, 100% 0, 100% 75%, 50% 100%, 0 75%);
}

Have a look at this website to generate your own masks.看看这个网站来生成你自己的面具。

We can do this with only linear-gradient and mask .我们可以只用linear-gradientmask来做到这一点。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM