Question

I'd like to create a CSS button in the shape of a hexagon using a single div to keep the markup clean. I've been experimenting with before and after pseudo elements and can do it with the hexagon 'points' at top and bottom but would like to do it with them pointing left and right to fit the rest of my theme. I've got close but I can't get the after pseudo element where I want it. Can anyone fix this?

Here's where I'm up to:

#hex {
  background-color:green;
  width:100px;
  height:100px;
  float:left;
  display:block;
}

#hex::before {
  content:"";
  border-top:50px solid red;
  border-bottom:50px solid red;
  border-right:30px solid blue;
  float:left;
}

#hex::after {
  content:"";
  border-top:50px solid red;
  border-bottom:50px solid red;
  border-left:30px solid blue;
  float:left;
}

and there's a JS Fiddle at http://jsfiddle.net/higginbottom/YKx2M/

Was it helpful?

Solution

try this example: http://jsbin.com/ipaked/6 (tested on Fx and Chrome)

relevant CSS

.hexagon {
  position: relative;
  width: 124px;
  height: 100px;
  background: #d8d8d8;
}

.hexagon:after,
.hexagon:before {
  position: absolute;
  content: "";
  z-index: 1;
  top: 0;  
  width: 0px;
  background: #fff;
  border-top: 50px transparent solid; 
  border-bottom: 50px transparent solid;
}

.hexagon:before {
    left: 0;
    border-right: 30px #d8d8d8 solid; 
}
.hexagon:after {
    right: 0;
    border-left: 30px #d8d8d8 solid; 
}

(Adjust border-width and size of the hexagon so it can look as you prefer.)


As alternative you can also use a single pseudoelement in which you could show the black hexagon unicode character U+2B21, like in this example: http://jsbin.com/ipaked/7

CSS

.hexagon {
  position: relative;
  width: 120px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.hexagon:before {
  position: absolute;
  content: "\2B21";
  font-size: 160px;
  z-index: 1;
  width: 100%; 
  height: 100%;
  left: 0;
  top: 0;  
}

This is probably a better choice (if using a relative font size) so the hexagon can adjust itself when the user increase or decrease the base font-size on his browser.

OTHER TIPS

I'm using clip-path:

.btn {
  display: inline-block;
  text-align: center;
  text-decoration: none;
  vertical-align: middle;
  user-select: none;
  padding: 0.375rem 2rem;
  
  --btn-raise: 1rem;
  clip-path: polygon(var(--btn-raise) 0%, calc(100% - var(--btn-raise)) 0%, 100% 50%, calc(100% - var(--btn-raise)) 100%, var(--btn-raise) 100%, 0 50%);
  background-color: #fefd64;
  text-transform: uppercase;
}
<a class="btn" href="/call">Call call</a>

Try This codepen link http://codepen.io/bherumehta/pen/egdXLv or http://codepen.io/bherumehta/pen/VPKRBG

.hexa{
  width:300px;
  background:red;
  height:70px;
  color:#fff;
  postion:relative;
  border-top:1px solid red;
  border-bottom:1px solid red;
}

        .hexa-inner{
            height:70px;
             position:relative;
         }
                .hexa-inner{
                  height:70px;
                  position:relative;
                  }
                .hexa-inner:before{
                  content: '';
                  position: absolute;
                  top: 0;
                  left: 0;
                  height: 50%;
                  width: 50px;
                  background: red;
                  -webkit-transform: skew(-45deg, 0deg);
                  -moz-transform: skew(-45deg, 0deg);
                  -ms-transform: skew(-45deg, 0deg);
                  -o-transform: skew(-45deg, 0deg);
                  transform: skew(-45deg,0deg);
                }
                .hexa-inner:after {
                  content: '';
                  position: absolute;
                  top: 50%;
                  left: 0;
                  height: 50%;
                  width: 50px;
                  background: red;
                  -webkit-transform: skew(-135deg, 0deg);
                  -moz-transform: skew(-135deg, 0deg);
                  -ms-transform: skew(-135deg, 0deg);
                  -o-transform: skew(-135deg, 0deg);
                  transform: skew(-135deg, 0deg);
                }
                .left-arrow{
                 margin-left:-18px;
                 float:left;
                }
                .right-arrow{
                  transform:rotate(180deg);
                  float:right;
                  margin-right:-18px
                }
                .hexa p{
                  white-space:nowrap;
                  max-width:100%;
                  overflow:hidden;
                  text-overflow:ellipsis;
                }        

HTML

    <div class="hexa">
       <div class="hexa-inner left-arrow"> </div>
       <div class="hexa-inner right-arrow"> </div>
       <p>hexagonhexagonhexagonhexagonhexagonhexagonhexagonhexagonhexago
        xagonhexagonhexagonhexagonhexagonhexagonhexagon</p>
   </div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top