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

My website includes a <div> section that features both an image and a <figcaption>. However, when I attempt to apply padding to the <figcaption>, it does not seem to take effect

I've been trying to troubleshoot this code, but the padding just won't cooperate and I can't seem to figure out why. .img-caption { visibility:hidden; width:22%; height:435px; background-color:#f9f4e3; ...

Looking to create a custom loader that adapts to different screen sizes

Can anyone help me make the text in my centered div responsive on my website loader? I'm new to coding and struggling to figure it out. Any assistance would be greatly appreciated! Make sure to view the full page snippet so you can see the text prope ...

How can I ensure my game or website fits perfectly on the screen without any need

I have recently developed a fun Erase-A-Man game that is optimized for mobile devices. However, I am facing an issue where users need to scroll to view all the letters on their screens. My goal is to make the game fit perfectly on all phone screen sizes so ...

What is the method for adding the square root symbol to a button's value?

I am attempting to display the square root symbol inside a button, but I am having trouble making it work. Below is my button code: <input type="button" id="sqrt" value="sqrt" onclick="insert function here"> For the value, I want the square root sy ...

Refreshing a view in Laravel after a successful insertion using JQuery Ajax

I have been successfully inserting records into a table using jQuery ajax, and receiving a flash message confirming the successful insertion. However, I am now facing an issue where I do not know how to reload the table to reflect the changes after the rec ...

What makes 'Parsing JSON with jQuery' unnecessary?

Just performed an ajax request with a query and noticed that my response is already in the form of a JavaScript object. When I try to parse the JSON using: var obj = jQuery.parseJSON(response); 'obj' turns out to be null, yet I can directly ac ...

The function Expo.Fingerprint.isEnrolledAsync will provide an output that includes information about fingerprint

Attempting to incorporate fingerprint authentication into my react native app. Utilized Expo SDK for this purpose. Although the Expo.Fingerprint.authenticateAsync() method claims to return a boolean, it actually returns an object when examined closely. E ...

Setting state for a dynamic component based on condition in React

Working on creating a dynamic component where the data index matches the URL parameter blogID received from the router. Below are the router parameters sending props to the component: <Route path='/blog/:blogId/:blogTitle' render={() => & ...

Sorting Tables through the Power of Drag and Drop

While utilizing both the Jquery Tablesorter plugin and the Drag and Drop plugin together, everything seems to be functioning correctly. However, when attempting to use the serialize function of the tableDnD, an error message stating "empty string getElemen ...

Is it possible to use jQuery to refresh only a section of the page and modify the URL at the same time?

There is a page (http://myflashpics.com/picture/p9e0) that displays user information along with a small thumbnail on the side. Currently, when clicking on the image, it redirects to a different page and the sidebar reloads as well. I am curious if it' ...

Retrieving text content from an HTML element with Jsoup for web page manipulation

My task involves extracting text content from a specific HTML element <span class="adr" style="float: none !important;"> <span class="street-address" style="float: none !important;">18, Jawaharlal Nehru Road, </span> ...

drawing tool with JavaScript and HTML5

Can you help me figure out why my sketchpad isn't working in Processing.js? I've checked my code and there are no errors, but the canvas is not showing up. Any suggestions? Take a look at the code below: function createSketchPad(processing) { ...

Ways to apply the strategy pattern in Vue component implementation

Here's the scenario: I possess a Cat, Dog, and Horse, all of which abide by the Animal interface. Compact components exist for each one - DogComponent, CatComponent, and HorseComponent. Query: How can I develop an AnimalComponent that is capable of ...

The intricate dance between JAVA and HTML

Can Java be compatible with HTML and JS? Is it possible for them to cooperate without using JSP? In order to connect the WEKA function, we utilized Java code through a JAR file, but now we also require HTML and JS links for visualization. Is there an alte ...

What is the best way to extract the value from a textfield and then merge it with

This is a code snippet used to retrieve data from a text field: <script type="text/javascript"> var mod=document.getElementById("mod").value; ajax(mod); function callback() { if(ajaxObj(mod) { document.getElementById( ...

How to dynamically adjust font size using Javascript

Is there a way to adjust the font size of the left-column using JavaScript? <div id="left-column"> <a href="">1</a><br> <a href="">2</a><br> <a href="">3</a><br> <a href=""> ...

Is it possible to visually distinguish the selected mat-grid-tile? Particularly when they are being created dynamically

On the user interface, I have a dynamic display of mat-grid-tile within a mat-grid-list. These tiles change in number and data based on backend values. When a user clicks on a mat-grid-tile, it triggers a function that receives the tile's data. My goa ...

JavaScript/DOM - What sets apart a "CSS Selector" from an attribute?

When it comes to excluding declarative event handlers: <a href='#' onclick=<handler> ... /> Is there a significant difference between an Attribute and a CSS Selector? For example, if I define my own attribute: <a href='#&a ...

Troubleshooting problem with Materialize CSS in UI router

Incorporating Materialize CSS along with Angular's ui.router for state management and HTML rendering has led to a challenge. Specifically, the Materialize Select component is not initialized upon state changes since Materialize components are typicall ...

"Enhance your Angular experience with SweetAlert integration using directives and translation

Currently, I am utilizing the Angular implementation of the SweetAlert plugin from GitHub. I am attempting to pass an Angular directive with translation to the title. The variable being passed as the title is: {{ 'register.confirmation_modal.SUPERI ...