62 | Hexi-Flexi Grid is an SCSS component, built on the CSS Grid layout, that creates a hexagonal lattice. The mixin includes a number of customizeable settings to control the size, layout and look of the hex grid.
63 |
64 | Fork on Github
65 |
Features
68 |-
69 |
- Pure CSS styling, no JavaScript 70 |
- Flexible height, width, column and row counts 71 |
- Modular styling for individual cells, columns and rows 72 |
- Supports auto-populating background images 73 |
Browser Support
75 |-
76 |
- Firefox 56+ 77 |
- Chrome 61+ 78 |
- Safari 10.1+ 79 |
- iOS Safari 10.3+ 80 |
- Chrome for Android 62+ 81 |
- IE 11/Edge: CSS Grid is currently supported in modern IE and Edge, however Hexi-Flexi Grid makes use of CSS clip-path, which is not supported. A work-around for this would be to populate the grid with hexagonal SVG img elements, as SVG is universally supported by modern browsers. 82 |
Getting Started
125 |To build the basic 3x2 hex grid shown above, follow the steps listed below.
138 |HTML
139 |Include the following nested div tree in your HTML markup at the location you want to build the hex grid. Give the top level parent a unique id.
140 |Within the div with the class .hexContainer, include a number of divs with the class .hex equal to the number of hexagonal cells in the grid (in this case 6).
141 | 142 |HTML
144 |<div id="myHexGrid">
145 | <div class="hexCrop">
146 | <div class="hexContainer">
147 | <div class="hex"></div>
148 | <div class="hex"></div>
149 | <div class="hex"></div>
150 | <div class="hex"></div>
151 | <div class="hex"></div>
152 | <div class="hex"></div>
153 | </div>
154 | </div>
155 | </div>
156 | SCSS
159 |Include the Hexi-Flexi Grid component files in the same directory as your main SCSS file. Within your main SCSS file, import hex-style.scss above any rules that will affect the content of the grid.
160 | 161 |SCSS (main)
163 |@import 'path/to/hex-style.scss';
164 | Inside of hex-style is a modular block of code that contains settings for the hex grid. Set the id selector the top of the code block to match the id of the top-level parent div.
167 |If there will be multiple unique hex grids, duplicate this code block for each unique id.
168 | 169 |SCSS (_hex-style.scss)
171 |#myHexGrid {
172 | $gridWidth: 20em;
173 | $gridHeight: auto;
174 | $columnCount: 3;
175 | $rowCount: 2;
176 | $hexCount: auto;
177 | $hexLayout: row;
178 | $gridOrient: vertical;
179 | $crop: none;
180 | $cropFactor: 1;
181 | $hexContent: auto;
182 | $hexSize: auto;
183 | $hexMargin: 0.5em;
184 | $hexShape: hexagon;
185 | $hexColor: #48a999;
186 | $breakpoints: none;
187 | $images: none;
188 |
189 | // ...
190 |
191 | }
192 | Customization
195 |The grid can be assigned custom css from inside the hex-style codeblock.
196 |Hexi-Flexi Grid assigns unique class names to each individual column, row and cell in the hex grid.
197 |
198 | .c-[n] targets every cell in column [n].
199 |
200 | .r-[n] targets every cell in row [n].
201 |
202 | .c-[n1].r-[n2] targets the cell located at column [n1], row [n2].
SCSS (_hex-style.scss)
206 |#myHexGrid {
207 |
208 | // ...
209 |
210 | margin: 2em;
211 | // create 2em margin around the grid
212 |
213 | .c-1 {background-color: red;}
214 | // column 1 cells will be red
215 |
216 | .r-2 {transform: scale(0.8);}
217 | // row 2 cells will be 80% as large
218 |
219 | .c-3.r-1 {opacity:0.5;}
220 | // make column 3, row 1 50% opaque
221 |
222 | // ...
223 |
224 | }
225 | Grid Settings
229 |230 |
$gridWidth
231 |Set the width of the hex grid.
232 |Value | 236 |Description | 237 |
---|---|
'auto' | 242 |The grid width will match the ratio of $gridHeight. When set to auto, $gridHeight must be explicitly set. | 243 |
length | 246 |Explicitly set the height. Note: percent width is not supported. | 247 |
251 |
$gridHeight
252 |Set the height of the hex grid.
253 |Value | 257 |Description | 258 |
---|---|
'auto' | 263 |The grid height will match the ratio of $gridWidth. When set to auto, $gridWidth must be explicitly set. | 264 |
length | 267 |Explicitly set the height. Note: percent height is not supported. | 268 |
272 |
$columnCount
273 |Set the number of columns in the hex grid.
274 |Value | 278 |Description | 279 |
---|---|
'auto' | 284 |The column count will match the number of cells divided by the number of rows. When set to auto, $rowCount and $hexCount must be explicitly set. | 285 |
number | 288 |Explicitly set the number of columns. | 289 |
293 |
$rowCount
294 |Set the number of rows in the hex grid.
295 |Value | 299 |Description | 300 |
---|---|
'auto' | 305 |The row count will match the number of cells divided by the number of columns. When set to auto, $columnCount and $hexCount must be explicitly set. | 306 |
number | 309 |Explicitly set the number of rows. | 310 |
314 |
$hexCount
315 |Set the number of hex cells in the hex grid.
316 |Value | 320 |Description | 321 |
---|---|
'auto' | 326 |The cell count will match the number of columns times the number of rows. When set to auto, $rowCount and $columnCount must be explicitly set. | 327 |
number | 330 |Explicitly set the number of cells. | 331 |
335 |
$hexLayout
336 |Set the direction the cells will populate the grid.
337 |Value | 341 |Description | 342 |
---|---|
'row' | 347 |Cells will populate the grid starting at the top left corner and filling in horizontally, spilling over into the next row once each has filled. | 348 |
'column' | 351 |Cells will populate the grid starting at the top left corner and filling in vertically, spilling over into the next column once each has filled. | 352 |
356 |
$gridOrient
357 |Set the orientation in which the grid cells will align.
358 |Value | 362 |Description | 363 |
---|---|
'vertical' | 368 |Columns will run in straight lines, rows will offset by one half cell. | 369 |
'horizontal' | 372 |Rows will run in straight lines, rows will offset by one half cell. | 373 |
377 |
$crop
378 |Set whether the grid will be rectangularly cropped.
379 |Value | 383 |Description | 384 |
---|---|
'none' | 389 |The grid will be uncropped. | 390 |
'crop' | 393 |The grid will be scaled by the factor set with $cropFactor and overflow is hidden. | 394 |
398 |
$cropFactor
399 |Set the amount the grid will be scaled when it is cropped.
400 |Value | 404 |Description | 405 |
---|---|
number | 410 |Explicitly set the crop-factor. A value of 1.2 will scale the grid by 120%. | 411 |
415 |
$hexContent
416 |Set the behavior of the cell content.
417 |Value | 421 |Description | 422 |
---|---|
'auto' | 427 |Cell content will stretch to cover the entire size of the cell, minus the value of $hexMargin. | 428 |
'center' | 431 |Cell content will be centered, and will match the value of $hexSize. $hexMargin will be ignored. | 432 |
436 |
$hexSize
437 |Set the size of the cells.
438 |Value | 442 |Description | 443 |
---|---|
'auto' | 448 |Cells will take up 100% of the available space. | 449 |
number | 452 |Explicitly set the cell size. Note: percent size is not supported. | 453 |
457 |
$hexMargin
458 |Set the size of the margins on the cells.
459 |Value | 463 |Description | 464 |
---|---|
number | 469 |Explicitly set the cell margin. Note: percent size is not supported. | 470 |
474 |
$hexShape
475 |Set the shape of the clip-mask on the cells.
476 |Value | 480 |Description | 481 |
---|---|
'hexagon' | 486 |Cells will be hexagonal. | 487 |
'circle' | 490 |Cells will be circular. | 491 |
495 |
$hexColor
496 |Set the background color of the cells.
497 |Value | 501 |Description | 502 |
---|---|
color | 507 |Accepts hexcode, rgb/a and named colors. | 508 |
512 |
$images
513 |Set a list of files to be passed to the hex cells as background images. The files will be added to the grid according to the order specified by $gridLayout.
514 |A value of 'none' will disable $images.
515 |SCSS (_hex-style.scss)
517 |#myHexGrid {
518 |
519 | // ...
520 |
521 | $images:
522 | 'path/to/image-1.jpg'
523 | 'path/to/image-2.jpg'
524 | 'path/to/image-3.jpg'
525 | 'path/to/image-4.jpg'
526 | 'path/to/image-5.jpg'
527 | 'path/to/image-6.jpg'
528 | ;
529 |
530 | // ...
531 |
532 | }
533 |