How to get 'div' shaped as a flag with CSS

前端 未结 7 1705
旧巷少年郎
旧巷少年郎 2020-12-17 23:43

I want to add a label on some of my elements on a website and design for a label that is a flag with an inverted V-shaped cut at the bottom.

So far I have this:

7条回答
  •  [愿得一人]
    2020-12-18 00:17

    With CSS:

    You can use CSS transforms on pseudo elements to create the background with a transparent inverted triangle at the bottom:

    body{background:url('http://lorempixel.com/image_output/food-q-c-640-480-1.jpg');background-size:cover;}
    p{
      position: relative;
      width: 150px; height: 150px;
      overflow: hidden;
      border-top:3px solid #EF0EFE;
    }
    p:before, p:after{
      content: '';
      position: absolute;
      top: -3px;
      height: 100%; width: 50%;
      z-index: -1;
      border:2px solid #EF0EFE;
      box-sizing:border-box;
    }
    p:before{
      left: 0;  
      transform-origin: 0 0;
      transform: skewY(-20deg);
      border-width:0 0 4px 3px;
    }
    p:after{
      right: 0;
      transform-origin: 100% 0;
      transform: skewY(20deg);
      border-width:0 3px 4px 0;
    }

    Some text ...

    Note that you will need to add vendor prefixes on the transform and transform-origin properties to maximize browser support. See canIuse for more information.


    With SVG

    Another approach is to use an inline SVG with the polygon element:

    body{background: url('http://lorempixel.com/image_output/food-q-c-640-480-1.jpg');background-size: cover;}
    div{position: relative;width: 100px; height: 150px;}
    svg{position: absolute;width: 100%;height: 100%;z-index: -1;}

    Some text ...

提交回复
热议问题