0

I'd like to replicate the image below using only html and css if possible. I'd like to use this as a "badge" with changing icons and colors. A static image won't work for me:

enter image description here

All seems doable, but I do not know how to replicate the actual shape of the badge itself (the green part). Is there a particular css directive I need to look into to achieve this?

EDIT: Appears a "Duplicate Question" concern was raised. Please note the linked to question does NOT include an image or text in the center of the hexagon, this one does. This adds a bit of complexity to the problem, and thus the new question. Based on the answers in this question, I was able to formulate a better google search and came across a codepen that worked beautifully for me. I vote for re-opening the question.

Source Matters
  • 1,110
  • 2
  • 15
  • 35

2 Answers2

4

Yes possible. But its better if you use SVG. Here is an example with rounded corner

body{margin:0;}
.hexagon {
    width: 120px;
    height: 60px;
    position: relative;
    margin:50px 10px;
}
.hexagon, .hexagon:before, .hexagon:after {
    background: green;
    border-radius: 5px  
}
.hexagon:before, .hexagon:after {
    content: "";
    position: absolute;
    left: 23px;
    width: 74px;
    height: 70px;
    -moz-transform: rotate(145deg) skew(22.5deg);
    -webkit-transform: rotate(145deg) skew(22.5deg);
    transform: rotate(145deg) skew(22.5deg);
}
.hexagon:before {
    top: -35px;
}
.hexagon:after {
    top: 35px;
}
.hexagon span {
    position: absolute;
    top: 0;
    left: 0;
    width: 120px;
    height: 70px;
    background:green;
    z-index: 1;
}
 <div class="hexagon"><span></span></div>
Baezid Mostafa
  • 2,680
  • 2
  • 14
  • 26
  • 1
    I like how you are pushing skew to get some rounding! It doesn't happen on all corners evenly (in FF), but I hadn't considered that. – Nathaniel Flick Nov 27 '16 at 21:29
  • @NathanielFlick I know all the corner is not rounded .. I also don't consider using css to make it . :P SVG is more easier. My previous answer was same as your one So I edit my answer :) – Baezid Mostafa Nov 27 '16 at 21:42
3

CSS Tricks has a great page on shapes, more here, and here's how I'd accomplish what you want, instead of the text I have in the shape div, replace with your icon font of choice and its html and css (ignore the margin-top I added to the hexagon, it's only there so you can see the whole thing:

#hexagon { width: 100px; height: 55px; background: red; position: relative; margin-top: 3em; line-height: 55px; text-align: center; color: white;} 

#hexagon:before { content: ""; position: absolute; top: -25px; left: 0; width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 25px solid red; } 

#hexagon:after { content: ""; position: absolute; bottom: -25px; left: 0; width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-top: 25px solid red; }
<div id="hexagon">X THING</div>
Nathaniel Flick
  • 2,902
  • 2
  • 22
  • 31
  • rep for the link! awesome page. – Source Matters Nov 27 '16 at 21:25
  • You're very welcome! I've found it useful many times. While I love SVG I still favour getting it done with html and css first before adding it in. It's still more complex code with more chance of incorrect export and coming up with feature bloat and some small issues. – Nathaniel Flick Nov 27 '16 at 21:26