Guide on creating a perfectly-aligned line within a rectangular shape using CSS

I'm still learning the ropes when it comes to styling.

In my web app (created with GWT), I am displaying a grid of results. In one column, I want to showcase a score visualization. The idea is to have a central black line and either a green rectangle on the right for positive scores or a red rectangle on the left for negative scores. The size of the rectangle will represent the value of the score. Here's an example:

For this task, I have a div for the entire cell. My approach involves adding two sub-divs to draw the center line and then placing the corresponding rectangle in each div. However, I am struggling a bit with how to execute all these steps.

This is what I have so far:

 @Override
        public SafeHtml getValue(SearchResult value) {
            SafeHtmlBuilder sb = new SafeHtmlBuilder();

            float ratio = value.getRatio();

            sb.appendHtmlConstant("<div style='width: 100%; height: 100%; position: relative;'>");

            if (ratio > 0) { 
                sb.appendHtmlConstant("<div style='position: absolute; left: 0; top: 0; width: 100%; z-index: 1'><div style='display: inline; float: left; width: "
                        + ratio
                    + "%; height: 20px; background-color: #82cd80;'></div>");
            }
            else {
            sb.appendHtmlConstant("<div style='display: inline; float: right; width: "
                    + ratio
                    + "%; height: 20px; background-color: #c54c4d;'></div>");
            }
            sb.appendHtmlConstant("</div>");
            sb.appendHtmlConstant("</div>");
            return sb.toSafeHtml();
        }

Unfortunately, nothing seems to be appearing as expected. I could really use some guidance here.

Answer №1

For each graph, you will need two divs. One div should wrap around the entire graph as a cover, while the other should only surround the bar itself. The cover div is where you will apply your line style. Add a border on the right side of this div using the following code:

border-right: 2px solid color;

(For the second graph, use border-left instead)
I'm not familiar with "SafeHtml," so I can't provide specific instructions, but since it appears to utilize basic HTML features, as long as you know where to place the code, it should work correctly.

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

Overlaying content: Innovative dropdown menu design

My goal is to create a dropdown box that overlays the content of a div when an icon within it is clicked. However, currently, the dropdown box is pushing down the content of the div instead of overlaying it. I have tried adjusting my CSS by setting some el ...

Troubleshooting the issue with the simple Angular material layout/flex example

I attempted to implement a basic layout/flex example from the Angular material design demo site: https://material.angularjs.org/latest/layout/container However, no matter what I try, the layout does not function as described and simply arranges the four ...

Learn the process of incorporating animations into text with Material UI

I'm looking to incorporate a 3D animation effect with a 360-degree rotation on some text using Material UI. <Typography variant="overline" color="secondary" style={{fontFamily:'Roboto'}}> All about your needs </Typography> Ho ...

Using JQuery to Customize Navigation Menu Targeting

On my website, I am trying to make an href button and a menu fade in as the page loads. However, I am struggling to find the correct code due to its placement within multiple tags, especially since I am new to JS and JQuery. I have provided a fiddle with a ...

Display the specified div element automatically

Despite my best efforts, I can't seem to figure this out. I've searched everywhere for a solution but no luck so far. Is there a way to automatically display the content from http://tympanus.net/Tutorials/CSS3ContentNavigator/index.html#slide-mai ...

How can I use jQuery to identify the numerical range within class/td/div elements and modify their CSS styles?

1# I need assistance in changing the CSS properties of a TD, Class, and div using a selector that specifies a specific number range. Specifically, I am looking to modify the css of torrent results with a seed count between 250-25000. Any torrents with a se ...

Manage the element that should receive focus() after tab is pressed from an input field

On my webpage, there is a variety of elements such as inputs and anchors. I need to specify which input field the browser should move focus to when I press the tab key while on an input. Currently, pressing tab from one specific input takes me to a menu i ...

When the form is submitted, any blank inputs and their corresponding hidden fields will be disabled

I created a form that has multiple input fields, and users have the option to enter values or leave them blank. Each input field is accompanied by a hidden input field which contains a specific id unique to the corresponding visible input field. To disable ...

What is the best way to modify a newly added element using jQuery?

When a user clicks a button on my screen, a new template is loaded dynamically using jQuery's .load() method. This transition adds new HTML to the page that was not present when the script initially loaded: (function($) { $('.go').on("c ...

Incorporating a custom background image for Google Maps

Can a live Google map achieve the same overlay effect as a static image? Discover more here Many thanks, Greg Update: Check out this Fiddle that was created based on another Fiddle by @SinistraD ...

The reduced motion media query prevented me from using a Bootstrap modal with animations

I'm currently working with Bootstrap 4 modals and I've encountered an issue where the fade animation and slide animation are not being displayed. After doing some research, I discovered that the problem lies within the transitions.scss file which ...

Trouble with achieving equal height columns in nested flexbox layout on Google Chrome

I am looking to evenly space several sidebar blocks throughout a lengthy post. To achieve this, I have utilized Flexbox within the sidebar for handling the even distribution of the blocks and it has been working flawlessly. Check out Code Pen: http://cod ...

Position the icon in the center using :before pseudo-element

I've been trying to center an icon both horizontally and vertically inside a link using :before, but so far I haven't been successful. Despite my numerous attempts, I can't figure out why the icon won't center. Here's my HTML: & ...

Tips for perfectly centering text within p tags using CSS

Currently, I am troubleshooting the JSFiddle where my goal is to vertically center text using CSS. Below is the HTML code that I utilized to create the JSFiddle: <div class="poster text-center pt-4"> <div class="item"> <img class=" ...

list of key combinations in a ul element

I programmed a list of world states in PHP using an unordered list (<ul>): $WORLD_STATES = array( "France", "Germany", "Greece", "Greenland", "United Kingdom", "United States", "Uruguay" ...

Issue with displaying jQuery class names accurately

I am implementing jquery.scrollablecombo.js for a dropdown menu using jQuery. However, when I include it in my HTML code, the class attribute is displayed as "classname" instead of "class." See the example below: <div classname="searchBar cb_selectMain ...

Invoking a function means calling another one simultaneously

There are two buttons in my code: The button on the right triggers a function called update(): <script> function update(buttonid){ document.getElementById(buttonid).disabled = true; event.stopPropagation(); var textboxid = buttonid.sli ...

fancybox thumbs will never function properly

I recently made the switch from using PrettyPhoto to FancyBox for my gallery, which utilizes Isotope for filtering and animations. However, I am encountering an issue where the images appear but the thumbnails are missing. In the developer tools, there is ...

Adapting CSS according to input selection

Forgive me for asking what may seem like a simple question. I am currently using the CSS code below to modify the background color of a label when its associated checkbox is checked: #project input[id="button1"]:checked + label {background-color:red;} Is ...

IFrame transparency setting not recognized

I am encountering an issue with transparency when adding an iframe to a webpage. I have set the allowTransparency attribute of the iFrame to true, but upon inspecting the element using Internet Explorer's F12 Developer Tools, I noticed that although t ...