Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

like image 865
Mindaugas Avatar asked Mar 08 '15 21:03

Mindaugas


People also ask

How do you add a flag in CSS?

New flag can be easily added to CSS - just create or download the SVG of the flag, clean it (there are several programs to clean SVG) and encode its code with help of base64, then just add this code to your CSS file. Encoding is needed because of support some internet browsers.

How do you make a triangle div in CSS?

Normally there is no direct technique to create a triangle using CSS. Approach: To create the triangle, in the HTML part we have to just add a single div for each triangle. The concept is to create a box with no width or height. The width of the border determines the Triangle's actual width and height.


2 Answers

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;
}
<p>Some text ... </p>

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;}
<div>
  <svg viewbox="-1.5 -1.5 103 153">
    <polygon points="100 0, 100 100, 50 85, 0 100, 0 0" fill="transparent" stroke-width="3" stroke="#ef0efe"/>
  </svg>
  <p>Some text ... </p>
</div>
like image 182
web-tiki Avatar answered Sep 23 '22 19:09

web-tiki


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>
like image 45
misterManSam Avatar answered Sep 26 '22 19:09

misterManSam