Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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: 00;
  transform: skewY(-20deg);
  border-width:004px3px;
}
p:after{
  right: 0;
  transform-origin: 100%0;
  transform: skewY(20deg);
  border-width:03px4px0;
}
<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><svgviewbox="-1.5 -1.5 103 153"><polygonpoints="100 0, 100 100, 50 85, 0 100, 0 0"fill="transparent"stroke-width="3"stroke="#ef0efe"/></svg><p>Some text ... </p></div>

Solution 2:

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: 46px003px#000;
  position: absolute;
  top: 1px;
  right: -120%;
}
div:after {
  transform: rotate(-20deg);
  left: -120%;
  box-shadow: -46px003px#000;
}
<div>Text</div>

Solution 3:

STOLEN FROM CSS-SHAPES

#flag {
  width: 110px;
  height: 56px;
  padding-top: 15px;
  position: relative;
  background: red;
  color: white;
  font-size: 11px;
  letter-spacing: 0.2em;
  text-align: center;
  text-transform: uppercase;
}
#flag:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 0;
  border-bottom: 13px solid #eee;
  border-left: 55px solid transparent;
  border-right: 55px solid transparent;
}

DEMO:

#flag {
  width: 110px;
  height: 56px;
  padding-top: 15px;
  position: relative;
  background: red;
  color: white;
  font-size: 11px;
  letter-spacing: 0.2em;
  text-align: center;
  text-transform: uppercase;
}
#flag:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 0;
  border-bottom: 13px solid #eee;
  border-left: 55px solid transparent;
  border-right: 55px solid transparent;
}
          
<divid="flag"></div>

My Approach

My approach uses skewed elements, and allows you to quickly position them to your needs.

div {
  height: 100px;
  width: 100px;
  position: relative;
  border-left: 10px solid tomato;
  border-top: 10px solid tomato;
  border-right: 10px solid tomato;
  text-align: center;
  line-height: 100px;
  font-size: 30px;
}
div:before {
  content: "";
  position: absolute;
  height: 50%;
  width: 50%;
  left: -10px; /*width of border*/bottom: -30px;
  z-index: -2;
  -webkit-transform: skewY(-20deg);
  transform: skewY(-20deg);
  border-bottom: 10px solid tomato;
  border-left: 10px solid tomato;
}
div:after {
  content: "";
  position: absolute;
  height: 50%;
  width: 50%;
  right: -10px; /*width of border*/bottom: -30px;
  z-index: -2;
  -webkit-transform: skewY(20deg);
  transform: skewY(20deg);
  border-bottom: 10px solid tomato;
  border-right: 10px solid tomato;
}
div:hover, div:hover:before, div:hover:after{
  background:lightgray;
  }
<div>TEXT</div>

Solution 4:

I've had a go at updating your CSS to create the effect you want:

.css-shapes {
  height: 250px;
  width: 0px;
  border-left: 99px solid #f00fff;
  border-right: 99px solid #f00fff;
  border-bottom: 39px solid transparent;
  position: relative
}
.n-shape {
  height: 248px;
  width: 0px;
  border-left: 95px solid #ffffff;
  border-right: 95px solid #ffffff;
  border-bottom: 39px solid transparent;
  position: absolute;
  top: -6px;
  right: -95px;
}
.top {
  position: absolute;
  top: 0px;
  width: 198px;
  height: 2px;
  background-color: #f00fff;
  left: -99px;
  border-bottom: 1px solid #f00fff;
}
<divclass="css-shapes"><divclass="n-shape"></div><divclass="top"></div></div>

Fiddle: http://jsfiddle.net/dywhjwna/

Solution 5:

Here is what I came up with.

Link Fiddle

It correspond to what you were looking for however I guess there should be a "better way" to it rather than playing with border.

HTML

<div id="text-div">
    Text
</div>
<div id="pacman">
    <div id="left-triangle"></div>
    <div id="right-triangle"></div>
</div>

CSS

#text-div {
    width: 118px;
    height: 60px;
    text-align: center;
    border: 1px solid purple;
    border-bottom: 0px;
    line-height: 60px;
}

#pacman { 
    width: 0px; 
    height: 0px;
    border-right: 60px solid purple; 
    border-top: 0px; 
    border-left: 60px solid purple; 
    border-bottom: 60px solid transparent;
}

#left-triangle{
    position: relative;
    left: -59px;
    border-right: 58px solid transparent; 
    border-top: 0px; 
    border-left: 58px solid white; 
    border-bottom: 58px solid transparent;
}
#right-triangle{
    position: relative;
    top: -59px;
    left: -57px;
    border-right: 58px solid white;
    border-top: 0px; 
    border-left: 58px solid transparent; 
    border-bottom: 58px solid transparent;
}

Post a Comment for "How To Get 'div' Shaped As A Flag With Css"