This code creates a vertical stacked bar chart using CSS and jQuery. It visually represents data percentages in bars. The JavaScript part adjusts the bar heights based on the data provided, enabling easy data visualization. This chart style is helpful for showcasing proportional data in a visually appealing manner without using traditional chart libraries.
It’s handy for showcasing progress, comparisons, or any data with percentages in a visually engaging way. This method offers a simple, lightweight solution without relying on extensive chart libraries, making it easy to implement and customize.
How to Create CSS Vertical Stacked Bar Chart
1. First of all, load the Google Fonts and jQuery by adding the following CDN links into the head tag of your webpage.
- <link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
- <script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
2. Within the body section, create a container element to hold the chart. Use nested div
or section
elements to structure the bars. Assign classes to elements for styling and manipulation purposes.
- <div class="container">
- <h1>Stacked Bar Charts</h1>
- <p>A nice and simple stacked bar chart with some CSS & some Jquery.</p>
- <!-- BEGIN CIRCLE CODE -->
- <section class="section-stacked-bars">
- <section class="report report-usage-daily" id="reveal-usage">
- <ul class="vertical-bars">
- <li class="bar">
- <span class="usage" data-usage="20">
- <span class="usage-value">
- <small>20%</small>
- </span>
- <small class="legend">80%</small>
- </span>
- </li>
- <li class="bar">
- <span class="usage" data-usage="40">
- <span class="usage-value">
- <small>40%</small>
- </span>
- <small class="legend">60%</small>
- </span>
- </li>
- <li class="bar">
- <span class="usage" data-usage="60">
- <span class="usage-value">
- <small>60%</small>
- </span>
- <small class="legend">40%</small>
- </span>
- </li>
- <li class="bar">
- <span class="usage" data-usage="80">
- <span class="usage-value">
- <small>80%</small>
- </span>
- <small class="legend">40%</small>
- </span>
- </li>
- <li class="bar">
- <span class="usage" data-usage="100">
- <span class="usage-value">
- <small>100%</small>
- </span>
- <small class="legend">0%</small>
- </span>
- </li>
- <li class="bar">
- <span class="usage" data-usage="60">
- <span class="usage-value">
- <small>60%</small>
- </span>
- <small class="legend">40%</small>
- </span>
- </li>
- <li class="bar">
- <span class="usage" data-usage="40">
- <span class="usage-value">
- <small>40%</small>
- </span>
- <small class="legend">60%</small>
- </span>
- </li>
- <li class="bar">
- <span class="usage" data-usage="0">
- <span class="usage-value">
- <small>0%</small>
- </span>
- <small class="legend">100%</small>
- </span>
- </li>
- </ul>
- </section>
- </section>
- <!-- END CIRCLE CODE -->
- </div>
3. Define the CSS styles for the chart elements. Customize the colors, dimensions, positions, and typography based on your design preferences. Pay attention to classes like .bar
, .usage
, and .usage-value
for styling the bars.
- .vertical-bars .bar .usage {
- display: block;
- background: #e67e22;
- height: 250px;
- width: 40px;
- position: relative;
- font-size: small;
- }
- .vertical-bars .bar .usage small {
- position: absolute;
- top: 0;
- right: -35px;
- }
- .vertical-bars .bar .usage-value {
- background: #d35400;
- display: block;
- position: absolute;
- bottom: 0;
- left: 0;
- right: 0;
- height: 80%;
- }
- .vertical-bars .bar .usage-value small {
- background: #2c3e50;
- z-index: 5;
- }
- body {
- background: #2c3e50;
- }
- .container {
- width: 940px;
- margin: 0 auto;
- color: #ecf0f1;
- font-family: "Open Sans", sans-serif;
- text-align: center;
- }
- h1 {
- text-transform: uppercase;
- }
- p {
- margin-bottom: 48px;
- }
- a {
- color: #ecf0f1;
- }
- ul {
- margin: 0 0 0 -10px;
- padding: 0;
- }
- ul li {
- display: inline-block;
- margin-left: 10px;
- width: 10%;
- text-align: left;
- }
4. Finally, add the following jQuery script to manipulate the chart elements dynamically. Target the data attributes within the HTML elements and adjust the height of the bars accordingly. Utilize $(document).ready()
to ensure the DOM is fully loaded before executing the script.
- $(document).ready(function() {
- var graph = $('.usage');
- $.each(graph, function() {
- var usage = $(this).attr('data-usage');
- $(this).children('.usage-value').css({
- 'height': + usage +'%',
- });
- });
- });
That’s all! hopefully, you have successfully created a Vertical Stacked Bar Chart using jQuery and CSS. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:

I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.