The vertical position of an element using jQuery's position() method varies between Chrome and Firefox

Whenever I execute the code in the provided fiddle on Chrome and Opera, the position().top is shown as 0. However, Firefox displays it as 1 and IE shows 0.5.

Is there a way to resolve this inconsistency?

http://jsfiddle.net/scottieslg/Loefza24/2/

HTML:

<table id='testTable'>
    <thead>
        <tr><td>Test</td></tr>
    </thead>

    <tbody>
        <tr><td>Body</td></tr>
    </tbody>
</table>

<div id='topPos'></div>

CSS:

* { margin: 0; padding: 0 }
#testTable {
    table-layout: fixed;
    width: 100%;
    padding: 0;
    margin: 0;
    border-collapse: collapse;
}

#testTable thead tr td {
    border: 1px solid #ccc;
}

Script:

$(document).ready(function() {
    var topPos = $("#testTable thead tr:nth-child(1)").position().top;
    console.log(topPos);
    $("#topPos").html("Top: " + topPos);
});

Answer №1

FF appears to be treating the border as separate from the element itself. One potential workaround is to use box-shadow instead:

$(document).ready(function() {
    var topPos = $("#testTable thead tr:nth-child(1)").position().top;
    console.log(topPos);
    $("#topPos").html("Top: " + topPos);
});
* { margin: 0; padding: 0 }
#testTable {
    table-layout: fixed;
    width: 100%;
    padding: 0;
    margin: 0;
    border-collapse: collapse;
}

#testTable thead tr td {
     box-shadow:0px 0px 0px 1px #ccc inset;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='testTable'>
    <thead>
        <tr><td>Test</td></tr>
    </thead>

    <tbody>
        <tr><td>Body</td></tr>
    </tbody>
</table>

<div id='topPos'></div>

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

Sending an array of integers to Django using Ajax

I'm currently working with DataTables and have implemented a feature that allows users to select multiple rows and delete them. As of now, the functionality deletes only the first selected row using the following code snippet. Ajax Code: /* Clic ...

Achieving consistent alignment for text on a curved path

I am working on a unique "flow-builder" project and I need to achieve this specific result: https://i.sstatic.net/6TTuS.png At the moment, my current edge looks like this: https://i.sstatic.net/9ydzx.png The text on the edge is currently aligned with the ...

What is the best method for creating uniform cards with different amounts of text that won't cause overflow issues?

At the moment, I have a container that acts as a boundary for a group of cards: .cards-container { display: flex; align-items: stretch; justify-content: center; flex-wrap: wrap; margin-top: 20px; border: 5px solid violet; } .card { ...

I am looking to obtain a value through a JavaScript function that sums values upon clicking. The result is satisfactory, but I am seeking a way to limit the value

I only need 2 decimal values, not large ones. Can someone please help me achieve this? <script> $(function() { $("#isum, #num1, #num2, #num3, #rec1, #rec2, #rec3, #difnum1, #difnum2, #difnum3").on("keydown ke ...

What is the method for adjusting the border color of an ng-select component to red?

I am having an issue with the select component in my Angular HTML template. It is currently displaying in grey color, but I would like it to have a red border instead. I have tried using custom CSS, but I haven't been able to achieve the desired resul ...

The AJAX logout feature is experiencing technical difficulties and can't be used properly

When this button is clicked, I want to logout. HTML input: <input type="button" value="logout" id="top-btn-logout" onclick="test()"> My JavaScript file: function test() { $("#top-btn-logout").click(function() { $.ajax({ ur ...

Is it possible to load a jQuery Mobile dialog without the header?

Within my jQuery Mobile website, I have a button in the header that looks like this: <a href="/foo.html" data-rel="dialog" data-icon="grid">Foo</a> The issue arises when JQM requires me to redeclare the head section with all scripts and CSS. ...

Issues with Adding Commas to Numerical Values

I've added a comma separator to display the total value that is being updated for roundNum: $.fn.digits = function(){ return this.each(function(){ $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?! ...

Skipping validation while navigating back on SmartWizardIf you need to bypass validation during backward

Looking for assistance with Jquery smartwizard? Check out the link. I want to disable validation when a user clicks on the "Previous" button in any step, except for the first step where the Previous button is disabled by default. Below is my JavaScript co ...

What is the best way to stack div elements one after the other?

I am a complete beginner in HTML and I have a project for my web design class about a movie. http://jsfiddle.net/Lbo1ftu9/ <div id="footer"> <a href="D:/MOVIE REPORT/akira.htm" title="GO BACK?"><img src="D:/IMAGES/goback.gif"> My code ...

How can I quickly submit this form with jQuery?

Alright, so I believed I had everything figured out but turns out I was mistaken. Essentially, there's a form wrapped in a span (#container) and embedded within a page. The server-side handles all the validation and determines whether it's a ful ...

Is there a way to identify the specific list item that a draggable element has been dropped onto within a jQuery sortable with connected draggable lists?

Take a look at these two sets of items: user's inventory <ul id='list1'> <li>Apple</li> <li>Banana</li> </ul>available products <ul id='list2'> <li>Orange</li> ...

Unable to prevent ordered lists from overlapping with other content I attempt to place beneath them in HTML

function filterImages() { let input = document.getElementById('searchbar').value; input = input.toLowerCase(); let images = document.getElementsByClassName('gallery'); for (let i = 0; i < images.length; i++) { ...

Implementing the decrement functionality within an onclick event handler for a variable

Can someone assist me with this issue? I am trying to use a variable ID in HTML to call a function on a JavaScript page. For example: (Minus button not functioning) <button class="minus-button quantity-button button" type="button" name="subtract" onc ...

Dynamically filling the text area by fetching data from the database when a dropdown option is selected

There is a table called 'vendortable' with attributes 'vendorid', 'vendorname', and 'vendoraddress'. I am struggling to populate the data from the database into a textarea using jQuery and AJAX. Calling all amateu ...

Get rid of the gutter in the Bootstrap 4 grid system

When working with Bootstrap 4, the task is to customize the default grid system for desktop screens like this: https://i.sstatic.net/ZoEem.png specifically, setting the container and desktop breakpoint at 1280px. The example attempted is : body ...

Unleashing the Power of Tailwind CSS: Enabling Group Hover Effects on Anchor Tags

When I run my code on play.tailwindcss.com, the group hover feature works fine. However, when I copy the same code into my local file, the group hover doesn't work there. What do I need to add to my tailwind.config.css file in order to enable group ...

Retrieve all CSS properties associated with a specific DOM element, similar to the functionality provided by Firebug

Is there a method to retrieve all the CSS styles specified for a specific DOM element? Do I need to iterate through all the CSS style names? Could there be a more sophisticated approach? How exactly does Firebug achieve this task? Appreciate any insights ...

Issues with Disabling the Browser's Back Button with jquery

I have been attempting to prevent the back button from functioning in a specific Browser, Microsoft Bing. Interestingly, my code works only if I click somewhere in the window first. If I don't click anywhere and try to go back directly, it still allow ...

Changing the sliding underline effect in a jQuery navigation bar

Recently, I implemented a sliding underline element in my navigation bar. The idea is that when a link is hovered over, the underline smoothly transitions to that element. You can take a look at the codepen example here: https://codepen.io/lucasengnz/pen/e ...