Twitter: raymondcamden

Address: Lafayette, LA, USA

Adding an Absolutely Positioned Header to Reveal.js

04-01-2014 3,269 views Misc 3 Comments

I'm a big fan of reveal.js. It is a lightweight HTML-based presentation framework that just works well for me. I switched over to it a while ago and enjoy working with it. Every now and then though I run into an issue that is a bit hard to handle with it. Yesterday I was working on my cfObjective presentation and created the slide you see below.

As you can see, the most important part of the slide is covered by the header. This is unacceptable! Unfortunately, it is difficult to reposition the h1 (or h2) element within a Reveal.js slide. If you set the position to absolute and use 0/0 for top and left, you end up at the top left of the section element, not the top left of the screen. You can use negative values, but that felt wrong to me, and I was worried about what would happen on a smaller screen when I presented.

I raised the issue on Twitter (where all important things should be discussed, am I right?) and Rey Villalobos (@planetoftheweb) came up with a good solution. He was ok with me sharing this on my blog, but note that any mistakes in the description are entirely hismy fault.

First, you need to add an element to handle the header, but place it outside the div for your slides.

<!-- In between the <div="reveal"> and the <div class="slides">-->
<header style="position: absolute;top: 50px; left: 100px; z-index:500; font-size:100px;background-color: rgba(0,0,0,0.5)"></header>
<!-- In between the <div="reveal"> and the <div class="slides">-->

(Note - those top and left values are a bit arbitrary. Ditto for the RGBA color.)

Then - for each slide - use CSS to specify the content for the element.

<section data-state="header1">
<style>.header1 header:after { content: "Header 1 Example"; }</style>
<p>header1 example</p>

Note - when you add a state attribute to a slide in Reveal, the framework automatically applies the state name to the class of the div wrapping all the slides, which means it also impacts that earlier header. As soon as you leave that slide, the class is removed, which means the content dynamically added via CSS (you did know you could do that, right?) is also removed.

Here is the updated slide.

There is one small caveat to this post that I don't have an answer for yet (but check the comments, going to bug Rey until he can figure out a fix) - how to modify the position of the header so it is right aligned. You could probably just use a second DOM element (a div perhaps) but that seems wrong.


  • Commented on 04-01-2014 at 11:45 AM
    Wow...Brony slides all the way! But I digress.

    You could do two headers by creating headers with two different for left and one for right. You can put this right between the div with the reveal and slides class.

    <header class="left" style="position: absolute; left: 10px;"></header>
    <header class="right" style="position: absolute; right: 10px;"></header>

    Then whenever you want to create a slide with an element to the right. you use

    <section data-state="headright">
    <style>.headright header.right:after { content: "headline"; }</style>

    and an element to the left

    <section data-state="headleft">
    <style>.headleft header.left:after { content: "headline"; }</style>

    Here's a gist with it working.
  • Commented on 04-01-2014 at 1:10 PM
    Great tip Ray. Here's a related question. How do you make an image full screen? Everytime I add and image it is centered and boxed.
  • Commented on 04-01-2014 at 1:14 PM
    Use data-background="..". You can also do width too. Details are on the reveal.js readme:

    (CTRL+F for Slide Backgrounds)

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty