What is the method for determining the numerical worth of the px containers?

https://i.stack.imgur.com/0K2TD.png

Total width of the bar is set to 500px, with the red box at a width of 150px, the yellow box at 200px, and the green box at 50px.

CSS Styles:

.box { 
        float:left;
        width:150px;
        box-shadow:3px 3px 2px #666767;
        height:30px; 
    }
.red { 
        background-color:#ff0000;
        width:150px;                
    }
.yellow {           
        background-color:#ffff00; 
        width:200px;
        }
.green {
        background-color:#00ff00;
        width:50px;
        }

HTML Markup :

content += "<div class=\"box-container\">
               <div class=\"box red\">
               </div>

               <div class=\"box yellow\">
               </div>

               <div class=\"box green\">
               </div>
            </div>
         </br>";

This code snippet helps in determining the individual widths of the red, yellow, and green boxes using CSS properties.

Answer №1

To determine the total width of a box, you can utilize JQuery in the following manner:

width1 = $(".red").width();
width2 = $(".yellow").width();
width3 = $(".green").width();

totalWidth = width1 + width2 + width3;
alert(totalWidth);
.box {
  float: left;
  width: 150px;
  box-shadow: 3px 3px 2px #666767;
  height: 30px;
}

.red {
  background-color: #ff0000;
  width: 150px;
}

.yellow {
  background-color: #ffff00;
  width: 200px;
}

.green {
  background-color: #00ff00;
  width: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box-container">
  <div class="box red"></div>
  <div class="box yellow"></div>
  <div class="box green"></div>
</div>

View the working Fiddle here

Answer №2

After encountering some issues with the html and css, I decided to make a few modifications to make it work for me. You can check out the console.log with the correct pixel values here.

  <div class="box">
    <div id="red" class="color">
    </div>

    <div id="yellow" class="color">
    </div>

    <div id="green" class="color">
    </div>
   </div>

CSS

.box { 
    float:left;
    width:500px;
    background-color: white 0;
    box-shadow:3px 3px 2px grey;
    height:30px; 
 }    
#red { 
    background-color:red;
    width:25%; 
    height:100%;
        float: left;
 }
#yellow {           
    background-color:yellow; 
    width:25%;
    height:100%;
    float:left;
    }
#green {
    background-color: #00ff00;
    width: 50%;
    height: 100%;
    float: left;
    }

Javascript

 function getWidth(){
   var redWidth = $("#red").width();
   var greenWidth = $('#green').width();
   var yellowWidth = $('#yellow').width();
   console.log(redWidth, yellowWidth, greenWidth);
 }
 getWidth();

Answer №3

let redBoxWidth = document.getElementsByClassName("red").offsetWidth;
let yellowBoxWidth = document.getElementsByClassName("yellow").offsetWidth;
let greenBoxWidth = document.getElementsByClassName("green").offsetWidth;

Answer №4

After completing the basic computations, I defined the width value.

var totalIssues = args.chart.chartWidth[i].redWidth + args.chart.chartWidth[i].yellowWidth + args.chart.chartWidth[i].greenWidth; 
            var red = (args.chart.chartWidth[i].redWidth/totalIssues)*100;
            var yellow = (args.chart.chartWidth[i].yellowWidth/totalIssues)*100;
            var green = (args.chart.chartWidth[i].greenWidth/totalIssues)*100;

            var totalPx = 300; 

            var redWid = (red/100) * 500;
            var yellowWid = (yellow/100) * 500;
            var greenWid = (green/100) * 500;


            console.log("Red Width : " + redWid + "Yellow Width : "+ yellowWid+ "Green Width : "+greenWid);
            content += "<div class=\"project-data\"><div class=\"box-container"+i+"\"><div class=\"box"+i+" red" +i+ "\"></div><div class=\"box"+i+" yellow" +i+ "\"></div><div class=\"box"+i+" green" +i+ "\"></div></div></br>"; 
             content += "<style type=\"text/css\"> ";
             content +=".box-container"+i+"{ margin: 50;}";
             content +=".box"+i+"{float: left; width:150px; box-shadow:3px 3px 2px #666767; height:20px;}";
             content += ".red" +i+" { width: " + redWid + "px; background-color:#ff0000; }";
             content += ".yellow"+i+" { width: " + yellowWid + "px; background-color:#ffff00;  }";
             content += ".green"+i+" { width: " + greenWid + "px; background-color:#00ff00; }";      
             content +=  "</style> </br>";

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

Interacting between C# and Node.js

I am looking to develop a GUI application using C# that will serve as a platform for Node.js. Specifically, I want to utilize Node's file system API to read files from a directory, and have my C# program generate a list (similar to a ListView) to show ...

Upon the initial data retrieval, everything seems to be working fine. However, when the user transitions to chatting with another user, the state value does not reset and instead shows a combination

Exploring the world of react.js and node.js, I am currently working on developing a chatting application with the integration of socket.io. The issue I'm facing is that when a user clicks on another user to chat, the previous chat messages are display ...

Building a Responsive Grid Layout with HTML and CSS: Layering one div on top of another

I'm struggling to find a solution to my problem. I attempted to set my div as absolute but it messed up the entire flexible grid layout. All I need is to place the div above the background div. Thank you in advance! CSS #mannequin { box-sizing: ...

In Express.js, the value of req.body.data is not defined

Recently, I've been delving into nodejs and express js. My aim is to send a json object to my nodejs application using postman. Below is the code snippet from my app: var express = require("express"); var bodyParser = require('body-parser') ...

Combining Multiple NodeLists in Javascript

Initially, I was seeking a sophisticated method to replicate the functionality of Array.concat() on the results of the getElementsByTagName function in older browsers like IE because it appeared that concat wasn't supported. However, as it turns out, ...

What causes the unexpected outcome when applying theme overrides in Mui V5?

I am looking to adjust the border radius of all input fields in my project. Specifically, I would like the TextField component to have a border radius of https://i.stack.imgur.com/ULEGj.png instead of https://i.stack.imgur.com/NWmUe.png. According to the M ...

What is the best method for integrating addEventListener with Javascript functions located in a different file?

I currently have document.addEventListener('DOMContentLoaded', functionName); which uses the function below: function functionName() { $.ajax({ type: 'GET', url: '/populatePage', success: function(data) { ...

The event type of jQuery's hover is displayed as mouseover and mouseenter

Check out this picture: Do you think this is a bug or the norm? Please note that 'mouseover after args' is just a label. Capture Event $ -> $('.bigger-on-hover').hover (event) -> console.log 'args' console ...

Embedded Twitter posts are not showing up correctly when using vue

My backend is Django and frontend is Vue. In one of my models, I have a rich text editor. When saving the content, it converts tweet links to embedded HTML tweets. While displaying this content in a div with v-html binding in Vue, everything appears as exp ...

Exploring the Power of ReactJS Source Mapping

After adding the .env file to my react project, I noticed that when the project is uploaded to the server, the console source is displaying all components. Although I added the .env file and included the following code: GENERATE_SOURCEMAP = false; The i ...

Modify text input when a different option is selected (with both options originally coming from a database)

A dropdown menu is filled with options from a database, and the chosen option is compared to a variable $comp_cntry currently on the page: <select name="country"> <option value="--" disabled>Please Select...</option> <option v ...

What is the best way to retrieve past data using RTK Query?

When working with data retrieval using a hook, my approach is as follows: const { data, isLoading } = useGetSomeDataQuery() The retrieved data is an array of items that each have their own unique id. To ensure the most up-to-date information, I implement ...

Child component not receiving disabled state from Angular 8 FormControl

In my code, there is a unique custom input that I have defined: <nivel-servico-slider formControlName="fornecedor_a"></nivel-servico-slider> This custom input has all the necessary properties as outlined in Angular's guide for c ...

What is the best way to ensure that the larger child divs always fit perfectly within the parent div?

Creating a Responsive Layout <div class="container"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> CSS Styling .box1, .box2, .box3{ display: block; f ...

Retrieve the heading from a pop-up box

This jQuery tooltip allows for popups from another HTML page to be created. UPDATE: I have provided an example HERE The issue arises when trying to retrieve the title from the popup. Currently, using document.title displays the title of the current page ...

Manipulate data with Jquery Serialize

There are multiple text boxes, drop down menus, and radio options on this form. Upon clicking submit, I need to save all the information entered by the user in order to store it into a database. <div id="reg2a"> First Name: <br/><input type ...

Is it possible to remove htmlescape from the custom options in Magento? Can you specify which file showcases the custom

Looking to incorporate HTML into custom option labels for products on my Magento store. Unfortunately, Magento does not parse HTML by default when it is placed in the custom options interface. I suspect there may be a line like "$this->htmlEscape()" wit ...

Substitute empty spaces and organize the text layout

I'm struggling to figure out how to substitute the whiteSpace in an aggregate request using MongoDB and also how to remove a specific part of a string (most likely requiring regex). Here's an example of my document: { "_id": "57dfa5c9xxd76b ...

How to properly handle file uploads and get the correct image path from Node Js (Express) to React Js?

Currently, I am working on my local system developing a file upload feature using node js. My project file structure looks like this: Project ..client .... source code of React App ..Server ....uploads ......avatar ........image.png ....index.js In this ...

Using JavaScript to implement CSS3 with multiple background images

Is there a way to programmatically define multiple background images in CSS3 using JavaScript? While the common approach would be: var element = document.createElement( 'div' ); element.style.backgroundImage = "url('a.png') 0 100%, ur ...