在div上切角,图像设置为100%的高度和宽度 [英] Cut corner on div with image set to 100% height and width

查看:197
本文介绍了在div上切角,图像设置为100%的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这基本上是我想要实现的 - > https://jsfiddle.net/tak1pyt7 / 1 /

.clip {width:500px;身高:500px; -webkit-clip-path:多边形(100%0,100%81%,82%100%,0 100%,0 0); clip-path:多边形(100%0,100%81%,82%100%,0 100%,0 0);} body {背景图像:径向梯度(圆圈,#3F9CBA 0%,#153346 100% );身高:100%; width:100%;}

< body> < div class =clip> < img src =http://lorempixel.com/600/600/width =100%height =100%/> < / div>< / body>

$ b

一个解决方案,但在小提琴中的代码存在各种问题,如下所示:


  • 它在IE或Edge中不能用作剪辑路径不支持

  • 剪辑路径不支持Firefox中的css形状,如果您需要使其在Firefox中工作,则需要提供内联svg

  • 试图提供内联svg,但它有它自己的问题,不能解决我的要求。无论容器的高度和宽度如何,我都需要切割尺寸保持不变。使用内嵌svg可以立即剪切图像,它的尺寸随高度和宽度的变化而变化,即剪切是有反应的。我需要剪切的静态尺寸。



除了上面的解决方案,我搜索了很多解决方案,可以帮助我创建带角落的div剪切和切割本身是透明的,因为我没有背后的坚实背景。



其他解决方案探索

使用CSS3 Gradients示例





使用CSS边界





使用jQuery插件( http://jquery.malsup.com/corner/


  • 示例代码 - > http://jsfiddle.net/spdawson/nxnCD/light/


  • 您需要拥有坚实的背景颜色,考虑到我的要求,情况并非如此。相反,我有一个图像作为背景。


  • $ b >




    • 样本 - > http://jsfiddle.net/26v7pfLb/


    • 我现在正在使用此解决方案来获得固定高度和宽度的容器,但我在我的应用程序中有一个页面,容器有一个静态宽度但动态高度。在这种情况下,破解将非常难以实施,而且亲自使用该解决方案似乎很奇怪。

    • 我试图找到更优雅和干净的东西来实现CSS中的这种解决方案。

    • / ul>

      任何指针都会有帮助。

      解决方案

      您可以使用我在此问题中描述的方法:使用CSS切割角落使用带有 overflow:hidden; 的旋转容器



      对于你的例子(因为你想在元素中显示图像),你需要像这样旋转内容:

        .clip {margin:-150px 0 0 -150px;显示:inline-block; transform:旋转(-45deg); overflow:hidden;} img {margin:150px 150px 0 150px;显示:块; transform:rotate(45deg);} body {background:url(http://lorempixel.com/output/nature-qg-640-480-7.jpg); background-size:cover;}  < div class =clip>  



      此解决方案还需要定位边距或绝对定位。


      This is basically what i want to achieve --> https://jsfiddle.net/tak1pyt7/1/

      .clip {
        width: 500px;
        height: 500px;
        -webkit-clip-path: polygon(100% 0, 100% 81%, 82% 100%, 0 100%, 0 0);
        clip-path: polygon(100% 0, 100% 81%, 82% 100%, 0 100%, 0 0);
      }
      body {
        background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
        height: 100%;
        width: 100%;
      }

      <body>
        <div class="clip">
          <img src="http://lorempixel.com/600/600/" width="100%" height="100%" />
        </div>
      </body>

      Although i have a solution but there are various issues with the code in the fiddle as follows :

      • It doesn't work in IE or Edge as clip path isn't supported
      • Clip path doesn't support css shapes in firefox and if you need to make it work in firefox then you need to supply inline svg
      • Tried supplying inline svg as well but it has it's own set of issues that doesn't solve my requirement. I need the cut dimensions to remain the same irrespective of the height and width of the container. With inline svg that cuts the image right now, it's dimension changes with change in height and width i.e. the cut is responsive. I need static dimensions of the cut.

      Other than above solution i searched a whole lot of solutions that might help me create div's with corners cut and the cut is transparent itself because i don't have a solid background at the back.

      Other solutions explored

      Using CSS3 Gradients sample

      Using CSS Borders

      Using jQuery plugin (http://jquery.malsup.com/corner/)

      A Solution that WORKS but is very HACKY

      • Sample --> http://jsfiddle.net/26v7pfLb/

      • I am using this solution right now for containers that have fixed height and width but i have a page in my app where the containers have a static width but dynamic height. In that case the hack would be pretty difficult to implement and it seems very weird to use this solution personally.

      • I am trying to find something more elegant and clean to implement such a kind of solution in CSS

      Any pointers would be helpful.

      解决方案

      You can use the approach I described in this question : Cut Corners using CSS using a rotate container with overflow:hidden;

      For your example (as you want to display an image inside the element) you need to "unrotate" the content like this :

      .clip {
        margin: -150px 0 0 -150px;
        display: inline-block;
        transform: rotate(-45deg);
        overflow: hidden;
      }
      img {
        margin: 150px 150px 0 150px;
        display: block;
        transform: rotate(45deg);
      }
      body{background:url(http://lorempixel.com/output/nature-q-g-640-480-7.jpg);background-size:cover;}

      <div class="clip">
          <img src="http://lorempixel.com/600/600/" />
      </div>

      This solution also requires positioning either with margins or with absolute positioning.

      这篇关于在div上切角,图像设置为100%的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆