Rules of HTML tables extend to the border size

Check out this HTML code snippet:

div {
  border: 2px solid red;
  height: 50vh;
  width: 50vw;
}

table {
  border: 1px solid;
  table-layout: fixed;
}
<div>
  <table rules="cols">
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Address</th>
      <th>Phone No</th>
    </tr>


    <tr>
      <td>Antony</td>
      <td>20</td>
      <td>USA</td>
      <td>12345</td>
    </tr>
    <tr>
      <td>Das</td>
      <td>20</td>
      <td>UK</td>
      <td>12345</td>
    </tr>

    <tr>
      <td>Michel</td>
      <td>20</td>
      <td>USA</td>
      <td>12345</td>
    </tr>

  </table>
</div>

The initial output is shown here:

I would like the outcome to resemble this instead:

To achieve this effect, I require the border rule to extend until the end.

Adjusting the table's height currently resizes along with the content,

I am in need of resizing only the column rules. Any suggestions will be greatly appreciated.

Answer №1

To ensure the last row of your table expands with empty space, you can add a tall div within one of the columns in that row. Keeping the width minimal (such as 1 pixel) will prevent the column from widening beyond the content width. Set the height to a large value, taking advantage of the parent div's overflow hidden property.

Setting vertical-align to top is essential for maintaining alignment, especially when rows wrap and ensuring content appears correctly positioned.

div {
  border: 2px solid red;
  height: 50vh;
  width: 50vw;
  overflow:hidden;
}

tr {                             
    vertical-align:top;
}

.empty-space {                   
    width:1px;
    height:99999px;  
}

table {
  border: 1px solid;
  table-layout: fixed;
  
}
<div>
  <table rules="cols">
   ...
  </table>
</div>

It's important to note that refining your code may be necessary for better control over styling. Utilizing class names in your CSS allows targeted application of styles rather than affecting all elements universally.

Answer №2

Hello, it's important to adjust the width and height of the TABLE based on its parent tag, which is the DIV

      div {
        width: 100%;
    height: 100%;

    text-align: center;
}

table {
  border: 1px solid;
  table-layout: fixed;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Custom Title Here</title>
  </head>
  <body>
    
    <div >
      <table rules="cols" w>
        <tr>
          <th>Name</th>
          <th>Age</th>
          <th>Address</th>
          <th>Phone No</th>
        </tr>
    
    
        <tr>
          <td>Antony</td>
          <td>20</td>
          <td>USA</td>
          <td>12345</td>
        </tr>
        <tr>
          <td>Das</td>
          <td>20</td>
          <td>UK</td>
          <td>12345</td>
        </tr>
    
       <tr>
          <td>Michel</td>
          <td>20</td>
          <td>USA</td>
          <td>12345</td>
        </tr>
    
      </table>
    </div>
  </body>
</html>

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

Can you explain the variance between e-resize and ew-resize cursor styles?

Can you explain the differences between these cursor types? I'm noticing that they all appear as the same cursor in Chrome on Windows. .e-resize {cursor: e-resize;} .ew-resize {cursor: ew-resize;} .w-resize {cursor: w-resize;} <p>Mouse over t ...

Grid item overlay

Currently, I am facing an issue with React where I am attempting to place an overlay on top of each grid item in a grid. Despite trying multiple approaches, the overlay appears beneath each grid item instead of over it. Even setting the position: absolute ...

Code executing twice instead of once in Javascript

Having trouble with a script in my demo below. When I enter "first input", submit, then click on the returned "first input", it alerts once as intended. However, upon refresh, if I enter "first input", submit, then add "second input", submit, and finally c ...

The Javascript function is designed to function exclusively with onclick events or setTimeout functions

I am currently working on a website that I want to be responsive to the window size. However, I am facing an issue with vertical alignment. I have created a jQuery function to handle this, and it works well for divs with images but not so well for a div wi ...

Ways to obtain jquery object for replaced DOM element

Is there a way to select the new content after using the replaceWith function in my plugin? I want to chain my plugin for the new content. $.fn.customPlugin = function() { this.replaceWith('<div>New Content</div>'); }; So, ideal ...

Identify Horizontal Swipe Gestures on Page-level

I am currently focused on ensuring accessibility for users utilizing voiceover technology. While navigating their phone, these individuals rely on right and left swipes to interact with elements on a page. I am seeking to implement swipe detection at the ...

Resolving a CSS Layout Dilemma: How to Overlay Sidebar on Wrappers

I've run into a challenge while attempting to position a sidebar over page wrappers. The issue with absolute positioning arises when the sidebar needs to align with the corner of the blue header. I have contemplated using JavaScript to maintain its cu ...

Extracting information from JSON using arrays

I'm facing a bit of a challenge with this one. I've been trying to use jQuery on my website to update an element. It works perfectly fine without using an array of data in JSON, but as soon as I introduce an array of data, it stops functioning. I ...

Uncertainty about the integration of a JavaScript file into a PHP file

Having two PHP files where one is executed through a URL and makes AJAX calls to the second PHP file can present challenges. Sometimes, the AJAX results return HTML content with JavaScript events that do not work as expected. To solve this issue, I have in ...

Minimize or conceal iframe

This iframe contains a Google form that cannot be edited. I am looking for a way to close or hide this iframe, either through a button, a popup window button, or without any button at all. The $gLink variable holds the Google form link through a PHP sessio ...

How can AngularJS handle sorting based on the start date and end date?

Is there a way to filter the items by Start and End Date, based on the invoice_date, using the date range functionality in a meanjs app? I am facing an issue where the date filter functions work perfectly in Plunker and my localHost production environm ...

arrangement of form labels in material-ui

Is there a way to configure the labels in Material-ui + react forms to be displayed beside input fields for better readability? For example: name [input] title [input] instead of name [input] title [input] I have looked through the documentation b ...

Troubleshooting Dynamic Input Issue - Incorrect Appending Behaviour - Image Attached

I am working on a project that involves adding input fields dynamically using JavaScript. However, I've encountered an issue where the styling of the first input field is not applied correctly when it is clicked for the first time. You can see the dif ...

How to apply custom styling to a specific element within an Angular Material 2 component using CSS based on its Id or

My EntryComponent features a material button menu where I attempted to customize the default style using ::ng-deep. However, the changes affected all button components in the parent Component as well. <style> ::ng-deep .mat-button{ max-width: 50 ...

Unable to display menu content text using jQuery

Currently experimenting with jQuery to create a dynamic submenu. The goal is to have a sub menu appear when the main menu is clicked, and then disappear when an item in the sub menu is selected, revealing additional information within a div. Unfortunately, ...

Ways to Achieve the Following with JavaScript, Cascading Style Sheets, and Hypertext

Can someone help me convert this image into HTML/CSS code? I'm completely lost on how to do this and don't even know where to start searching for answers. Any assistance would be greatly appreciated. Thank you in advance. ...

What reasons could explain why the more efficient approach of offering an initial portion of data is not faster in practice?

My website contains numerous pages with a plethora of small images that need to be loaded. To enhance user experience, I devised two methods to first display a subset of the content without waiting for the entire page to load. The data is stored in a .json ...

Styling for main banner image on large desktop screens and mobile devices

I need help with website design and CSS solutions. Currently, I have a WordPress website with a onepager theme. At the top of the page, there is an image that almost fills the screen along with a title. My concern is how this image behaves with different b ...

Customizing Eclipse Outline View for Cascading Style Sheets (CSS) Files

Is there a way to modify Eclipse's Outline view for CSS files? Specifically, how can I make the outline display comments and create collapsible ranges for groups of CSS rules? Include comments in the outline Create collapsible ranges for groups of ...

What is the reason for the presence of additional space below when using x-overflow:hidden?

When I place two spans inside each other and use overflow-x:hidden on the inner span, it results in extra space below the inner span. Why does this happen? <span style="" class="yavbc-color-tip"><span style="">Some text</span></span&g ...