dynamic styles
HTML
<div class="hexagon"></div>
<div class="hexagon"></div>
<div class="hexagon"></div>
<div class="hexagon"></div>
<div class="hexagon"></div>
<div class="hexagon"></div>
<div class="hexagon"></div>
<div class="hexagon"></div>
<div class="hexagon"></div>
CSS
.hexagon {
position: relative;
display: inline-block;
width: 100px;
height: 57.74px;
background-color: #F7CA18;
margin: 28.87px 0;
margin-right: 4px;
}
.hexagon:before,
.hexagon:after {
content: "";
position: absolute;
width: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
}
.hexagon:before {
bottom: 100%;
border-bottom: 28.87px solid #F7CA18;
}
.hexagon:after {
top: 100%;
width: 0;
border-top: 28.87px solid #F7CA18;
}
JavaScript
var randomColors = ["#96281B","#F7CA18","yellow", 'black', 'red', 'green', 'blue'];
function getHexagonClassName(color) {
return 'hexagon-' + (color[0] === '#' ? color.substr(1) : color);
}
$.each(randomColors, function(_, color) {
var className = getHexagonClassName(color);
$('<style>.' +
className + '{background-color:' + color + ';}.' +
className + ':before{border-bottom-color:' + color + ';}.' +
className + ':after{border-top-color:' + color + ';}</style>')
.appendTo($('head'));
});
$(".hexagon").each(function(index) {
var len = randomColors.length;
var randomNum = Math.floor(Math.random()*len);
var color = randomColors[randomNum];
var className = getHexagonClassName(color);
$(this).addClass(className);
});