Quickly Create (Mostly) Responsive HTML Columns With {htmltools}

I had need to present a wall-of-text to show off a giant list of SSL certificate alternate names and needed the entire list to fit on one slide (not really for reading in full, but to show just how many there were in a way that a simple count would not really convey).

Keynote, PowerPoint, and gslides all let you make tables or draw boxes but I really didn’t want to waste time fiddling as much as I’d need to with those tools just for this one slide.

Thankfully, I remembered that HTML5 <div> elements can be styled with a column-count attribute and we can use {htmltools} to make quick work of this task.

To show it off, first we’ll need some words, so let’s make some using stringi::stri_rand_lipsum():

library(stringi)library(htmltools)library(tidyverse)set.seed(201912)stri_rand_lipsum(5) %>%  stri_paste(collapse = " ") %>%  stri_split_boundaries() %>%  flatten_chr() %>%  stri_trim_both() -> wordshead(words)## [1] "Lorem"  "ipsum"  "dolor"  "sit"    "amet,"  "sapien"length(words)## [1] 514

Now, we’ll make a function — columnize() — that we can reuse in the future and have it take in a character vector, the column count we want and some CSS styling, then use some {htmltools} tag functions to make quick work of this task:

columnize <- function(words, ncol = 5,                      style = "p { font-family:'Roboto Condensed';font-size:12pt;line-height:12.5pt;padding:0;margin:0}") {  tagList(    tags$style(style[1]),    tags$div(      words %>%        map(tags$p) %>%        tagList(),      style = sprintf("column-count:%d", as.integer(ncol[1]))    )  )}

In this function we turn the style param into a <style> section in the generated HTML, then turn words into <p> tags wrapped in <div>.

This function can be used in a R Markdown code block (set block parameters to results='markup') to have the columns appear automagically in the resultant HTML document output. You can also use it in standalone fashion by using html_print() on the results:

html_print(  columnize(words, 10))

10 columns of example text made with R

The above is an image just for easier blog display purposes. You can test out a working example from a spun R script over at that has some different column count examples. Grow and shrink the browser width to see how the columns shrink and grow with it.


Hopefully this helps others save time and effort like it did for me today. You can experiment with making the columnize() function more robust by having it work with all the other column-formatting properties:

  • column-count: Specifies the number of columns an element should be divided into
  • column-fill: Specifies how to fill columns
  • column-gap: Specifies the gap between the columns
  • column-rule: A shorthand property for setting all the column-rule-* properties
  • column-rule-color: Specifies the color of the rule between columns
  • column-rule-style: Specifies the style of the rule between columns
  • column-rule-width: Specifies the width of the rule between columns
  • column-span: Specifies how many columns an element should span across
  • column-width: Specifies a suggested, optimal width for the columns
  • columns: A shorthand property for setting column-width and column-count

You can find out more about these properties (and play with some examples) over at

*** This is a Security Bloggers Network syndicated blog from authored by hrbrmstr. Read the original post at: