Border around the viewport of an IE document

There seems to be a thin 2px border surrounding the document viewport in some versions of Internet Explorer. This issue is not present in other browsers, making it challenging to calculate mouse positions accurately for the page and client areas. Initially, I attempted to address this by subtracting 2 from each calculation to adjust for the border.

However, after testing in various versions of IE and different IE embedding programs, I discovered that there are cases where no border exists. Therefore, simply checking for IE and subtracting 2 will not suffice.

Is there a reliable way to determine if the document viewport has a border in IE?

Example1: How to find the mouse position inside an object

<html>
<head>
    <script>
        var isIE = (!window.addEventListener);
        window.onload = function(){
            var foo = document.getElementById('foo');
            if (isIE) foo.attachEvent('onmousemove',check_coords);
            else foo.addEventListener('mousemove',check_coords,false);
        }
        function check_coords(e){
            var foo = document.getElementById('foo');
            var objPos = getPos(foo);
            if (isIE) mObj = [window.event.clientX+document.body.scrollLeft-objPos[0], window.event.clientY+document.body.scrollTop-objPos[1]];
            else mObj = [e.pageX-objPos[0], e.pageY-objPos[1]];
            foo.innerHTML = mObj;
        }
        function getPos(obj){
            var pos = [0,0];
            while (obj.offsetParent){
                pos[0] += obj.offsetLeft;
                pos[1] += obj.offsetTop;
                obj = obj.offsetParent;
            }
            return pos;
        }
    </script>
    <style>
        body{
            margin:0px;
            padding:0px;
        }
        #foo{
            border:3px solid black;
            position:absolute;
            left:30px;
            top:52px;
            width:300px;
            height:800px;
            background-color:yellow;
        }
    </style>
</head>
<body>
    <div id='foo'>Test test</div>
</body>
</html>

At coordinate [0,0], Internet Explorer (versions with the border) returns [2,2]

Example2: Determining scrollbar width

alert(screen.width-document.body.clientWidth);

For instance, with a scrollbar width of 17px, Internet Explorer (versions with the border) returns 21px without taking into account the 2px border on each side.

UPDATE:
After further investigation, it turns out that the 2px border was actually a default style applied to the body tag. Apologies for the confusion earlier! To correctly obtain the viewport border without altering the page (this applies specifically to IE calculations), you should use .currentStyle['borderWidth']. It appears that the script functions properly in all other browsers without requiring a border check. When subtracting border-widths, ensure you consider both borderTopWidth AND borderLeftWidth.

In conclusion, I have awarded the bounty to Samuel for being the first to suggest that it might be a default browser style causing the border.

Answer №1

Have you considered the possibility that it could be related to the body tag's styling? Certain browsers may apply default styles to the body tag. To ensure consistency, I make it a habit to include the following in my stylesheet:

body{
    padding:0px;
    margin:0px;
}

This helps maintain control over the layout and appearance of the webpage.

Answer №2

To achieve a higher level of consistency across different browsers, consider incorporating a CSS reset such as the one developed by Eric Meyer (link here) or utilizing a contemporary front-end framework like HTML5 Boilerplate (link here).

Answer №3

function getPosition(element){

var position = [0,0];

while (element.offsetParent){

position[0] += element.offsetLeft-1;

position[1] += element.offsetTop-1;

element = element.offsetParent;

}

return position;

}

</script>

<style>

body{ 
margin:0; 
padding:0;
border:none;
outline:none;
}

#foo{
border:3px solid #000;
position:absolute;
top:50px;
left:50px;
margin:0;
padding:0;<br>
outline:none;
width:300px; 
height:800px; 
background-color:yellow; 
}
</style>

</head>

<body>

<div id='foo'>Test test</div>

</body> </html>

Answer №4

I encountered a similar issue and managed to solve it by using this approach: document.body.clientLeft is typically zero, but in cases where IE's viewport has insets, its value becomes 2. To counteract this, you can subtract this value from the coordinates of the event.

var x = e.clientX + document.body.scrollLeft - document.body.clientLeft;
var y = e.clientY + document.body.scrollTop - document.body.clientTop;

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

What is the process for sorting non-integer records in MySql?

I need help sorting MySQL records in descending order to retrieve the most recent entry. Here are the records stored as strings: 1/2017 1/2018 2/2017 2/2018 3/2017 I specifically want to retrieve the value 1/2018. The entries correspond to each year an ...

Preloading and rendering an image onto a canvas in a Vue single-page application is not functioning as expected

I am working on a Vue 3 SPA where I am manipulating canvas elements. To preload my image, I am using the function provided below: async preloadLogo () { return new Promise( (resolve) => { var logo_img_temp = new Image(); const logo_s ...

Exploring methods to iterate through an extracted div using Selenium in Python

In my previous step, I encountered a challenge while trying to extract text from a div. After several hours of troubleshooting, I was finally able to achieve the desired result. However, the next step proved to be equally difficult as I needed to implement ...

Is it possible to insert an image as the background of a specific word within a paragraph?

I am looking to enhance certain words in a paragraph by adding a brushstroke image in the background, similar to this example: https://i.sstatic.net/KzjGn.png. I attempted to use a simple background for a span, but the image exceeded the padding and was cu ...

How can I use jQuery to add a row at the beginning of a table based on the number of columns in the first row of HTML?

I'm struggling to add a new row as the first child of the tbody with checkboxes. I attempted the following code but didn't achieve the desired result. var ss = $('tbody tr')[0] var trLen=$(ss).find('td').length; ...

A guide on ensuring content overlay compatibility with Bootstrap mastheads

Currently, the webpage I am developing using Bootstrap features a masthead navbar at the top. As it stands, without any padding added, the content overlaps with the navbar. The content of each page is loaded dynamically through ajax, leading to variability ...

Utilizing media queries to customize the appearance of a jQuery accordion widget

Hello, I'm struggling with implementing a jQuery accordion for mobile platforms that destroys itself on larger screens. While my code is mostly working, I've encountered two issues: The accordion only gets destroyed when the window is resized ...

Responsive input form causes floating label to be positioned incorrectly

I have crafted a form using Django, Crispy Forms, and Bootstrap. When the screen width is above 575px, everything looks great with fields of appropriate width and floating labels in their correct position. However, when the screen width drops below 575px, ...

Creating a display of divs that flow from left to right in each row, and then stacking the rows from bottom to top using CSS and jQuery

My current dilemma involves a collection of divs that must be displayed in a specific order, but with a rather intricate layout. Essentially, this is the structure of my HTML: <div> <div class="box">1 (first in list)</div> <di ...

I am looking for assistance constructing a task management application using vue.js

I am facing an issue with developing a to-do list using Vue.js. The goal is to add tasks to the list by entering them and clicking a button. Once added, the new task should show up under "All Tasks" and "Not finished". Buttons for marking tasks as " ...

To prevent the default alignment in case the text exceeds the input length, stop the automatic alignment

I have a query regarding an input field with a maximum length of 4 characters. The appearance is such that these 4 characters seem to be delineated by borders which are actually 3 lines displayed above the input field. When I enter the fourth character, a ...

Automatically changing the URL of Vue image assets

Is there a way to prevent the URL from changing every time we release in a Vue project when using an image from another project? In my Vue project, I have this code that works fine: <v-img class="header__logo" lazy-src="@/ ...

CGI delivers HTML without displaying it properly

In my Python CGI program, I serve HTML content rather than URLs. Here is the flow: 1. Open http://jioworld.jioconnect.com/cgi-bin/loginpp.py 2. You will see an HTML form (since you haven't logged in yet) 3. Enter a dummy username and password, then s ...

Aligning buttons using JavaScript

I'm currently working on a layout with a horizontal div where buttons (referred to as letters in the code) are being created. However, I've encountered an issue where the buttons are aligning to the left side of the div and I'm unable to cen ...

Warning button covers alert box

Bootstrap warning message covering the Create Repository button. Tried setting display:block, but it didn't fix the issue. Check out this JSFiddle for more details. <a href="#"> <button style="width:250px; display:block" class="btn btn-p ...

Empty space under the banner in Wordpress theme Avada

I can't seem to locate the CSS class for a blank space that appears below the header on one of my pages. This space is situated between the header and "SERVICIOS 24 HORAS". Any ideas on how I can remove this mysterious gap? My website is built wit ...

Troubleshooting a CSS problem within mat-autocomplete component in Angular 7

While using mat-autocomplete, I noticed that when I select an option from the dropdown and then scroll through the main bar, the dropdown menu does not move along with the autocomplete input field. view image here Here is the code snippet: <td width ...

Initiate a button press upon pressing the space bar

Currently, I am only able to use the click function on the button. However, I am also interested in incorporating the enter key and space bar for this action. Button (click)="ShuffleClick()" ...

Toggle the visibility of content with jQuery or Ajax

As I work on designing a webpage at this link, I am facing an issue with the projects tab due to my lack of experience in proper protocol. The plan is to have a side menu where users can select different content options (CAD, FEA, MATLAB, etc.), which wi ...

div within a fixed width container with 100% width inside

Is there a way to ensure that div B has a width of 100% when nested inside Div A, which already has a fixed width? .divA{width:600px; margin:0 auto;} .divB{width:100%; height:20px; background-color:black} .filler{height:800px; background-color:grey} ...