Is a table cell's border considered as part of its content?

A discrepancy is observed in the rendering of a document on Firefox and Chrome browsers:

<!DOCTYPE html>
<html>
    <head>
        <title>Testing table rendering</title>
        <style>
            table {
                border-collapse: collapse;
                border-spacing: 0;
                padding: 0;
                background-color: #ffffff;
                border: 2px solid black;
            }
            table td {
                border: 1px solid black;
                padding: 5px;
            }
            tr.testRow > td {
                border-left-width: 0;
                border-right-width: 0;
                padding: 0;
            }
            tr.testRow table {
                border-color: #ff0000;
            }
        </style>
    </head>
    <body>
        <table>
            <tr>
                <td>cell1.1</td>
                <td>cell1.2</td>
                <td>cell1.3</td>
            </tr>
            <tr class="testRow">
                <td>cell2.1</td>
                <td colspan="2">
                    <table>
                        <tr>
                            <td>internal table cell</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
</html>

Firefox: https://i.sstatic.net/ppwnq.png

Chrome: https://i.sstatic.net/PX8Ub.png

It's noted that Firefox doesn't consider the bottom-right cell's border as part of its content, causing the "internal table" to render slightly to the right of the border of the cell above it. However, Chrome displays the "internal table" aligned with the border of the cell above.

In standard mode for both browsers, which one follows the correct behavior? Additionally, how can modifications be made to the code so that both browsers render the content in the same way as Chrome currently does (preferred behavior)?

Answer №1

Which browser is displaying the correct behavior?

Both browsers are technically behaving correctly.

They are each interpreting the specifications in their own way and have unique methods of compiling and rendering a page on your screen.

How can I adjust the code to make them both behave like Chrome?

You can achieve this by utilizing the CSS property box-sizing.

If you set it to border-box, the padding and border will always be included within the width of an element.

.box {
  width: 200px;
  height: 75px;
  border: 5px solid black;
  padding: 10px;
  margin-bottom: 10px;
}
.with_sizing {
  box-sizing: border-box;
}
<div class="box">This is a box with no box sizing</div>
<div class="box with_sizing">This is a box with box sizing</div>

Observe both boxes above, and you will notice that the top element only contains content within 200px, while adding padding and border on top. In contrast, the bottom element with box-sizing: border-box will encompass the entire area within 200px.

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

The placement of a material-ui component at the beginning of the render method can impact the styling of the following components in the hierarchy

After creating some styled components, I noticed that they were not rendering correctly for me. However, when I transferred the code to a codesandbox environment (which had a material-ui button in the template), the components rendered perfectly fine. It w ...

encountering a Zend Framework error

Looking for help with printing data after an inner join of two tables in table format. I encountered the following error in the code below: Parse error: syntax error, unexpected end of file in /home/bina/public_html/studentzend/application/views/scri ...

Convert HTML form input into a JSON file for safekeeping

<div class="email"> <section class="subscribe"> <div class="subscribe-pitch"> </div> <form action="#" method="post" class="subscribe-form" id="emails_form"> <input type="email" class="subscribe-input" placeholder="Enter ema ...

The iPython terminal is not displaying properly due to a constrained screen width

When my iPython displays a long list, it appears as one tall column instead of utilizing the full width of the terminal screen. I have attempted to adjust the CSS in '.ipython/profile_default/static/custom/custom.css' by setting '.container ...

What is the hexadecimal color code for a specific element's background?

How can I retrieve the background color code of a specified element? var backgroundColor = $(".element").css("background-color"); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="elemen ...

Issue with grid breakpoints for medium-sized columns and grid rows not functioning as expected

I'm working on building a grid layout from scratch in Vue.js and have created my own component for it. In my gridCol.vue component, I have defined several props that are passed in App.vue. I'm struggling to set up breakpoints for different screen ...

Extracting URL from HTML automatically

I'm in the tedious process of copying multiple URLs from a website, which involves clicking on numerous links and then pasting the URL (along with other information) into an Excel file. This method is incredibly time-consuming, especially since I have ...

Fade In/Out Scroll Effect

As the user scrolls down the page, I have a slider that smoothly slides from right to left. What I'm trying to achieve is for the slider to fade out when the last slide is no longer in view during downward scrolling, and fade back in as the last slid ...

Concatenate a variable to the conclusion of a string using PHP

Currently, I am trying to include the id of the logged-in user in a table name. For instance, I would like to have a table named Rating_User_1 where 1 corresponds to the id of the user who is currently logged in. I have stored the user's id in a varia ...

Trouble with Fancybox 2 and CSS classes not applying correctly

Despite my previous successful experiences with fancybox, I am currently facing an issue where the CSS styling is not being applied as expected. The large images appear grouped correctly, but without any styling, causing them to shift to the right after ap ...

Positioning pop-up windows using Javascript and CSS techniques

In my web application, there is a search field that allows users to enter tags for searching. The window should pop up directly below the search field as shown in this image: Image I am trying to figure out how the window positioning is actually d ...

In what way could the border exceed the dimensions of the div?

<head> <meta charset="UTF-8"> <title>Document</title> <style> #Tri{ width:0px; height:0px; border-top: 50px solid yellow; border-right:50px solid red; border-bottom:50px solid green; border-left:50px solid blue; } </style&g ...

Avoid unnecessary extra margin on CSS div elements

Currently working with Bootstrap4. In a row with 2 col-md-6 (totaling 12 columns for the row). However, experiencing an unwanted margin on the left column as shown in the image below. How can I remove it? https://i.sstatic.net/u5oix.png html: <hea ...

Guide to creating a toggle button

Can someone help me create a button that toggles between displaying block and display none when clicked? I've been trying to use Classlist.toggle with JavaScript, but I'm not sure if I have the correct classes targeted. let showCart = documen ...

Tips for updating the border color of a navigation tab

Struggling with changing the border color for Tab 2, while Tab 1 works fine. All my custom CSS comes after Bootstrap 5 CSS, so why isn't it overriding the changes? CSS .nav-tabs { border-color: black; } .nav-tabs > li > a { border-colo ...

Bootstrap link causing alterations to my CSS code

After incorporating Bootstrap into my HTML, I noticed that it caused some unexpected changes to my CSS code. Original CSS: https://i.stack.imgur.com/EsE6J.jpg Modified CSS after applying Bootstrap: https://i.stack.imgur.com/yH1r9.jpg Link to Bootstrap ...

Creating a layout with Bootstrap 4 cards split into two rows

I need to change my layout from 3 columns to 2 columns. I know I can achieve this using CSS like the code snippet below, but I'm curious if there's a built-in Bootstrap method for this: .card-columns { -webkit-column-count: 2; -moz-column-co ...

Spacing between columns in Bootstrap5 for responsive design

In my Django app, I am utilizing Bootstrap for styling columns. I have successfully created a responsive layout that stacks vertically after a specific breakpoint using classes. Now, my goal is to add some space or gutter between the columns while maintai ...

Guide on changing image source using a function

I am looking to dynamically set the img src="" attribute using a JavaScript function that changes the image based on a variable and checks its values: Here is the JavaScript code in the file: function myFunctionstatus(){ var ledactual = document.getE ...

Having trouble creating a full-screen modal with NgbModal by passing content as a component?

I've been using Bootstrap widgets and trying to create a full-screen modal where the header sticks on top, the footer stays at the bottom, and the body scrolls in the middle. Initially, I found a simple HTML solution for this: However, when I want to ...