Generate SVG Skeleton of a Web Page Using JavaScript

Generate SVG Skeleton of a Web Page Using JavaScript

XHR and its related features have been commonly used in recent decades. Since then, more and more Web sites now represent as Web Apps. To improve user experience with progressive loading, many Apps use skeleton screen to ease the feelings of unknown, rather than serve a blank white screen while waiting.

What is a Skeleton Screen

It’s the gray shapes shown while the data is loading from the server instead of displaying a classical spinner. —-^What is a skeleton screen 💀?

⬆️ Look at This ⬆️

What for

Look at this comparison, UI with skeleton seems to have less reflow which I think is more comforting.

This image referred from ^What is a skeleton screen 💀?

Types of Skeletons

Element + CSS



Just a different type of image representation depends on what your designers gave. Used as background image or source of an image element.


The Structure of a SVG file is very similar to a HTML file because they both are XML-based file format. Also, a SVG file can be rendered directyly in a HTML without using <img> or background-image CSS.

Here is a example for rendering a rectangle:

<div style="width: 100px; height: 100px; background-color: red;">
<svg width="100" height="100" xmlns="">
<rect width="100" height="100" fill="red" />

For more attributes, check

Easy, right?

How to Generate SVG from HTML

Full code in

  1. Foreach HTML DOM tree nodes, from top to bottom, from outside to inside, collect sizes and positions
  2. Add rect node to SVG File when DOM node is a leaf node or it’s has background color
  3. Wrap these rect nodes in a <svg> tag
  4. Done~

Visualized process looks like this ⬇️:

Some nodes should be skipped, such as:

  1. Non-media element with no background (most of them are placeholder elements)
  2. <a>with no style or with only 1 child element

Generate SVG Skeleton of a Web Page Using JavaScript



Posted on


Licensed under