CSS Charts: How to Create a Horizontal Organizational Chart

In a previous tutorial, we learned how to create a CSS-only vertical organizational chart. Today, as some folks requested, we’ll go through the process of building its corresponding horizontal one. 

And here’s the twist; we’ll create this new chart without changing a single line of markup from the original, we’ll only modify the styles.

Keep in mind, as the concept remains the same, we’ll be grabbing some parts from the other tutorial. Ready to test your CSS skills again?

The Organizational Chart We’re Building

Here’s the CSS chart we’ll be creating:

On screens up to 1300px wide, the chart will appear in a vertical layout. On larger screens, its data will appear horizontally. Be sure to check this behavior by opening the demo on a large screen and resizing your browser window. 

1. Specify the Container

Our chart will live inside a container:

2. Define Some Basic Styles

Before examining its levels, we’ll set up a few reset rules and helper classes:

Notice the rectangle class. We’ll append this to every node/element of our chart.

Additionally, keep an eye on the container styles. Thanks to CSS Grid, we can divide the chart into two parts. The first one will include the first level, while the second one all the others. Plus, we’ll give the container a maximum width that will affect only on smaller screens.

The parts of chart container

Note: for simplicity, I haven’t optimized the CSS. This will help you get a better understanding of the styles of each level. 

3. Specify the Levels

At this point, we’ll have a closer look at the chart levels. As you might remember from the previous tutorial, we’ll have four of them:

The chart levels

Level #1

The first level will only include a single node:

Level 1

HTML

To describe it, we’ll use an h1 tag as it’s the most important part of our chart:

CSS

We’ll use its ::before pseudo-element to create a relationship between the first and second levels:

Level #2

The second level will consist of two nodes:

Level 2

HTML

To describe it, we’ll use an ordered list with two list items. Each list item will contain an h2 element:

CSS

Each list item will act as a grid container that will contain two columns. We’ll position the contents of its first one on the top edge of the column axis, while the contents of its last one on the bottom edge of the column axis.

We’ll also define the ::before and ::after pseudo-elements of the h2 elements. Their job will be to create the associations between the adjacent levels:

Level #3

The third level will include four nodes.

We’ll associate the first two nodes with the first node of the second level, while the last two with its second node: 

Level 3

HTML

Still, inside the initial list where the second level lives, we’ll define two new lists. Each one of them will contain two list items. For each item will specify an h3 element:

CSS

Similarly to the previous level, we’ll consider each list item as a grid container and split it into two columns.

In the same way, we’ll set the ::before and ::after pseudo-elements of the h3 elements for creating the required connections:

Note: if your text nodes span onto a separate line, you have to change the following hardcoded values: top: 34px and top: -34px.

Level #4

We’ll need sixteen nodes for the fourth level. These will equally be distributed into four lists.

Each third-level node will include one list:

Level 4

HTML

Still, inside the initial list where the second level lives, we’ll define four new lists. Each one of them will contain four list items. For each item we’ll specify an h4 element:

CSS

Once again, we’ll do the same actions. Firstly, we’ll use the CSS Grid to create the layout for the fourth-level nodes. Then, we’ll specify the required pseudo-element of target elements for establishing the connections: 

Note: if your text nodes span on a separate line, you have to change the top: 34px and top: -34px hardcoded values.

4. Going Responsive

As you have already guessed, we follow a desktop-first approach for our styles. Generally speaking, I prefer building for large screens first and then going down to smaller, less complicated ones.

On screens between 1301px and 1650px, the chart will still be in horizontal mode, yet its nodes will have a smaller width (200px instead of 150px).

On screens up to 1300px wide, all chart nodes will appear vertically, like this:

Responsive Layout of Horizontal Organization Chart

Unlike the horizontal layout, here the width and typography of the text nodes differ depending on their level. The higher the level, the larger the width and typography.

Of course, feel free to change these breakpoints depending on your content.

It’s worth noting that this design is slightly different compared to the responsive layout of the vertical chart:

Responsive Layout of Organization Chart

In case you’re wondering, there isn’t any specific purpose behind this new implementation. I just wanted to show you two different responsive layouts. Feel free to use any of these in your pages.  

Here are the responsive styles:

The Revert Keyword

From the styles above, keep an eye especially on the new revert CSS keyword that we apply to the headings. That will help us restore their default browser styles. Merrily enough, its browser support is growing every day. 

Conclusion

That’s all for today, folks! At this point, you have everything you need to build your own CSS-only organizational charts.

Indeed, in a real scenario, you might prefer to use a powerful JavaScript library like Highcharts.js for creating such a chart. However, pushing the limits of CSS without ignoring its limitations is always a great way to learn and expand your knowledge. Moreover, with the combination of tools/features like the CSS Grid and pseudo-elements you can accomplish really neat things.

Let’s remind ourselves of what we built:

Go ahead, fork the demo and get your hands dirty. Don’t be afraid to destroy and experiment with it. You can achieve different variations without putting in too much effort. For example, here’s another quick alternative for desktop screens:

Another implementation for the horizontal organizational chart

Last but not least, here at Tuts+ there are a lot of detailed tutorials showing how to implement different kinds of CSS charts (sometimes with JavaScript). Be sure to have a look, and as always, thanks a lot for reading!

Leave a comment

Your email address will not be published.