Is it possible to accurately measure the width of elements down to the partial pixel level?

This dilemma has been causing me frustration for quite some time. My goal is to achieve a simple task - float li elements in a ul container and give them a hover effect. Here's the code snippet: Everything seems to be in order, with all CSS reset, display block on elements, and a clear class that clears both after each element.

<ul class="clear">
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

ul > * { float: left; border: 1px solid black  }
li{ padding: 10px;}
li:hover{ background-color: white; }    

However, I've noticed that the width of the ul container changes in different browsers for some unknown reason (perhaps due to the font being used). This makes it difficult to hard code the width of the ul to fit all the li elements perfectly. So, my workaround involves measuring the width of each li element once everything loads, adding them up, and setting that as the width of the ul.

Unfortunately, this approach also presents challenges. Methods like jquery.width(), offsetWidth, or outerWidth only return whole numbers, while some elements have partial pixel widths like .34px. As a result, the ul ends up either slightly too wide, resulting in a gap, or too narrow, causing the floated elements to drop to the next line.

Is there a more precise way to measure the width accurately?

Answer №1

Here is a solution that might work for you, check out the example provided in this fiddle https://jsfiddle.net/b827CqrM/15/

$(".remove")[0].getBoundingClientRect().height

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

Remove the option to delete without making any changes to the flash file

Utilizing the DataTable javascript tool to export a grid, I have obtained the following HTML generated code: <div class="DTTT_container"> <a class="DTTT_button DTTT_button_copy" id="ToolTables_example_0" tabindex="0" aria-controls="e ...

Proper syntax for SVG props in JSX

I have developed a small React component that primarily consists of an SVG being returned. My goal is to pass a fill color to the React component and have the SVG use this color. When calling the SVG component, I do so like this: <Icon fillColour="#f ...

Altering the dimensions of radio buttons

I am a newcomer to using material-ui. I am currently working on incorporating radio buttons in a component and would like to reduce its size. While inspecting it in Chrome, I was able to adjust the width of the svg icon (1em). However, I am unsure how to a ...

Enabling droppable blocks within Jquery UI for div elements

I am facing a challenge in achieving my goal. Here is an example I have set up: http://jsfiddle.net/zs6US/ $('.draggable').draggable({ revert: 'invalid' }); $('#droppable').droppable({ accept: '.draggable' ...

Strategies for managing recurring identical HTML content

Within my website, I find myself repeating similar content frequently on the same page. For example: <a href="link1"><img src="path1" alt="name1"></a> <a href="link2"><img src="path2" alt="name2"></a> <a href="link3" ...

The sequence in which jQuery's bind() function is executed

Suppose I have two jQuery bindings like the following: $("input:checkbox").bind("touchstart",function(){ } and $(document).bind('touchstart',function(e) { } In this case, the input checkbox is part of the same document. My question is: Wha ...

Vue.js Class-based Decorator not Transmitting Event from Child to Parent Component

I'm fairly new to working with Vue.js and I've encountered an issue with the child to parent emit functionality. Essentially, I have a Box component that contains a Search component. In the Search component, I attempted the following: @Watch("se ...

What potential issues could arise from utilizing a numerical value as an identifier or class in an HTML4, XHTML, or HTML5 webpage?

I am hesitant about using elements with either id="12" or class="12", as the title suggests. Although it is allowed by the specifications, particularly in HTML5, have you faced any issues when doing so? In my specific scenario, I have <li> elements ...

Maintaining the order of list items while utilizing jQuery sortable功能

Check out my JSFiddle link here I am utilizing the jQuery sortable function to enable sorting of 3 columns. My goal is to ensure that when a name is moved from column 2 or 3 to column 1, the names in columns 2 and 3 remain unaffected, maintaining their al ...

Exporting a function from one module does not automatically make it accessible in another module

I'm stuck trying to call the retrieve_endpoints function from cli_config.js. I made sure to add the functions to the module exports and included the require statement in the cli_config file. But for some reason, I can't seem to access the retriev ...

Is there a way to calculate the overall total ($sum) of an array specifically from a nested field in an array structure?

I am looking to calculate the total sum of all elements in an array that is nested within my schema. Here is a snippet of the schema: const mongoose = require('mongoose'); let historySchema = new mongoose.Schema({ time: { type:String ...

Is there a way to simulate a click event (on a file type input) within a promise?

I've been struggling with this issue for a long time and have not been able to find a solution. The problem arises when attempting to trigger a click event on a file input type from within a promise. When I directly trigger the event inside the promis ...

How come the absolutely positioned element is nested within the first "row" relative div rather than its immediate parent div?

I have a specific goal in mind: I aim to create two rows, each containing three columns. I have used relative positioning for the rows, with three images in each row for a total of two rows, and this is working well. My next step is to add layered divs b ...

How can a single item be selected from a multi-select drop-down menu using Python Selenium?

In the web page, there is a button labeled [Add Field] which, when clicked, reveals a drop-down menu. The xpath for this drop-down menu is: '//*[@id="adv_options[3][]' The second array item is empty as shown here: '[]'. I have manag ...

Warning: The update depth in Nextjs has surpassed the maximum limit

I am currently developing a React Header component with a dropdown menu feature that can be toggled. Upon loading the page, I encountered the following error: next-dev.js?3515:20 Warning: Maximum update depth exceeded. This issue may arise when a compone ...

Tips on showing content while filtering is taking longer with AngularJS

When working in Angular, I encountered a situation where filtering a large amount of data in a table was slowing down the process. To address this issue, I wanted to display a spinner every time a filter operation was in progress. Here is an example simil ...

Displaying three tables in each row using ng-repeat, with the ability to repeat the process multiple times

Is it possible to repeat a table multiple times with each set of three tables in the same line using AngularJS and Bootstrap? I am unsure how this can be achieved with ng-repeat. table 1 | table 2 | table 3 table 4 | table 5 | ... <div ng-repeat="zo ...

Error encountered: Denied access in AWS Transcription Node JS API

I have been working with the AWS transcription API in Node JS and my code looks like this: const tClient = new TranscribeClient({ region: "us-east-1", credentials: { accessKeyId: AWS_ID, secretAccessKey: SECRET, ...

Unable to assign to 'disabled' as it is not recognized as a valid attribute for 'app-button'

How to link the disabled property with my button component? I attempted to add 'disabled' to the HTML file where it should be recognized as an input in the button component (similar to how color and font color are recognized as inputs) ... but ...

Design a circular text element using Tailwind CSS

Just starting out as a web developer and running into some issues with Tailwind CSS. Here's what I'm trying to achieve: https://ibb.co/KL8cDR2 This is the code I have right now: <div class="w-1/2 h-10 rounded-full bg-gray-400" style ...