Giter Club home page Giter Club logo

responsive-grid-shapes's Introduction

Hexagons and Beyond: Flexible, Responsive Grid Patterns without Media Queries

From my article: https://css-tricks.com/hexagons-and-beyond-flexible-responsive-grid-patterns-sans-media-queries/

Responsive grid shapes

Create a CSS-only responsive grid without using media query. It does support different shapes (Octagon, Hexagon, Rhombus, etc). It's built using inline-block elements and float (yes, the old float!) combined with shape-outside. You can easily adjust the different parameters using CSS variables

How to use

Each item of the grid is a single div (where you can put any content) and we need 2 wrapper. The HTML code looks like below:

<div class="main">
  <div class="container">
    <div>...</div>
    <div>...</div>
    <div>...</div>
    ..
  </div>
</div>

For the CSS we have the following. All you have to do is to update a few CSS variables to control the grid.

.main {
  --s: 100px; /* size of the shape */
  --r: 1;     /* ratio */
  --mv: 4px;  /* vertical margin  */
  /* clip-path parametres*/
  --h: 0.25;  
  --v: 0.35; 
  /**/
  
  /* You don't need to touch the below */
  --hc: calc(clamp(0,var(--h),0.5) * var(--s)) ;
  --vc: calc(clamp(0,var(--v),0.5) * var(--s) * var(--r)); 
  --mh: calc(var(--mv) + (var(--s) - 2*var(--hc))/2); /* horizontal margin */
  --f: calc(2*var(--s)*var(--r) + 4*var(--mv)  - 2*var(--vc) - 2px);
  /**/
  display:flex;
}

.container {
  font-size: 0;
}
.container div {
  width: var(--s);
  margin: var(--mv) var(--mh);
  height: calc(var(--s)*var(--r)); 
  display: inline-block; /* or inline-grid, inline-flex, etc */
  font-size:initial;
  clip-path: polygon(var(--hc) 0, calc(100% - var(--hc)) 0,100% var(--vc),100% calc(100% - var(--vc)), calc(100% - var(--hc)) 100%,var(--hc) 100%,0 calc(100% - var(--vc)),0 var(--vc));
  margin-bottom: calc(var(--mv) - var(--vc)); 
}
.container::before {
  content: "";
  width: calc(var(--s)/2 + var(--mh));
  float: left;
  height: 135%;
  shape-outside: repeating-linear-gradient( #0000 0 calc(var(--f) - 2px),#000  0 var(--f));
}

Find all the details in my CSS-tricks article

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.