Unforeseen box model quirks found in modern browsers when styling <table> elements

Here is a sample HTML document that demonstrates the issue:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Box model test</title>
        <style type="text/css">
            html,body { margin:0; padding:0; background-color:#808080;}
            #box { position:absolute; top:20px; left:20px; background-color:Green; }
            #tbl { position:absolute; top:20px; left:40px; background-color:Red; }
            .common { width:20px; height:30px; border-bottom:solid 1px black; }
        </style>
    </head>
    <body>
        <div id="box" class="common"></div>
        <table id="tbl" class="common"></table>
    </body>
</html>

When using a combination of HTML5 doctype and X-UA-Compatible meta tag, most modern browsers should switch to their most standards-compliant mode. In this document, there are two absolutely-positioned elements - a <div> and a <table>. These elements have identical width, height, and border properties in CSS. However, when rendered in various browsers, they show unexpected differences as seen here:

Screenshot Link

The <div> (green) follows the box model rules where the content area includes the border height. On the other hand, the <table> (red) has its content area not including the border height. This inconsistency raises questions about the interpretation of element height in relation to borders. Is this behavior documented in W3C specifications? Can it be relied upon in future implementations?

Note: Testing was done on IE 7, IE 8, Opera 10.10, Safari 4.0.4, Chrome 3.0, Firefox 3.5, all showing the same rendering on Windows 7 x64.

Answer №1

Currently, I am encountering similar issues when trying to specify the height of a cell within a table. It seems that tables utilize the border-box model instead of the content-box model. I have a strong suspicion, although I do not have concrete evidence at this time, that this design choice was made to ensure compatibility with table-based layouts. For instance, if you set a width of 100% and add a border or margin, you may encounter scrolling problems (especially if your table occupies the entire window).

An advantage of the border-box model is its ability to easily achieve percentages minus pixel values. This can be challenging with the content-box model, but straightforward with the use of the border-box model. Thankfully, CSS3 now allows for the selection of the border-box model for block-level elements, a feature that could have been beneficial from the beginning. I recall hearing that IE5 initially implemented the border-box model due to specifications at the time, which were later altered.

A careful examination of the CSS specification related to table layout would likely confirm whether this hypothesis holds true.

The issue I am currently facing involves setting a cell's height to 200px, adding a 10px border and 20px padding, yet finding that getCalculatedStyle() reports a height of only 140px. This discrepancy arises despite my explicit instruction to set the height to 200px.

Sincerely, Allan

Answer №2

Quite intriguing. I have managed to make them behave in the same way, but only by including a <tr> and <td> in the table and setting both to border-collapse: collapse and display: table, as demonstrated here:

http://jsbin.com/ijila

While not a definitive solution, it may shed some light on the issue.

Answer №3

Include a border-collapse: collapse property in the CSS for the element with id #tbl. Ensure that cell padding is still retained.

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

Here's how you can pull information from a MySQL database and showcase it on an HTML

I successfully created a database in MySQL where users can enter their details on /upload.html, which then get stored in the test2 database and person_list table. However, I am facing an issue with displaying this data on /download.html. My goal is to hav ...

Craft an engaging and dynamic Image map with SVG technology to elevate responsiveness and interactivity

I'm currently working on a website and I need to create two clickable sections on the home page. Each section will lead to a different specialization of the company. To achieve this, I decided to use a square image split into two right-angled triangle ...

Generate a single-page PDF document mirroring the content of a website

I'm facing a dilemma. I need to generate a one-page PDF file without any gaps between the pages. Despite trying numerous plugins, add-ons, scripts, and software, all of them produce multiple pages. What I really require is to have all the pages in a s ...

spacing for margins of table elements generated dynamically

Despite setting the width for the td tag, there seems to be a significant gap between the Odo and Location columns. Below is a basic table structure: <table width="95%" border="0" cellspacing="0" cellpadding="2"> <tr> <td wi ...

Having numerous calls to isNaN within a JavaScript if-else statement

I've created a JavaScript function that generates div elements and styles them based on user input values. However, I'm facing an issue when trying to display a warning message if the input is not a number. Currently, I'm unable to create th ...

Tips for clearing a saved password in a browser using Angular.js and Javascript

There is an issue with the password and username fields in my Angular.js login page. When a user clicks on the 'remember me' option in their browser after logging in, the saved username and password are automatically displayed in the respective f ...

Why is the Django image URL redirecting to an HTML template instead of the media file?

I created a Django application with a template that includes an image. However, I encountered an issue where the server is attempting to display the image using the redirect link from the HTML template instead of retrieving it from the media file. For ins ...

What is the source of this additional space?

To view my Codepen project, check out this link: http://codepen.io/leongaban/pen/aFJfs I have noticed that the spacing issue is related to the .email-tip-icon and the tip-message classes. When I remove them, the extra space disappears. Even though both th ...

Transmitting video through a local area network

I am seeking a solution to stream video content to a local network with potentially over 1000 viewers. The streaming will need to be accessible via web browsers such as Internet Explorer, Chrome, and Firefox, as not all users have internet access due to co ...

The navigation bar is showing my logo in a blurry and small format when viewed on a desktop

Currently, Twitter Bootstrap is being utilized on my website and a high-resolution logo has been successfully uploaded. The dimensions of the logo are 529 x 100 pixels, and here is the relevant CSS: .navbar-brand { float: left; padding: 12px 15p ...

What could be the reason my vue.js button is not generating a fresh textarea?

I am currently developing my first Web App with vue.js and I'm trying to implement a feature where clicking a button will generate a new textarea. It seemed to be functioning correctly when tested on jsfiddle, but once I tried running it in visual stu ...

Tips for incorporating IntersectionObserver into an Angular mat-table to enable lazy loading功能?

I am looking to implement lazy loading of more data as the user scrolls down the table using IntersectionObserver. The container I am using is based on the Bootstrap grid system. However, despite using the code below, the callback function is not being tri ...

Update the color of a span in JQuery when its value equals 0

Currently, my goal is to update the color of a span tag only if its value is 0. I attempted to achieve this with the following code: $('span.number:contains("0")').css('color', '#C1C1C1'); However, this code also changes the ...

Designing a div with a proportional size amidst elements of fixed dimensions

I need help figuring out how to create a dynamically sized div based on the browser's height. I'm not sure where to start or how to approach this problem. The layout I have in mind includes a 50px high header, followed by the dynamic div and the ...

I am still receiving an empty dropdown value despite implementing ng-selected

I am having issues with using ng-selected to retrieve the selected value from a dropdown. Instead of displaying the selected value, it appears blank. Here is the code snippet I have tried: <div> <select id="user_org" ng-model="selectedorg.all ...

Is it time to shake up the location of your Toastr

Recently, I've been working on implementing Toastr from this GitHub repository. While I am able to receive alerts and toasts as intended, I'm running into issues when trying to apply custom options. It seems to only stick to the default settings ...

What is the best way to create distance between my buttons?

Can anyone help me with an issue I'm facing where adding margin to my buttons is causing the last button to jump down a row? I have an idea of what might be happening but I'm unsure how to solve it. Below is the CSS code for the buttons, any sugg ...

Is it possible to create repetitive events using loops in jQuery?

When it comes to specificity in coding... $("#ht_1").click(function () { alert("hello"); }); $("#ht_2").click(function () { alert("hello"); }); // here's what I attempted for (i = 1; i <= 2; i++) { $("#ht_" + i).click(function () { ...

The Alert Component fails to display when the same Error is triggered for the second time

In the midst of developing a Website using Nuxt.js (Vue.js), I've encountered an issue with my custom Alert Component. I designed a contact form on the site to trigger a specialized notification when users input incorrect data or omit required fields ...

What is the best way to apply maximum height to an element using CSS?

Looking for help with my website: I made some changes in the code using Firebug and identified these elements that I want to modify (but changes are not live yet) The following code represents the elements on my site: This is the HTML code: <di ...