Search code examples
htmlcssrxaringan

Create an Image Grid in Xaringan


I'm trying to learn a bit more CSS/HTML to customize xaringan slides and could use some help.

I would like to place a number of GIF's in an image grid like the one shown here into a xaringan slide.

enter image description here

I know that normally one can display two images side-by-side by either using .pull-left[] & .pull-right[] or by defining sections with custom CSS like this:

.left-code {
  color: #777;
  width: 38%;
  height: 92%;
  float: left;
}
.right-plot {
  width: 60%;
  float: right;
  padding-left: 1%;
}

Then one would put the images in a xaringan slide with R code like this:

.pull-left[
<img src=figure1.jpg>
]

.pull-right[
<img src=figure2.jpg>
]

For an image grid the custom CSS would be:

.row {
  display: flex;
  flex-wrap: wrap;
  padding: 0 4px;
}

/* Create two equal columns that sits next to each other */
.column {
  flex: 50%;
  padding: 0 4px;
}

.column img {
  margin-top: 8px;
  vertical-align: middle;
}

However, it also includes HTML code that specifies all of the images so I'm not quite sure how to integrate the two in a xaringan slide.

<div class="row">
  <div class="column">
    <img src="wedding.jpg">
    <img src="rocks.jpg">
    <img src="falls2.jpg">
    <img src="paris.jpg">
    <img src="nature.jpg">
    <img src="mist.jpg">
    <img src="paris.jpg">
  </div>
  <div class="column">
    <img src="underwater.jpg">
    <img src="ocean.jpg">
    <img src="wedding.jpg">
    <img src="mountainskies.jpg">
    <img src="rocks.jpg">
    <img src="underwater.jpg">
  </div>
  <div class="column">
    <img src="wedding.jpg">
    <img src="rocks.jpg">
    <img src="falls2.jpg">
    <img src="paris.jpg">
    <img src="nature.jpg">
    <img src="mist.jpg">
    <img src="paris.jpg">
  </div>
  <div class="column">
    <img src="underwater.jpg">
    <img src="ocean.jpg">
    <img src="wedding.jpg">
    <img src="mountainskies.jpg">
    <img src="rocks.jpg">
    <img src="underwater.jpg">
  </div>
</div>

Solution

  • You can do this using the extension theme here. This theme is included the later version of xaringan so you can specify in the YAML by css: "ninjutsu". The layout should look like below:

    ---
    title: "Split to grid in `xaringan`"
    output:
      xaringan::moon_reader:
        css: ["ninjutsu"]
    ---
    
    
    class: split-four
    
    .column[
    <img src="wedding.jpg">
    <img src="rocks.jpg">
    <img src="falls2.jpg">
    <img src="paris.jpg">
    <img src="nature.jpg">
    <img src="mist.jpg">
    <img src="paris.jpg">
    ]
    
    .column[
    <img src="underwater.jpg">
    <img src="ocean.jpg">
    <img src="wedding.jpg">
    <img src="mountainskies.jpg">
    <img src="rocks.jpg">
    <img src="underwater.jpg">
    ]
    
    .column[
    other images
    ]
    
    .column[
    other images
    ]