Thursday, November 19, 2020

CSS Charts: How to Create an Organizational Chart

CSS Charts: How to Create an Organizational Chart

In previous tutorials we’ve learned how to create different types of charts including bar charts, thermometer charts, and donut charts. Today we’ll continue this journey by building a CSS-only organizational chart.

Ready to test your CSS skills?

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

It consists of four levels and describes the hierarchical structure of a company.

To find out what an organizational chart is, let's borrow Wikipedia's definition:

“An organizational chart , also called organigram or organogram, is a diagram that shows the structure of an organization and the relationships and relative ranks of its parts and positions/jobs. The term is also used for similar diagrams, for example ones showing the different elements of a field of knowledge or a group of languages.”

Here’s an example:

By S.s.kulkarni9090 - Own work, CC BY-SA 3.0

This type of chart is commonly used for presenting the relationships between the people or departments of a company. On a corporate website, you will probably find it on the “About Us” or “Company” page.

You’ll also see organizational charts used for family trees (check out the British Royal Family tree and line of succession on the BBC’s website). They’re ideally suited for illustrating hierarchy.

Our chart will live inside a container:

Before going through 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.

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

At this point, we’re ready to specify the chart levels; as we discussed earlier, here we’ll have four of them:

Levels

Each level will represent a role in a company starting from the highest-ranking one.

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

As we’ll see in a moment, each node will include other child nodes. These child nodes will represent lower levels of the managerial hierarchy.

HTML

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

CSS

Thanks to CSS Grid, we’ll create the layout for this level.

Next, we’ll use the ::before pseudo-element of specific elements for creating the associations between the nodes of this level and the adjacent levels:

 

Notice that we also define the ::after pseudo-element of the second-level nodes. This will appear only on small screens.

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

Thanks again to CSS Grid, we’ll position the nodes.

In the same way, we’ll set the ::before pseudo-element of specific elements for creating the required connections:

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 will specify an h4 element:

CSS

Once more, we’ll set out the ::before pseudo-element of specific elements for associating the fourth-level nodes with their parents:

Organizational Charts and Going Responsive

Making an organizational chart responsive is tricky. I remember myself having to reconstruct the markup one or two times until coming up with this version. So, if you plan to create such a chart, I recommend you follow a mobile-first approach.

With all this in mind, here’s its mobile layout:

Responsive Layout of Organization Chart

To accomplish this behavior, we have to modify some styles:

We’ve Finished Our CSS Chart!

Congrats, folks! Without writing a single line of JavaScript, we managed to build a fully functional organizational chart.

Let’s remind ourselves of what we built:

Of course, keep in mind that our chart has a specific structure. Depending on your needs, you might want to enrich its content or modify its layout. If you need something more advanced or dynamic, have a look at some JavaScript libraries like Highcharts.js.

Have you ever created a CSS chart? If so, please share your experience with us!

More CSS Charts (Sometimes With JavaScript)

If you still need some inspiration, don’t forget to check at my other charts in the Tuts+ archive or do a quick search on CodePen.

As always, thanks a lot for reading!

 


No comments:

Post a Comment