Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding an Absolutely Positioned Header to Reveal.js

DZone's Guide to

Adding an Absolutely Positioned Header to Reveal.js

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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 his my 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>
</section>

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.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}