Skip to content

Instantly share code, notes, and snippets.

@brehaut
Last active June 20, 2024 04:00
Show Gist options
  • Save brehaut/7051259 to your computer and use it in GitHub Desktop.
Save brehaut/7051259 to your computer and use it in GitHub Desktop.
Responsive grid overlay with bookmarklet

Responsive grid overlay

A scruffy script and bookmarklet to draw the vertical component of a grid over a webpage.

Configuration

Define a javascript object in your page net.brehaut.grid. It must have the following keys:

  • columns: The number of grid columns to render
  • containerID: The ID of an element that contains the content of the page. This element will be used to position the grid and determine its width.

Optionally (but recommended) also specify:

  • nominalWidth: The number of pixels to use as the basis for calculating column widths. Defaults to 960.
  • nominalGutter: The number of pixesl for a column gutter in proportion to the nominalWidth. Each grid column has its own gutters on each side. For example, with a nominal gutter of 2, the space between columns would be 4. Defaults to 5.
  • script: The location of the script to use. Defaults to an early version of this Gist. Highly recommended you specify your own version.

Usage

Once a site has a grid configuration set up, copy the bookmarklet (below) into a bookmark. Visit the site, click the bookmark.

javascript:void%20%28function%20%28%29%20%7B%20%20%20%20%20%20%22use%20strict%22%3B%20%20%20%20%20%20var%20script%20%3D%20document.createElement%28%22script%22%29%3B%20%20%20%20%20%20script.language%20%3D%20%22text/javascript%22%3B%20%20%20%20%20%20script.src%20%3D%20%28%28%28window.net%20%7C%7C%20%7B%7D%29%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.brehaut%20%7C%7C%20%7B%7D%29%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.grid%20%7C%7C%20%7B%7D%29%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.script%20%7C%7C%20%22https%3A//gist.github.com/brehaut/7051259/raw/45c652dcefb8526ec57d14310b787fbbadd079f4/gridder.js%22%3B%20%20%20%20%20%20document.body.appendChild%28script%29%3B%20%20%7D%29%28%29%3B
void (function () {
"use strict";
var script = document.createElement("script");
script.language = "text/javascript";
script.src = (((window.net || {})
.brehaut || {})
.grid || {})
.script || "https://gist.github.com/brehaut/7051259/raw/45c652dcefb8526ec57d14310b787fbbadd079f4/gridder.js";
document.body.appendChild(script);
})();
(function () {
"use strict";
var conf = ((net || {}).brehaut || {}).grid;
if (!(conf && conf.columns && conf.containerID)) return;
conf.nominalWidth = conf.nominalWidth || 960;
conf.nominalGutter= conf.nominalGutter || 1;
var gutterWidth = (conf.nominalGutter / conf.nominalWidth)
* 100;
var columnWidth = (((conf.nominalWidth - (conf.nominalGutter
* 2
* conf.columns))
/ conf.columns)
/ conf.nominalWidth)
* 100;
var el = document.getElementById(conf.containerID);
var grid = document.createElement("div");
grid.style.position = "fixed";
grid.style.top = 0;
grid.style.bottom = 0;
grid.style.zIndex = 1000;
grid.style.outline = "1px dotted rgba(0,0,0,0.5)";
grid.style.marginLeft = -0.5 * gutterWidth + "%"
for (var i = 0; i <= conf.columns; i++) {
var col = document.createElement("div");
col.style.position = "absolute";
col.style.top = 0;
col.style.bottom = 0;
col.style.width = gutterWidth * 2 + "%";
col.style.left = ((gutterWidth * 2 + columnWidth) * i) + "%";
col.style.background = "rgba(0,0,0,0.05)";
grid.appendChild(col);
}
function resize() {
grid.style.left = el.offsetLeft + "px";
grid.style.width = el.offsetWidth + "px";
}
document.body.appendChild(grid);
window.addEventListener("resize", resize);
resize();
})();
Copyright (c) 2013 Andrew Brehaut
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment