What changes can I expect if I switch from inline to block display for a table?

Why are border-spacing and border-collapse still working even when I set the display property of the table element to block? I thought these properties only worked when the display was set to table, but it's still functioning. Why is that?

I didn't expect the border spacing to be effective.

Take a look at the following code:

    #tab {
      display: block;
      border: 2px solid black;
    }

    .row {
      display: table-row;
    }

    .data {
      display: table-cell;
      border: 2px solid;
    }

    table {
      border: 2px solid;
      display: block;
    }

    td,
    tr {
      display: block;
      padding: 0px;
    }
<div id="tab">
    <div class="row">
      <div class="data">Lorem, ipsum.</div>
      <div class="data">Lorem, ipsum.</div>
    </div>
</div>
  <table>
    <tr>
      <td>Lorem, ipsum dolor.</td>
      <td>Lorem, ipsum dolor.</td>
    </tr>
  </table>

Answer №1

To gain better clarity, let's visualize the CSS box tree.

We'll start by examining a simple HTML table.

<table>
  <tr>
    <td>Lorem, ipsum dolor.</td>
    <td>Lorem, ipsum dolor.</td>
  </tr>
</table>

It's important to note that the tr element in the HTML code will automatically be wrapped in a tbody element by the HTML parser, resulting in an identical DOM document as shown below:

<table>
  <tbody>
    <tr>
      <td>Lorem, ipsum dolor.</td>
      <td>Lorem, ipsum dolor.</td>
    </tr>
  </tbody>
</table>

By adding borders and defining border-spacing through CSS, we create our initial snippet:

<table border style="border-spacing:1em">
  <tbody>
    <tr>
      <td>Lorem, ipsum dolor.</td>
      <td>Lorem, ipsum dolor.</td>
    </tr>
  </tbody>
</table>

Now, let's examine the structure of the DOM document and the Box Tree. It is stated in the CSS Tables Level 3 draft specification that the border-spacing property applies to "table grid boxes when border-collapse is separate".

https://i.sstatic.net/ggpuu.gif

If the display property of the table element is set to 'display:block', there will be no change visible in the displayed table.

<table border style="border-spacing:1em; display:block">
  <tbody>
    <tr>
      <td>Lorem, ipsum dolor.</td>
      <td>Lorem, ipsum dolor.</td>
    </tr>
  </tbody>
</table>

In this scenario, the table element does not generate the usual table wrapper box and table grid box but just functions as a standard block box. However, due to the incomplete CSS table structure, missing pieces are automatically generated to complete it, resulting in a Box Tree almost identical to that of the previous case but with a different process:

https://i.sstatic.net/dg8vD.gif

The property 'border-spacing' is inherited and applied to the block container box, with the anonymous table grid box inheriting and implementing the value.


It's worth noting that if all elements within the table, including internal ones, are set to display:block, the border-spacing feature will not take effect:

table, table * {
  display:block;
}
<table border style="border-spacing:1em">
  <tbody>
    <tr>
      <td>Lorem, ipsum dolor.</td>
      <td>Lorem, ipsum dolor.</td>
    </tr>
  </tbody>
</table>

Since the entire table structure is eliminated with all elements set to 'display:block', CSS does not need to create the anonymous table grid box.

https://i.sstatic.net/5JMQf.gif

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

show up on page where I am located, not just in the center

My AJAX code displays 81 products per page. When I click the add to cart button, a div appears in the center of the page. However, when I add a product to the last or bottom of the page, the div always appears in the center and I have to scroll up to see ...

What is the best way to implement multiple store filters in VueJS?

Just dipping my toes into the world of VueJS with my very first major project. I've incorporated 2 filters in this project for my elements - one is a search bar and the other involves checkboxes. Taking a look at my code, you'll notice a computed ...

Can you explain the purpose of the search_/> tag used in this HTML code?

I came across this code snippet while working on creating a customized new tab page for Firefox. <input class="searchBar search_google" type="text" name="q" placeholder="Google" search_/> However, I'm confused about the search_ ...

Is Javascript the best choice for managing multiple instances of similar HTML code?

I am currently facing the challenge of dealing with a lengthy HTML page consisting of around 300 lines of code. The majority of the code involves repetitive forms, each identical except for the ID (which varies by number). Is it considered appropriate or ...

Displaying dynamic content in JavaScript using Galleria module

Hello and thank you for taking the time to check out my question. I've encountered a little issue that I can't seem to solve by myself. I'm hoping someone could lend me a hand. On my website, there is a dynamic field filled with a code from ...

Extract the HTML content from a file stored on the computer

Currently, I am utilizing Google App Engine alongside Python. My goal is to retrieve the tree structure of an HTML file located within the same project as my Python script. Despite attempting numerous approaches, such as using absolute URLs (for example ht ...

Box without a border wrapping around it

I can't seem to figure out why the border I applied to a child div is not completely enclosing the element. I created two boxes using div tags and added a border to one of them. #box { height: 500px; width: 500px; background-c ...

What is the best way to adjust the size of an image to the viewing area

In my application, users can take photos either horizontally or vertically. These images are then displayed in a gallery on a webpage, and when clicked on, they expand using a Modal. My issue arises with horizontal images looking smaller than vertical one ...

Ways to include scrolling specifically for one template

As a newcomer to frontend development, I have recently started learning Vue and the Quasar Framework. I am currently working on a table and trying to set a fixed table name with only the table items scrollable. <template> <q-table virt ...

Creating responsive CSS layouts for various device sizes

My webpage features a form with a prompt for the input box, followed by the input box itself. I want to implement two different layouts based on the device accessing the page. For cell phones, I want everything stacked vertically. However, for computers, ...

HTML Date input field not respecting locale settings and defaults to mm/dd/yyyy format

When using an html date input in an ng-repeat scenario, I noticed that it defaults to mm/dd/yyyy even though my computer's regional settings are set to dd/mm/yyyy. <script src="//unpkg.com/angular/angular.js"></script> <body ng-app&g ...

PHP: the images uploaded are not located within the designated folder

Having trouble uploading multiple images to a folder and saving them in the database? The images are not moving to the folder, even though the code works on localhost. Here is the code snippet: var abc = 0; // Declaring and defining global incremen ...

The live feature is operational, but unfortunately the on feature is not functioning

I am facing an issue in my code where replacing .live with .on is not working. I have been trying to identify the problem but haven't found a solution yet. $(document).ready(function () { /* Reading the data from XML file*/ $.ajax({ ...

Differentiating card heights on hover in bootstrap can be achieved by adjusting the CSS properties specifically for each card

I'm currently working with bootstrap cards and have added hover effects to them. However, I'm facing an issue where on larger devices, hovering over one card enlarges all the other cards as well. This problem doesn't occur on smaller devices ...

Random variable without repetition in SASS

I have a unique project where I am tasked with incorporating a list of 5 specific colors into 10 different div elements. The challenge is that each color must be used twice and in a completely random order without any repetition of colors. Although utilizi ...

Personalize your Django Form: Tailoring the appearance of fields

In my form class, I have various fields, including first and last name. My objective is to update the form in my HTML file to accept input and pass it into a model object without altering the display of the form. However, the issue arises when the fields a ...

Layering digital sheets of paper and rearranging them with the help of CSS

I want to create a visual representation of a stack of paper sheets as a metaphor. The idea is to neatly stack the sheets on top of each other, with only the header of each sheet visible and the rest of the content covered. !-------------- | Sheet 1 +--- ...

Displaying a division when a button is pressed

Despite my best efforts, I can't seem to get the chosen div to show and hide when the button is pressed. <button id="showButton" type="button">Show More</button> <div id="container"> <div id="fourthArticle"> <i ...

Tips for updating images with HTML and CSS?

Sorry if this question seems strange, but I have a doubt that I need clarification on. Regarding assumptions: If I have 5 images, is it possible to change each image one by one (like a slider) using only HTML and CSS styles, without relying on jQuery or J ...

Once I press the button to switch all the other buttons to dark mode, it only successfully changes them once

I created a button that switches the entire page to dark mode. I also have some dark buttons that I want to switch to light when the dark mode button is clicked. The issue is that while changing the page to dark mode works, the dark buttons only toggle ...