Adjusting the width of an HTML table using CSS

After receiving data, I format it into a table displayed on a webpage with three cells arranged side by side (Cell 0,0 : Cell 0,1 : Cell 0,2).

I am attempting to evenly distribute the cells within the div so that the text in the first cell is aligned left, the middle cell is aligned center, and the last cell is aligned right.

My CSS attempt was as follows:

td {
   width: calc(100%/3);
}
<table border=1 style="border: 1px red dashed; width: 250px;">
<tr>
<td>first</td>
<td>second with wider contents</td>
<td>third</td>
</tr>
</table>

<table border=1 style="border: 1px blue dashed; width: 250px;">
<tr>
<td>first</td>
<td>second</td>
<td>third with wider contents</td>
</tr>
</table>

The current output does not meet my desired outcome. I aim to achieve alignment consistency across multiple tables on the same page.

Answer №1

Have you considered adjusting the table to fit the full width of its parent div?

Here's an example:

div {
  width: 300px;
}

table {
  width: 100%;
  border: 1px solid black;
}


td {
  width: calc(100%/3);
  border: 1px solid black;
}

I quickly put this together, hoping it aligns with what you need: https://jsfiddle.net/nsxLfv00/

Answer №2

To adjust the alignment of the content within all three cells, consider utilizing the nth-child CSS selector

td:nth-child(1){
   text-align: left;
}

You can apply similar styling adjustments for both the middle and right columns as needed.

Answer №3

If you want to easily achieve a table layout, I recommend using Bootstrap, Flexbox, or any grid framework instead of relying solely on pure CSS. Here's an example using Bootstrap:

<div class=container>
  <div class="row">
    <div class="col-sm-4">
      Your first cell content
    </div>
    <div class="col-sm-4">
      second
    </div>
    <div class="col-sm-4">
      third
    </div>    
  </div>
</div>

Feel free to customize the table structure as needed.

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

Removing a user via Fetch API in the delete endpoint

Creating a user website that allows the admin to delete users is my latest project. I've implemented it using an Azure SQL database and have set up an endpoint called deleteUser in my controllers file. deleteUser const deleteUser = (req, res) => { ...

Issue with using indexOf in an array causing unexpected results

I am working on a JavaScript function to identify the character that appears most frequently in a given string, and then display this result in a div. Here is what I have so far: var string = "AABBCCDDEEEEEEEEEE"; var stringInput = document.getElementBy ...

Ensuring that the bootstrap5 column extends all the way to the footer

Looking to display a specific email address mailbox for a project. The goal is to have the 'Mail Card List' scrollable instead of extending beyond the page. Tried using 'max-height:;' CSS, but it sets a fixed height. Seeking a variable ...

What's the best way to adjust text color to black or white for maximum contrast when the background changes?

Currently, I am attempting to replicate the email element found on this website: .custom-blend-mode { mix-blend-mode: difference; } Although I have managed to make it work, it results in a change to the opposite color. Ideally, I would like it to remai ...

Exploring the depths of nested collections in Angular 12

As I work on my very first Angular/Firestore app, I find myself grappling with understanding how to retrieve data from nested collections. The Firestore database path that I need to access is as follows: /Customer(CollectionName)/cl0Apvalb6c0w9hltQ8AOTF4go ...

Learn the method for automatically checking a checkbox when a page is loaded in Angular

I have multiple checkboxes on a single page <div class="check-outer"> <label>Place of operation</label> <div class="checkDiv"> <div ng-repeat="place in places"> <div class="checkbox-wrapper"> ...

Attempted to display a Google Map within a lightbox, but encountered difficulties in getting it to

I've copied and pasted my code below. It may contain references to Google or Lightbox being undefined, but I'm unsure how to integrate them into the code provided. My goal is to display a Google map within a Lightbox popup, but so far I have been ...

Generating CSS stylesheet from a string variable

Can a CSS file be created from a string data retrieved from the controller class in my application? I am working on a JSP page and need to access a CSS variable like this: <link rel="stylesheet" type="text/css" href="/Style/css=${css}" /> The "Sty ...

Issue with Tailwind CSS: Div element does not fill entire screen height

With the use of tailwind.css, I am developing a straightforward login form. Although my intention is for the form to occupy the entire height of the screen, it seems to fall short and leaves an undesirable white space at the bottom: https://i.sstatic.net/ ...

Automatically change the height of the UI element in HTML

I'm currently working on building a table view using the ul tag in HTML. Within the link provided, I've utilized two li tags to create a two-column layout. To visually represent the spaces occupied by the li tags, column dots, and row dots, I&apo ...

Create a log file containing the full console output with Batch script

I have developed a Discord bot named Mei, and recently I enhanced it by implementing a feature that logs all moderation and administrative commands to the Windows Command Prompt using a node module called "Logger". Now, I am seeking guidance on how to re ...

Add array as an element within another array

After initializing the data, I have an object structured like this and I am able to push data using the method below: myObj = { 1: ["a", "b", "c"], 2: ["c", "d", "e"], } data: { types: {} }, methods: { pushValue(key, value) { var ...

Angular Material: div not being filled by layout-fill

<!-- Container Div --> <div layout-fill> <!-- Image Div --> <div layout="column" layout-align="center center" class="coverImage" layout-fill> <md-content class="md-padding"> Sample Text Here ...

Positioning Arrows Adjacent to Definitions

I have a PHP snippet that adds arrow indicators next to previous and next sections on a page. This allows users to easily navigate between sections by clicking on the arrows. The desired output format is: (arrow) Previous Section Next Section ...

Issue with bootstrap 4: Specific column grid not displaying scroll-bar on overflow

I'm working with 3 columns inside my main tag (<main role="main" class="container">). The first column contains a table with multiple rows. I'm trying to make this column vertically scrollable while keeping it extended between the header an ...

What could be causing the defaultOpenKeys in my ant design to malfunction?

Using the Ant Design library, I have implemented a Menu/Submenu feature. Additionally, I created a custom function component called CustomSubMenu. This component simply displays the Ant SubMenu and is used within a wrapper component Menu. However, I am enc ...

JavaScript event listener for SVG path element click is not functioning correctly as anticipated

How to determine if an element or its children have been clicked? I am trying to identify when a parent element or any of its child SVG icons with the attribute name set to "setGameState" have been clicked. The issue I am facing is that sometimes the even ...

Load jquery dynamically in Internet Explorer

My goal is to load jquery as a fallback option if CSS transitions are not supported. I'm using modernizer for this purpose, and it's working well. I have noticed that jquery works perfectly when included in the header like this: <script type ...

Issue with Flatlist displaying items improperly

I am working on a project where I need to display items using a flatlist. My goal is to have the items turn green when tapped on. However, I have encountered an issue where if I tap on a few items and then perform a search for a device (e.g. 112), the prev ...

Is it possible to verify the presence of data in a database using the ajax method?

This piece of JavaScript code is responsible for validating mobile number data, among other information, and sending it to validate_user.php for storage. However, I only want to store the data of users whose mobile numbers exist in another table; otherwise ...