How can you create a vertical bar graph using CSS that fills in from the bottom to the top instead of top to bottom?

I have created a bar graph using HTML and CSS. The issue I am facing is that the bar fills up in reverse order - when the height is set to 0%, the bar appears filled at 100%. I attempted changing the CSS property from "top" to "bottom," but it did not resolve the problem. Can anyone advise on how to ensure that setting "height:100%" will correctly show the bar as fully filled?

HTML:


        <div class="bars">
            <div><span style="height:0%;"></span></div>
            <div><span style="height:10%;"></span></div>
            <div><span style="height:20%;"></span></div>
            <div><span style="height:30%;"></span></div>
            ...
            <div><span style="height:100%;"></span></div>
            <div><span style="height:90%;"></span></div>
            ...
            <div><span style="height:0%;"></span></div>
        </div>

CSS:

.bars {
    height: 48px;
    display: block;
    overflow: hidden;
    margin: 0 3px;
}

.bars div {
    background: rgb(12, 88, 160); 
    ...
    float: left;
    height: 100%;
    width: 8px;
    margin-right: 2px;
}

.bars div span {
    background: #233243;
    height: 0%;
    width: 100%;
    display: block;
    position: relative;
    top: 0;
}

Answer №1

To create the desired effect, apply the "position: absolute;" style to the .bars div class and update the .bars div span CSS with "position: relative; bottom: 0px;".

For a visual example, check out this link.

<div class="bars">
    <div><span style="height:0%;"></span></div>
    ...
    <div><span style="height:100%;"></span></div>
</div>

CSS:

.bars{
    height: 100px;
    display: block;
    overflow: hidden;
    margin: 0 3px;
    vertical-align:bottom;
}

.bars div{
    background: rgb(12,88,160);
    float: left;
    height: 100%;
    width: 8px;
    margin-right: 2px;
    position:relative;
}

.bars div span {
    background: #233243;
    height: 0%;
    width: 100%;
    display: block;
    position: absolute;
    bottom: 0;
}

Answer №2

To quickly address the issue, consider adjusting the top position rather than specifying the height in your spans.

<div class="bars">
        <div><span style="top:-0%;"></span></div>
        <div><span style="top:-10%;"></span></div>
        <div><span style="top:-20%;"></span></div>
        <div><span style="top:-30%;"></span></div>
        <div><span style="top:-40%;"></span></div>
        <div><span style="top:-50%;"></span></div>
        <div><span style="top:-60%;"></span></div>
        <div><span style="top:-70%;"></span></div>
        <div><span style="top:-80%;"></span></div>
        <div><span style="top:-90%;"></span></div>
        <div><span style="top:-100%;"></span></div>
        <div><span style="top:-100%;"></span></div>
        <div><span style="top:-90%;"></span></div>
        <div><span style="top:-80%;"></span></div>
        <div><span style="top:-70%;"></span></div>
        <div><span style="top:-60%;"></span></div>
        <div><span style="top:-50%;"></span></div>
        <div><span style="top:-40%;"></span></div>
        <div><span style="top:-30%;"></span></div>
        <div><span style="top:-20%;"></span></div>
        <div><span style="top:-10%;"></span></div>
        <div><span style="top:-0%;"></span></div>
    </div>

Then, ensure the CSS height for all spans is set to 100%.

.bars div span {
    background: #233243;
    height: 100%;
    width: 100%;
    display: block;
    position: relative;
    top: 0;
}

You can view the result here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating Dynamic Bootstrap 4 Forms with Two Columns

I have a user registration form (Bootstrap 4 - in Razor Template) Depending on the user's selections, I will hide certain parts of the form. The registration form consists of two steps, and this is the second step. If a user selects a country withou ...

What is the best way to eliminate unwanted white space at the top of the page?

I'm new to web development and I'm exploring the concept of white space at the top of a webpage. Here is a snippet of my code: HTML: <div> <p>lorem ipsum</P> </div> CSS: div { background-color: black; } I attem ...

When using Angular 8 with RxJs, triggering API calls on click events will automatically detach if the API server is offline

I have an Angular 8 application with a form containing a save button that triggers a call to a Spring Boot microservice using RxJs. I decided to test what would happen if the Rest API server were down. When the Rest API is running, clicking the button wor ...

What could be causing the data toggle feature in my navbar to not work on Bootstrap?

Here is a code snippet to create a simple collapsible navbar with Bootstrap 5 that includes a logout button. However, the data toggle feature for the logout button doesn't seem to work when clicked. Any suggestions on how to fix this? <!DOCTYPE ...

Customize Link Appearance Depending on Current Section

Looking for a way to dynamically change the color of navigation links based on which section of the page a user is currently viewing? Here's an example setup: <div id="topNav"> <ul> <li><a href="#contact">Contac ...

Formatting an invoice document with Bootstrap 4

Currently in my VueJS project, I am constructing an invoice form with the help of Bootstrap 4. Utilizing this snippet, everything has been running smoothly so far. However, I seem to be struggling when it comes to styling the UI at the moment. The issue I ...

Transformation of a double-row header

Click here to view the design - two-row header image I'm struggling with creating a header design that consists of two rows - the first row includes a centered logo and icons on the right side, while the second row contains the menu. Can someone guid ...

What is the best way to ensure the table header is aligned and maintained at the top of the table?

Currently tackling a virtualized table issue with an example. Any tips on aligning the table header correctly and making sure it stays at the top of the table when scrolling? Check out the code sandbox for reference:- https://codesandbox.io/s/material-ui ...

The JavaScript function may fail to work if the AJAX request is not completed

Upon completion of my ajax request, my function does not seem to be functioning as expected. Here is the script I am using: <script> function sendmsg(id,msg){ alert('id is'+id+'and msg is '+msg); } <scr ...

Can you provide the name of the slideshow plugin used on the Wipro website?

Can anyone tell me the name of the image slide show featured on: http://www.wipro.com/index.htm? Also, does anyone know where I can find the script for free? I am looking to incorporate it into a page that is coded in php, html, css, and javascript. Than ...

The button on the Cookie Policy navigation window is no longer operational on the ASP.NET Core MVC website utilizing Bootstrap version 4.3.1

After integrating Bootstrap 4 into my ASP MVC Core web project and converting some HTML code to a Cookie Policy window, I noticed that the accept button is no longer functioning. Although the button is visible and clickable, it does not respond. In additi ...

Send the selected dropdown value to two separate PHP pages without using JavaScript

I am specifically looking for PHP programming, not JavaScript. My requirement is to have a dropdown menu containing 15 weeks. When a user selects a week from the dropdown, they should be redirected to a page that displays a message saying: Welcome! You h ...

The navbar extends fully in height when viewed in Safari browser

My navbar design is flawless on most browsers, but in Safari it appears stretched at full screen height. Here's how it looks in other browsers: https://i.sstatic.net/H9noX.jpg https://i.sstatic.net/Embf3.png However, in Safari...not so much: https:// ...

Issue with inner span not inheriting max-width set on outer <td> element

Whenever I hover over a table cell, the Angular Bootstrap popover should appear next to the text. However, the 'span' element remains at its full width. <td style="max-width: 50px; text-align: left; white-space:nowrap; overflow:hidden; text- ...

The alignment of Material-UI Button and ButtonGroup is not consistent

I'm puzzled as to why the Button and ButtonGroup elements are not aligned on the baseline in the code snippet provided. Is there a specific property that can be adjusted on the ButtonGroup element to achieve alignment? <!DOCTYPE html> <htm ...

Identify the hover event within a div element

There is a parent div with a child div inside it. I want the border color and text of the child div to turn white when a user hovers over the parent div. For instance, in the example provided, the text inside the box that says No score, yet click and subm ...

Adjust image to fit inside a compact popup window - Implementing React.js and Bootstrap

Hello, I could really use some help with a problem I'm facing. I am currently working on my personal portfolio website using react.js and bootstrap. I've integrated the react popupbox package, but I noticed that on small screens, my picture is no ...

Incorporate JSON and JavaScript into your website template

Currently, I am in the process of developing my online resume using a free template that I found and downloaded. The template incorporates bootstrap and showcases a progress bar indicating my skills. However, I want to take it a step further by dynamically ...

Which is quicker, generating a gradient using CSS or directly loading an image?

I am curious about the potential benefits of the new CSS3 styling techniques. However, I am unsure if the effort is worth it! (Question: Can jQuery be used to apply a vignetting effect to a webpage?) Appreciate any insights! ...

Using Angular template reference variables for inputs to bind specific buttons with the same structure

Currently, I am exploring how to connect an input with a button in a row of buttons for opening images, inspired by an example shared by Mr. ruth. The layout of buttons in my web application resembles the following structure: topnav.component.html: <but ...