{{announcement.body}}
{{announcement.title}}

How to Create a Simple Gantt Chart Using CSS Grid

DZone 's Guide to

How to Create a Simple Gantt Chart Using CSS Grid

In this article, we discuss how to create a simple Gantt chart using CSS Grid to better manage your project workloads.

· Web Dev Zone ·
Free Resource

A Gantt chart is a handy type of bar chart that is used in project management for showcasing a schedule of tasks. This chart visualizes project activities as cascading horizontal bars, with width depicting the project’s duration. 

As a front-end web designer or developer, you can make use of Gantt charts to manage projects and enhance the productivity within your team.

In this article, I’m going to show you how to create a simple Gantt chart using the CSS Grid Layout system—no external libraries or other fluff, just pure CSS.

You may also like: Redesigning a Website Using CSS Grid and Flexbox.

You can reference this tutorial to understand how to use the layout system for applying CSS rules.

The chart will show a typical software development life cycle process, from January to December. 

Here is a screenshot of how the Gantt chart will look at the end of this tutorial:

Gantt Chart

Let’s get started!

Step 1: Create a Container div

Let’s start by creating a container div element for the Gantt Chart:

HTML
 




xxxxxxxxxx
1


 
1
<div class="container">
2
  
3
</div>



Let’s add some CSS styling to it:

CSS
 




xxxxxxxxxx
1


1
.container {
2
  max-width: 1200px;     
3
  min-width: 650px;     
4
  margin: 0 auto;     
5
  padding: 50px;
6
}



Step 2: Create a Chart div

Let’s create a div inside the overarching container and name it chart. This is where all the remaining actions are going to take place.

HTML
 




xxxxxxxxxx
1


1
<div class="chart"> 
2
  
3
</div>



Let’s add some CSS styling to it:

CSS
 




xxxxxxxxxx
1


1
.chart {   
2
  display: grid;     
3
  border: 2px solid #000;      
4
  position: relative;      
5
  overflow: hidden; 
6
} 



Notice that I’ve set the display property of the class to grid. Consequently, all its direct children will automatically become grid items.

Step 3: Create the Chart’s Rows

Let’s start by creating the first row, which will be the heading of the Gantt chart.

HTML
 




x


 
1
<div class="chart-row chart-period">
2
<div class="chart-row-item">
3
    </div><span>January</span><span>February</span>span>March</span>
4
    <span>April</span><span>May</span><span>June</span><span>July</span>
5
    <span>August</span><span>September</span><span>October</span>
6
    <span>November</span><span>December</span>
7
</div>



Notice that I’ve provided 12 span elements that will transverse the entire row, showing the months of the project’s duration — from January to December.

Here is its CSS:

CSS
 




xxxxxxxxxx
1


1
.chart-row {  
2
    display: grid; 
3
    grid-template-columns: 50px 1fr; 
4
    background-color: #DCDCDC;
5
}



CSS
 




xxxxxxxxxx
1
15


 
1
.chart-period { 
2
    color:  #fff;  
3
    background-color:  #708090 !important;  
4
    border-bottom: 2px solid #000;  
5
    grid-template-columns: 50px repeat(12, 1fr);
6
 
          
7
}
8
 
          
9
.chart-period > span {
10
    text-align: center;  
11
    font-size: 13px;  
12
    align-self: center;  
13
    font-weight: bold;  
14
    padding: 15px 0;   
15
}



Notice that I used the grid-template-columns property to specify the width and the number of columns in the grid layout.

Let’s see how it looks in a browser, so far:

browser view

Next, let’s add lines that will run throughout the chart in a box-like style, which helps showcase the duration of each project. 

I also used 12 span elements for creating the lines.

CSS
 




xxxxxxxxxx
1


1
<div class="chart-row chart-lines"> 
2
    <span></span><span></span><span></span>
3
    <span></span><span></span><span></span>
4
    <span></span><span></span><span></span>
5
    <span></span><span></span>  <span></span>    
6
</div>



Here is its CSS:

CSS
 




xxxxxxxxxx
1
10


 
1
.chart-lines { 
2
    position: absolute;  
3
    height: 100%;  
4
    width: 100%;  
5
    background-color: transparent;  
6
    grid-template-columns: 50px repeat(12, 1fr);}
7
 
          
8
.chart-lines > span {  
9
    display: block;  border-right: 1px solid rgba(0, 0, 0, 0.3);
10
}



Let’s see the output in a browser:

browser view

Step 4: Add Entry Items

Finally, let’s add the items that illustrate a year-long process of creating some software.

For example, here is how I added the first entry item:

CSS
 




xxxxxxxxxx
1


 
1
<div class="chart-row">  
2
    <div class="chart-row-item">1</div> 
3
    <ul class="chart-row-bars">    
4
        <li class="chart-li-one">Planning</li>
5
    </ul>
6
</div>



Let me describe what is happening in the code above:

  • First, the encompassing div element has a class of chart-row, which I illustrated earlier.
  •  
  • The div with a class of chart-row-item is used for numbering the entry items on the Gantt chart. Here is its CSS:

  • To show tasks on the Gantt chart, I created an unordered list and styled it to display a horizontal bar, with its length showing the duration of the task. 

Here is the CSS styling for the chart-row-bars class:

CSS
 




xxxxxxxxxx
1


 
1
.chart-row-bars { 
2
    list-style: none; 
3
    display: grid;  padding: 15px 0;  
4
    margin: 0;  
5
    grid-template-columns: repeat(12, 1fr); 
6
    grid-gap: 10px 0;  
7
    border-bottom: 1px solid #000;
8
}


  • The entry item is defined in the li tag. Here is its CSS styling:
CSS
 




xxxxxxxxxx
1
16


 
1
li {  
2
    font-weight: 450;  
3
    text-align: left;  
4
    font-size: 15px;  min-height: 15px;  
5
    background-color: #708090;  
6
    padding: 5px 15px;  color: #fff;  
7
    overflow: hidden;  
8
    position: relative;  
9
    cursor: pointer;  
10
    border-radius: 15px;
11
 } 
12
 
13
 ul .chart-li-one { 
14
    grid-column: 1/2;  
15
        background-color: #588BAE;
16
 }



Notice that I’ve used the grid-column property to specify the duration of the project. 

For example, a property of grid-column: 3/9; like the “Development” entry, spans a task across the grid from March to August.

Here is how the first entry item looks in a browser:

planning in browser

I added the other entries on the chart following the same process as the first entry.

Ultimately, it resulted in a nice-looking Gantt chart, just like the image I showed earlier. 

Wrapping Up

That’s it! You can view the entire code for this tutorial on CodePen:

As you’ve seen, creating a Gantt chart using CSS Grid is not complicated. With this type of chart, you can manage your web development projects effectively and ensure that everyone is on track toward accomplishing the stipulated objectives.

Furthermore, Gantt charts can also be used in other industries to manage projects. For example, if you are selling composting toilets, you can use Gantt charts to showcase the number of sales made over a period of time. 

Of course, I’ve just scratched the surface about the things you can achieve with Gantt charts. 

There are several other tweaks you can make to Gantt charts to suit your specific requirements and project goals. 

For example, you can use them to show the relationship between various tasks and how the completion of one relies on another, show how resources can be allocated for the success of projects, and show clear project requirements that ensure everyone is on the same page.

Do you have any questions or comments?

Please get in touch  and I’ll do my best to respond.

This article first appeared on freeCodeCamp.

Further Reading

Topics:
chart, css grid, gantt chart, javascript, project management, tutorial

Published at DZone with permission of Alfrick Opidi . See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}