Search code examples
htmlcsssvgcss-shapes

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


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:

HTML

<div class="css-shapes"></div>

CSS

.css-shapes{
    border-left: 99px solid #f00fff;
    border-right: 99px solid #f00fff;
    border-bottom: 39px solid transparent;
}

http://jsfiddle.net/yhexkm4u/2/

However, I need the background to be white and border around this shape in purple and 1px. I was trying to fit the same shape just in white inside of this one, but everything got messy and didn't go as expected.

Maybe it is a wrong approach, but I want to end up with labels that would look something like this:


Solution

  • Here is a slightly different method using pseudo-elements and transform rotations to create an outlined banner like this:

    Before overflow cut

    • This angled shape is created with position: absolute pseudo-elements, :before and :after:

    Before overflow cut

    • The excess is cut off with overflow: hidden on the parent to form our banner:

    After overflow cut

    • The outline is created with box-shadow and the two angles are prevented from overlapping by pulling / pushing the x-axis by 46px — box-shadow: 46px 0 0 3px #000

    Full Example

    div {
      height: 100px;
      width: 100px;
      margin: 100px auto;
      position: relative;
      overflow: hidden;
      border: solid 3px #000;
      border-bottom: none;
      text-align: center;
    }
    div:before,
    div:after {
      content: '';
      display: block;
      height: 100%;
      width: 200%;
      transform: rotate(20deg);
      box-shadow: 46px 0 0 3px #000;
      position: absolute;
      top: 1px;
      right: -120%;
    }
    div:after {
      transform: rotate(-20deg);
      left: -120%;
      box-shadow: -46px 0 0 3px #000;
    }
    <div>Text</div>