简体   繁体   English

如何在CSS中使用底部有角度的切口和图像背景来完成此形状?

[英]How to accomplish this shape with angled cuts at the bottom and an image background in CSS?

I have read up on various methods and played with the Clippy tool , the problem is the browser support just isn't there yet. 我已经阅读了各种方法并使用了Clippy工具 ,但问题是浏览器支持还不存在。 What would be the best method for accomplishing the look of the image below with CSS? 用CSS完成下面图像外观的最佳方法是什么? I am trying to add a shape as bottom-border as you can see in the image below right after the blue background image. 我正在尝试将形状添加为bottom-border就像您在蓝色背景图像之后的下图中看到的那样。 Is there a way I can do this that most recent major browsers support through CSS? 有什么办法可以使大多数主流浏览器都通过CSS支持呢?

What I've tried (doesn't seem to work in Chrome and others): 我尝试过的方法(在Chrome和其他系统中似乎不起作用):

.element {
  -webkit-clip-path: polygon(50% 0%, 100% 0, 100% 86%, 75% 100%, 0 85%, 0 0);
  clip-path: polygon(50% 0%, 100% 0, 100% 86%, 75% 100%, 0 85%, 0 0);
}

The desired result would look something like: 所需的结果如下所示: 在此处输入图片说明

Both dippas' answer and the demo in misterManSam's comment are good but they would work only if the page background is a solid color (which can then be used as border's color or within the gradient). dippas的回答和misterManSam注释中的演示都不错,但只有页面背景为纯色(然后可用作边框的颜色或在渐变内)时,它们才有效。 They would run into problems when the page's background is either an image (or) a gradient and they should show through the cutout portion of the shape. 当页面的背景是图像(或渐变)时,它们会出现问题,并且它们应该通过形状的切口部分显示出来。

For such cases I would recommend using SVG instead of CSS because it is so complex to create it with CSS that it is not actually worth the effort. 对于这种情况,我建议使用SVG而不是CSS,因为使用CSS创建它非常复杂,以至于实际上不值得付出努力。 Though you've asked for CSS, I will detail these SVG methods here just in case you want to use them (or atleast some future readers might find it helpful). 尽管您已请求CSS,但在这里我将详细介绍这些SVG方法,以防万一您想使用它们(或者至少将来有一些读者会发现它很有用)。

With SVG: 使用SVG:

With SVG we can either create a path and fill it with the image (or) use a SVG mask for creating the shape. 使用SVG,我们可以创建path并用图像填充(或)使用SVG蒙版创建形状。 ( Note: CSS clip-path using SVG is still a no-go due to lack of support in IE. ) 注意: 由于缺乏IE支持,使用SVG的CSS clip-path仍然不可行。

Below snippet uses SVG path element to create the shape and then fill it with the image. 下面的代码段使用SVG path元素创建形状,然后将其填充图像。

 svg { height: 200px; width: 100%; } path { fill: url(#image); } /* Just for demo */ path:hover{ cursor: pointer; } body { min-height: 100vh; background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%); } 
 <svg viewBox='0 0 1024 200' preserveAspectRatio='none'> <defs> <pattern id='image' height='200' width='1024' patternUnits='userSpaceOnUse'> <image xlink:href='http://lorempixel.com/1024/200/nature/3' height='200' width='1024' /> </pattern> </defs> <path d='M0,0 1024,0 1024,150 716.8,200 0,150z' /> </svg> 

The following snippet uses SVG mask. 以下代码片段使用SVG遮罩。 The difference between using a path with an image fill and a mask is the hover area. 使用具有图像填充的path和蒙版之间的区别是悬停区域。 With a path the hover effects are restricted to the shape boundary whereas with a mask, the image is still a rectangle (or square) and so hover effects are triggered even outside. 使用path ,悬停效果仅限于形状边界,而使用遮罩时,图像仍为矩形(或正方形),因此即使在外部也会触发悬停效果。

 svg { height: 200px; width: 100%; } image { mask: url(#masker); } /* Just for demo */ image:hover{ cursor: pointer; } body { min-height: 100vh; background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%); } 
 <svg viewBox='0 0 1024 200' preserveAspectRatio='none'> <defs> <mask id='masker' x='0' y='0' width='1024' height='200'> <polygon points='0,0 1024,0 1024,200 0,200z' fill="#fff" /> <path d='M0,150 716.8,200 1024,150 1024,200 0,200z' fill="#000" /> </mask> </defs> <image xlink:href='http://lorempixel.com/1024/200/nature/3' height='200' width='1024' /> </svg> 


With CSS: 使用CSS:

The below option is our best bet with pure CSS but unfortunately it has poor browser support. 下面的选项是我们使用纯CSS的最佳选择,但是不幸的是,它对浏览器的支持不佳。 It uses CSS linear-gradient as mask images to hide the portions that are not required. 它使用CSS linear-gradient作为蒙版图像来隐藏不需要的部分。 This method works only in Webkit powered browsers for now and so is a no-go . 此方法目前仅在支持Webkit的浏览器中有效,因此暂时不行

 .shape { height: 200px; width: 100%; background-image: url(http://lorempixel.com/1200/200/nature/3); -webkit-mask-image: linear-gradient(to top right, transparent 49.5%, white 50.5%), linear-gradient(to top left, transparent 49.5%, white 50.5%), linear-gradient(white, white); mask-image: linear-gradient(to top right, transparent 49.5%, white 50.5%), linear-gradient(to top left, transparent 49.5%, white 50.5%), linear-gradient(white, white); -webkit-mask-size: 70.5% 30%, 30% 30%, 100% 70%; -webkit-mask-position: bottom left, bottom right, top left; -webkit-mask-repeat: no-repeat; } body { min-height: 100vh; background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%); } 
 <div class='shape'></div> 

Other attempts to produce a transparent cut run into problems if the shape has to be responsive. 如果必须响应形状,则其他尝试产生透明切口的尝试​​也会遇到问题。 For example, the below snippet uses very complex transformations, positioning etc to achieve this shape but it is not responsive (view in full page mode). 例如,下面的代码段使用非常复杂的转换,定位等方式来实现此形状,但没有响应(以全页模式查看)。 I wouldn't have recommended this method even if the shape was responsive (due to complexities involved) but the lack of responsiveness means this is a no-go . 即使形状是响应性的(由于涉及的复杂性),我也不会推荐这种方法,但是缺乏响应性意味着这是不可行的

 .shape { position: relative; height: 200px; width: 100%; overflow: hidden; } .shape-left, .shape-right, .shape img { position: absolute; height: 100%; } .shape-left { width: 75%; transform: skewY(5deg); overflow: hidden; } .shape-left img { top: -7%; bottom: 0px; width: 133.3%; transform: skewY(-5deg); } .shape-left, .shape-left img { transform-origin: bottom right; backface-visibility: hidden; } .shape-right { right: 0%; width: 25%; transform: skewY(-10deg); overflow: hidden; } .shape-right img { top: -13.5%; left: -300%; width: 400%; transform: skewY(10deg); } .shape-right { transform-origin: bottom left; backface-visibility: hidden; } /* just for demo */ .reference { height: 200px; width: 100%; } .reference img { height: 100%; width: 100%; } * { box-sizing: border-box; } body { min-height: 100vh; background-image:radial-gradient(circle, #3F9CBA 0%, #153346 100%); } 
 <div class='shape'> <div class='shape-left'> <img src='http://lorempixel.com/800/200/nature/3' /> </div> <div class='shape-right'> <img src='http://lorempixel.com/800/200/nature/3' /> </div> </div> <hr> <div class='reference'> <img src='http://lorempixel.com/800/200/nature/3' /> </div> 

Note: This may have been the item that misterManSam was referring to in comments but I feel the needs are a bit different here even though both involve creating unusual borders. 注意: 可能是misterManSam在评论中提到的项目,但我觉得这里的需求有些不同,即使两者都涉及创建异常边界。

you can use a background-image on a div and two shapes using it pseudo-selectors :before/:after 您可以在div和两个形状上使用伪选择器使用background-image :before/:after

Something like this: 像这样:

 .bg { background: url(http://lorempixel.com/1600/900) no-repeat center top; min-height: 100px; min-width: 200px; position: relative } .bg:before { content: ""; border-bottom: 65px solid white; border-right: 575px solid rgba(0, 0, 0, 0); position: absolute; left: 0; bottom: 0; } .bg:after { content: ""; border-bottom: 65px solid white; border-left: 200px solid rgba(0, 0, 0, 0); position: absolute; right: 0; bottom: 0; } 
 <div class="bg"></div> 

I think the easiest way to do it is with pseudo elements on the parent div element. 我认为最简单的方法是使用父div元素上的伪元素。 This is basic CSS knowledge and can be implemented very easily. 这是CSS的基本知识,可以很容易地实现。 The parent div needs to have the position: relative; 父div需要具有以下position: relative; property set and the rest is done by the ::before and ::after elements. 属性集,其余的由:: before和:: after元素完成。

.background::before {
      transform: rotate(10deg);
      position: absolute;
}

Example

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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