Vintage Terminal

Vintage Terminal Effect in CSS3

Rudy Jahchan ·

Recently I revamped my personal website. For the most part I kept it simple, having it generated by Middleman with styling and layout provided by Bootstrap. However, I wanted my header to reflect my love of the command-line and 80s nostalgia by having it act like a vintage terminal similar to Cathode app: scanlines, screen burn, green glow, with blinking cursor. Most importantly, I wanted to do it with CSS3 animations and no Javascript to minimize the impact on browsers. Building on the work of Lea Verou and Anders Evenrud, here’s how I did it.

Typing and Blinking in steps()

First thing I wanted was to have my name be “typed” out in the header on page load by a blinking cursor. Lea Verou covered exactly how to pull this off. It’s basically a “sliding door” effect where an element initially completely covering the text has its width shrunk to slowly reveal the content. The animation happens in discrete steps (literally using the steps() CSS3 timing function) so that after each step only one character is revealed. Every step will decrease the width by the same amount, so to ensure only a single character is revealed at a time a monospaced font (where every character has the same fixed width) will be used. Finally, the blinking block cursor is achieved by setting the covering element’s left border to that same fixed width and animating it’s color. Let’s see the code. The text is wrapped in an element (an anchor tag in my case) and a span playing the role of the covering element described above is appended with an empty space.

<br>
<a href="http://rudyjahchan.com">Rudy Jahchan <span class="cursor"> </span></a><br>

In the CSS, the anchor is set to have the monospace font and the span is absolute-ly positioned, anchoring it to the top, bottom, and left of its containing element. This is done so when the width changes, it shrinks towards the right side.

<br>
a {<br>
    position: relative;<br>
    font-family: monospace;<br>
    background-color: black;<br>
    text-decoration: none;<br>
    color: green;<br>
    font-size: 1.5em;<br>
}

.cursor {<br>
    position: absolute;<br>
    top: 0;<br>
    bottom: 0;<br>
    right: 0;<br>
    width: 0;<br>
    background; black;<br>
}<br>

A CSS3 animation is defined for the beginning and end state of the typing. At the start the span 100% matches the width of its parent to completely hide the text. The animation ends with the width set to 0 so that all the text is revealed (again, it’s a sliding door).

<br>
/* Need to add prefix variants */<br>
@keyframes typing {<br>
    from { width: 100%; }<br>
    to { width: 0; }<br>
}<br>

Then the animation is applied to the cursor with an appropriate duration and made to repeat infinite-ly.

<br>
.cursor {<br>
  // ...

  // Need to add vendor prefix variants<br>
  animation: typing 6s infinite;<br>
}<br>

At this point the animation smoothly reveals the text. We now add the steps() call, one for each character.

<br>
.cursor {<br>
  // ...

  // Need to add vendor prefix variants<br>
  animation: typing 6s steps(13, end) infinite;<br>
}<br>

Now for the blinking cursor. As described before, the cursor is actually the left border of the span, its size set to match a single character width of the chosen (again, monospaced!) font. This is the one thing I had to experiment with to get right as it can vary depending on the font used. Also, it’s very important that the box-sizing strategy of the element is set to border-box to ensure the border is drawn within the span element.

<br>
.cursor {<br>
    // ...

    // Need to add vendor prefix variants<br>
    box-sizing: border-box;<br>
    border-left: .5em solid;<br>
}<br>

Another animation is defined for the blinking that takes the border color from transparent to the cursor color and back.

<br>
@keyframes blinking {<br>
    from, to { border-color: transparent; }<br>
    50% { border-color: green; }<br>
}<br>

This animation is applied to the same span and set to toggle to step to the end state (step-end) within 1 second repeatedly.

<br>
.cursor {<br>
  // ...

  // Need to add vendor prefix variants<br>
  animation: typing 6s steps(13, end) infinite, blinking 1s step-end infinite;<br>
}<br>

And voila, my name is typed out with a blinking cursor.

Screen-burning

The next task was to get that glorious old school scan-line feel. Anders Evenrud created and amazing demo of a vintage terminal which I borrowed liberally from. The technique consists of structuring a number of div elements and their :before and :after pseudo elements to produce the entire effect.

<br>
<div id="faux-terminal"><br>
  <div class="layer"></div><br>
  <div class="overlay"></div><br>
</div><br>

First there is the outer “screen” layer of faux-terminal. The fixed scanlines are drawn by styling the :before pseudo element to fill it’s background with a linear gradient. However, it also declares it’s background size as being only 4px size and repeating, thus creating the lines! The whole thing is given a high z-index to float over everything and a subtle pulse animation.

<br>
#faux-terminal:before {<br>
  // ... positioning<br>
  z-index: 4010;<br>
  background: linear-gradient(#444 50%, #000 50%);<br>
  background-size: 100% 4px;<br>
  background-repeat: repeat-y;<br>
  opacity: .14;<br>
  box-shadow : inset 0px 0px 1px 1px rgba(0, 0, 0, .8);<br>
  animation: pulse 5s linear infinite;<br>
}

@keyframes pulse {<br>
  0%   {transform: scale(1.001);  opacity: .14; }<br>
  8%   {transform: scale(1.000);  opacity: .13; }<br>
  15%  {transform: scale(1.004);  opacity: .14; }<br>
  30%  {transform: scale(1.002);  opacity: .11; }<br>
  100% {transform: scale(1.000);  opacity: .14; }<br>
}<br>

The :after pseudo element then adds a light “sheen” through a radiant gradient and box-shadow, the latter inset to have the effect within the element’s boundaries.

<br>
#faux-terminal:after {<br>
  // ... positioning<br>
  z-index : 4011;<br>
  background-color : $rudy-accent-color;<br>
  background: radial-gradient(ellipse at center, rgba(0,0,0,1) 0%,rgba(0,0,0,0.62) 45%,rgba(0,9,4,0.6) 47%,$rudy-accent-color 100%);<br>
  box-shadow : inset 0px 0px 4px 4px rgba(100, 100, 100, .5);<br>
  opacity : .1;<br>
}

Next up is another layer for the actual green burn, achieved by a radient gradient from our terminal green to a dark grey and it’s own subtle glitching animation.

<br>
.layer {<br>
  // ... positioning<br>
  z-index : 4001;<br>
  box-shadow : inset 0px 0px 1px 1px rgba(64, 64, 64, .1);<br>
  background: radial-gradient(ellipse at center,darken($rudy-accent-color,1%) 0%,rgba(64,64,64,0) 50%);<br>
  transform-origin : 50% 50%;<br>
  transform: perspective(20px) rotateX(.5deg) skewX(2deg) scale(1.03);<br>
  animation: glitch 1s linear infinite;<br>
  opacity: .9;<br>
}

.layer:after {<br>
  // ... positioning<br>
  background: radial-gradient(ellipse at center, rgba(0,0,0,0.5) 0%,rgba(64,64,64,0) 100%);<br>
  opacity: .1;<br>
}

@keyframes glitch {<br>
  0%   {transform: scale(1, 1.002); }<br>
  50%   {transform: scale(1, 1.0001); }<br>
  100% {transform: scale(1.001, 1); }<br>
}<br>

Finally, we animate the scanline with an overlay whose :before pseudo element is fixed 5px tall and animated to repeatedly slide down.

<br>
.overlay {<br>
  // ... positioning<br>
  z-index: 4100;<br>
}

.overlay:before {<br>
  content : '';<br>
  position : absolute;<br>
  top : 0px;<br>
  width : 100%;<br>
  height : 5px;<br>
  background : #fff;<br>
  background: linear-gradient(to bottom, rgba(255,0,0,0) 0%,rgba(255,250,250,1) 50%,rgba(255,255,255,0.98) 51%,rgba(255,0,0,0) 100%); /* W3C */<br>
  opacity : .1;<br>
  animation: vline 1.25s linear infinite;<br>
}

.overlay:after {<br>
  // ... positioning<br>
  box-shadow: 0 2px 6px rgba(25,25,25,0.2),<br>
              inset 0 1px rgba(50,50,50,0.1),<br>
              inset 0 3px rgba(50,50,50,0.05),<br>
              inset 0 3px 8px rgba(64,64,64,0.05),<br>
              inset 0 -5px 10px rgba(25,25,25,0.1);<br>
}

@keyframes vline {<br>
  0%   { top: 0px;}<br>
  100% { top: 100%;}<br>
}<br>

Now, here is where my implementation differs from Anders Evenrud’s. His layer structures wrap a text area containing the “output” of his virtual console. In my case, however, I wanted this effect to go over a Bootstrap navigation bar component. I did not want to go down the rabbit hole of restyling or redefining the component and decided to (somewhat hackely) absolute-ly position all the terminal layers over the toolbar. Since I was using SASS it was easy enough to make a mixin to help DRY-up the code. However, I ran into a huge problem. The virtual screen blocked all interactions with the navigation bar! This was easily remedied by setting the pointer-events property of the screen layer to none. All events are now passed through to the navigation bar. Whew! As you can see, a lot of CSS3 properties and functions were used along the way. While most are fully supported by all the major browsers some still require [vendor prefixing][], as noted in some of the sample code. Maintaining this long-term can be frustrating as the slightest change will need to be replicated. SASS mixins can alleviate some of these concerns but even better is autoprefixer. It parses your CSS and adds the necessary prefixes. Obviously you would not want to do this for every request in production but it’s perfect for static site generation and a snap to integrate.

Final Thoughts

One of the reasons I went with an all CSS3 solution was to avoid having an ongoing Javascript process driving the animation and taking up resources. That said, I want to investigate what is the impact of all these effects on performance. Regardless, it was great fun to implement and has me wanting to tackle other great 80s interfaces. Maybe the motion tracker from Alien or the Blade Runner zoom-and-enhance?


Now hiring developers, designers and product managers.
Apply now: www.carbonfive.com/careers
Rudy Jahchan
Rudy Jahchan

Rudy’s fascination with computer programming began at age 10 when he mistakenly picked up the Micro-Adventure book Space Attack; he thought it was going to be about Star Wars. That happy accident led him to graduate from McGill University in Computer Science and start a 12 year career in software development playing with a wide range of technology; everything from web applications to cryptology to NoSQL.