Colm provides responsive columns (like Pinterest or Masonry) with minimal setup. Supports current version of all major browsers.
For demos and more, visit Colm's website.
- Download colm.js or colm.min.js to your script directory.
- Add
<script type="text/javascript" src="path/to/colm.js"></script>anywhere in your HTML. - Add
data-colm-width="[desired column width in pixels]"to the container element or elements which you wish to convert into columns. - Optionally add
data-colm-align-columnsanddata-colm-align-itemsattributes to the container. See Advanced usage for details.
You are done. No other files to import, no javascript to call.
You can now use regular CSS, including @media queries to control the width of your container(s) and hence the actual number and width of columns. When the window is resized, colm checks if the number of column needs to be changed, and if so, lays out the elements again.
See the demo or visit the kitchensink to try out various settings.
Colm scans your HTML for elements containing the data-colm-width attribute, and lays out all their immediate children into the number of columns that best fit the desired column width. This is achieved by placing the appropriate number of simple <div> elements into the container to represent columns, and then looping through all the original children and placing each into the shortest column.
Colm relies on CSS3 flex box functionality. If it is not supported in the browser, Colm will quietly skip layouting and leave your HTML as is.
Use colm() to programmatically trigger layouting on the page. This is automatically called on page load and resize.
Use colm.appendTo to add new children to a container. Content can be a single element, an array or node list of elements or an HTML string.
Use the data-colm-align-columns attribute on the container element to control distribution of left over space, if the width of the container isn't an exact multiple of the column width.
Can have the following values:
leftaligns columns to the left of the containerrightaligns columns to the right of the containercentercenters columns horizontally within the containerjustifyadds equal space between columnsspaceadds equal space between and around columnsstretchstretches columns to fill the container
Use the data-colm-align-columns attribute on the container element to control distribution of left over space within each column.
Can have the following values:
topaligns items to the top of the columnbottomaligns items to the bottom of the columncentercenters items vertically within the containerjustifyadds equal space between itemsspaceadds equal space between and around itemsstretchstretches items to fill the column
Use the data-colm-min-width attribute on the container element if you want to allow your columns to shrink somewhat to fit another column in the container.
Use the data-colm-place attribute on child elements to force them into a particular column. Use negative numbers for counting from the right. The most useful values are "1" and "-1", especially when used on the first few children. This can be used to place content reliably in top left and right corners, and gracefully collapse it into a single column on narrower displays.
- Colm doesn't provide explicit spacing or gutters. Use CSS, especially
marginon child elements to control spacing. Alternatively, set your container widths exactly and usejustifyorspacecolumn alignment. - Colm doesn't resize the children in any way. Things will work best on all browsers if all the children are regular block elements.
- Colm changes your HTML by inserting a
<div>element between your container and its children (but may not run at all if e.g. JavaScript is disabled in a browser). Bear this in mind when constructing CSS selectors. - The container is assigned the attribute
data-colm-columnswith the current number of columns. Individual columns are assigned attributesdata-colm-column(column number), as well asdata-colm-first,data-colm-not-first,data-colm-lastanddata-colm-not-lastas appropriate. You can use these to style columns. Try things like:
#content > [data-colm-column] {
border-left: solid 1px silver;
}
#content[data-colm-columns="1"] .hide-on-narrow {
display:none;
}
#content > [data-colm-not-first] {
border-left: solid 1px silver;
}