Ways to left-align the columns themselves, rather than the content within them, in a table

Creating a table without knowing the number of columns in advance can be tricky. If you have a wider first column and want the rest to be the same width, it's important to align them properly so they fill the space on the left and leave room on the right.

Here is an example layout you may be aiming for:

column1           |  column2 | column3
----------------------------------------------------------------- 
value1            |        2 |       3  
-----------------------------------------------------------------

The challenge lies in aligning all columns except the first one to the right while ensuring the last column takes up the entire table width to show the bottom border. Your current CSS provides some styling but doesn't fully achieve the desired layout.

You may need additional CSS properties or adjustments to get the perfect layout you're looking for!

Answer №1

To implement this layout using flexbox and a pseudo-element ::after for the last column, you can follow these guidelines:

.mytable {
  width: 100%;  
}

tr {
    display: flex;    
}

tr:after {  
  content: '';
  flex-grow: 1;  
  border-left: 1px solid #000;
  border-bottom: 1px dashed #000;
}
  
td {
  width: 100px;
  border-left: 1px solid #000;
  border-bottom: 1px dashed #000;
  text-align: right;    
}

td:first-child {  
  width: 200px;
  border-left: 0;      
  text-align: left;
}
<table class="mytable">
  <tr>
    <td>column1</td>
    <td>column2</td>
    <td>column3</td>
  </tr>
  <tr>    
    <td>value1</td>
    <td>2</td>
    <td>3</td>
  </tr>
</table>


<h3>You can extend this table structure to include more columns:</h3>

<table class="mytable">
  <tr>
    <td>column1</td>
    <td>column2</td>
    <td>column3</td>
    <td>column4</td>
    <td>column5</td>
  </tr>
  <tr>    
    <td>value1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
  </tr>
</table>

Answer №2

To accomplish this, ensure that the width of the initial td is set to 100%, enabling it to utilize all available space. For the remaining tds, establish a minimum width using min-width to maintain consistency.

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

My DIV elements are not adapting to the row and column breakpoints as expected. Can anyone help me understand why this is happening?

Currently, I am working on setting up a row of highlighted products on my website. It displays fine with 5 products on desktop view, but the challenge arises when it comes to viewing it on medium size devices where I want only 2 products to show, and event ...

Adjusting image size within floating containers

I am working on a design with two floating divs, one on the left and one on the right, both nested within a main div. Each floating div contains an image that needs to be resized to the maximum size possible without differing sizes. This is what I curren ...

Out of reach: Menu div slides up on click outside

<a class="btn">Click ME </a> <div class="content"> Content </div> $('.btn').click(function(){ $('.content').slideToggle(); }); Check out the example here Everything is working properly, but I have a q ...

Position the button to the left of the input field

I need to create a button with the id #searchBtn positioned to the left of an input form with the id #searchInput. HTML: header { position: relative; z-index: 2; } .searchInputMain { width: 300px; height: 40px; } .searchButton { width: 200px ...

Which HTML tags can be activated with JavaScript to be interactive?

I'm currently diving into the world of JavaScript and one of the initial code snippets I've encountered is onclick. So far, I've seen it utilized in form buttons like this: <input type="button" onclick="checkName()" value="Check name" / ...

In this guide, learn the best way to dynamically adjust image height to match the height of any device

I am trying to make sure that the image on the front page of my website fits the height of the screen or device, and resizes responsively without cropping when the device height changes. How can I achieve this? If you need a visual example of what I mean, ...

The content on the page is not visible when viewing on an Android device in

After spending a considerable amount of time browsing StackOverFlow for coding solutions, I have come across a problem that I can't seem to find an answer for. Yesterday, I made some changes to the media queries on my site in order to incorporate a dr ...

Utilizing ng-class in AngularJS to apply dynamic classes based on conditions

In my project, I am dynamically applying different CSS classes based on the variable 'statetostartwaves'. If the variable is '0', then class 1 (preloader_pause waves) should be used. Otherwise, if it is '1', then class 2 (prel ...

Guide to utilizing the bootstrap grid system to stack below a width of 480px

I have been grappling with this issue, but unfortunately, there seems to be a lack of information on how to resolve it. I have gone through numerous old posts where individuals are "modding" the files and creating their own col-ms (medium-small) that stack ...

I'm having trouble receiving HTML content through Ajax or inserting the response into a DIV using JQuery

I'm having trouble retrieving a simple response from a controller and displaying it in a div so that I can view the output on the front end. Below is my JQuery code: $('.js-mapsearch').keyup(function (e) { var characters = $(t ...

ECharts - Version 3.1.6 Event Plugin

I am looking for advice regarding the management of two charts with reference to echars from Baidu (version 3.1.6). Is there a way to control both charts by engaging in a click event on one of them? In simpler terms, how can I capture and respond to the c ...

Is it possible to alter preact-material-components to switch to mdc-theme--dark mode, thereby changing the CSS style of all descendant components?

I recently created a preact-cli app and added the following code in Header.js: document.body.classList.add('mdc-theme--dark'); However, when a user tries to switch from the light theme to the dark theme, only the background of the app changes. ...

An empty space separating the header and the navigation bar

As a beginner in HTML and CSS, I decided to create a basic website. However, I encountered an issue with the layout - there seems to be a gap between my header image and navigation bar, but I can't figure out what's causing it. HTML <!DOCTYPE ...

It's a mystery unraveling the source of CSS margins and padding

Could someone please help me analyze this webpage? I am trying to make the center of the page white up to the navigation bar, at the bottom of the page, and horizontally up to the shadows on both sides. I'm not sure where this extra padding is coming ...

"How to change the hover background of a select element in Chrome from its default setting to something else

https://i.sstatic.net/G2deM.png Is there a way to remove the background color on hover and replace it with a different color? .drp-policydetails { border: 1px solid #dddddd; background-color: #ffffff; } <div className="form-group"> <sele ...

What is the significance of using "your-shop" as the action form without a file extension?

I've come across filenames (with file extensions) or URLs inside the action attribute of a form, but I have never seen code like this before: <form action="your-shop" name="shop_name_form" id="shop_name_form" method="post" onsubmit="return check_s ...

Struggling with aligning rows containing three divs each in Rails and bootstrap

Looking to display 3 article divs in each row on my website using bootstrap 3 grid system: <div class="container-fluid"> <h1>NEWS</h1> <div class="row"> <% @articles.each do |article| %> <div class="col- ...

Personalizing the pop-up window using window.open

When clicking a hyperlink on a page, I need to open multiple pop-up windows. To achieve this, I must use the Window.open function instead of showModalDialog. However, I have noticed that the appearance is not satisfactory when using Window.open. (Essentia ...

When applying a vertical-align property to both an anchor tag and a button with the same class, why is the span also aligned to the top?

Why is the span tag aligning to the top when I have only applied properties to the .btn class elements, and not targeted it directly? .btns { text-align: center; } .btn { text-decoration: none; border: 1px solid black; display: inl ...

What is the best way to calculate the total of multiple columns using JavaScript and jQuery?

I am looking to modify my table that has two different columns in order to calculate the sum of both. Currently, I can only add up one column using JavaScript, and duplicating the JS code with different value names for the second column is not ideal. How c ...