JohnSG JohnSG - 1 year ago 136
R Question

slidify - how to position an image?

I'm experimenting with using slidify for making html5 presentations. While the use of markdown for creating bullets and text is clear, I'm unsure how to work with images. I can resize and such using imagemagick, but how do I center (or flush top/right/bottom) and image using markdown?


I'm referring generally to images here, but this also applies to R graphics. The default appears to be centering images - I'd like to be able to place them side-by-side, or even in arbitrary locations.

Answer Source

Here is an (ugly) work-around to center and resize images to make sure they all fit on your slides.

Add the following to the top of your index.Rmd, just below the description block.

<!-- Limit image width and height -->
<style type='text/css'>
img {
    max-height: 560px;
    max-width: 964px;

<!-- Center image on slide -->
<script src=""></script>
<script type='text/javascript'>
$(function() {

The CSS will limit the height of images to avoid having them overflow.

The JS will load jQuery and then find all paragraph elements which contain an <img> element, and add the .centered class, which sets text-align: center.

The same version of jQuery is loaded later on by Slidify, but I couldn't find a straight-forward way to load the <script> block after Slidify has loaded jQuery.

Alternatively, you could also manually achieve this using plain HTML:

<div style='text-align: center;'>
    <img height='560' src='x.png' />

However, you would need to do this for each image.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download