Adding an Absolutely Positioned Header to Reveal.js

This post is more than 2 years old.

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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Ray Villalobos posted on 4/1/2014 at 8:45 PM

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.

Comment 2 by Simon MacDonald posted on 4/1/2014 at 10: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.

Comment 3 by Raymond Camden posted on 4/1/2014 at 10:14 PM

Use data-background="..". You can also do width too. Details are on the reveal.js readme:

(CTRL+F for Slide Backgrounds)

Comment 4 by Saki posted on 11/18/2014 at 8:02 AM

This does not seem to work if `parallaxBackgroundImage` is defined.

Comment 5 by Raymond Camden posted on 11/18/2014 at 4:52 PM

Thanks for the warning.

Comment 6 by Gregório posted on 11/4/2015 at 10:35 PM

Nice solution, thx for sharing!

I have had some trouble making it work with `data-markdown`, but the workaround is simple:

just put the <style> thing BEFORE the <section> tag

like this:


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




Comment 7 (In reply to #1) by Gregório posted on 11/11/2015 at 7:22 PM

what about BOTH in the same slide? is that possible?

Edit.: Yes, it is!

<style>.f1 footer:after { content: "f1"; }</style>
<style>.h1 header:after { content: "h1"; }</style>
<section data-state="f1 h1">
Comment 8 (In reply to #7) by Raymond Camden posted on 11/11/2015 at 11:18 PM

I still wish Reveal had an easier way of doing this.

Comment 9 by romanfroow posted on 1/21/2016 at 11:34 AM
Comment 10 (In reply to #9) by Raymond Camden posted on 1/21/2016 at 1:54 PM

Thanks for sharing that!