Tiled Hexagon Background

In this example below you will see how to do a Tiled Hexagon Background with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 007design, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 007design ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Tiled Hexagon Background</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <html data-ng-app="app">

  <head>
    <link rel="stylesheet" href="style.css">
  </head>

  <body>
    <div class="container">
      <div class="hexagon-tile-background"></div>
    </div>
    
    <script data-require="jquery@2.1.1" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    
    <script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://code.angularjs.org/1.5.6/angular.min.js"></script>
    <script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://code.angularjs.org/1.5.6/angular-animate.min.js"></script>
    <script src="script.js"></script>
  </body>

</html>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/007design/tiled-hexagon-background-ZOOpyo */
.container {
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 80%;
  width: 80%;
}

.hexagon-tile-background {
  position: absolute;
}
.hexagon-tile-background .hex-row {
  width: 100%;
  margin-bottom: -30px;
  display: block;
}
.hexagon-tile-background .hex-row.even {
  margin-left: 64.0373831776px;
}
.hexagon-tile-background .hex {
  position: relative;
  width: 36px;
  height: 60px;
  background-color: #ddd;
  text-align: center;
  display: inline-block;
  margin-right: 88.0373831776px;
}
.hexagon-tile-background .hex:before {
  content: " ";
  width: 0;
  height: 0;
  border-right: 18px solid #ddd;
  border-top: 30px solid transparent;
  border-bottom: 30px solid transparent;
  position: absolute;
  left: -18px;
  top: 0;
}
.hexagon-tile-background .hex:after {
  content: " ";
  width: 0;
  position: absolute;
  right: -18px;
  top: 0;
  border-left: 18px solid #ddd;
  border-top: 30px solid transparent;
  border-bottom: 30px solid transparent;
}


/*Downloaded from https://www.codeseek.co/007design/tiled-hexagon-background-ZOOpyo */
/* global angular */
"use strict";

angular.module('app', [])

.directive('hexagonTileBackground', [function() {
  return {
    restrict: 'C',
    compile: function(tElem) {
      tElem.append('<span class="hex-row" data-ng-repeat="y in repeatY track by $index" data-ng-class-even="\'even\'"><span class="hex" data-ng-repeat="x in repeatX track by $index"></span></span>');

      return function(scope, elem) {
        var hexSize = 100;
        var margin = 10;

        var elemWidth = 0;
        var elemHeight = 0;

        function calculateRepeats() {
          hexSize = parseInt(hexSize,10);
          margin = parseInt(margin,10);
          scope.repeatX = new Array(Math.ceil(elemWidth / (hexSize+margin))+1);
          scope.repeatY = new Array(Math.ceil(elemHeight / hexSize)*2);
          $(elem).css({
            'top': (hexSize/2*-1)+'px',
            'left': ((hexSize*.3)*-1)+'px',
            'height': (scope.repeatY.length * (hexSize+margin))+'px',
            'width': (scope.repeatX.length * (hexSize+margin))+'px'
          });
        }

        scope.$watch(function() {
          return $('.hex').height() || hexSize;
        }, function(s) {
          hexSize = s;
          calculateRepeats();
        });

        scope.$watch(function() {
          return $('.hex').css('margin-right') || margin;
        }, function(m) {
          margin = m;
          calculateRepeats();
        });

        scope.$watch(function() {
          return $(elem).parent().height();
        }, function(h) {
          elemHeight = h;
          calculateRepeats();
        });

        scope.$watch(function() {
          return $(elem).parent().width();
        }, function(w) {
          elemWidth = w;
          calculateRepeats();
        });

        $(window).on('resize',scope.$apply);
      };
    }
  }
}]);

Comments