Generating a Table with automatic width using the <td> tag

Allow me to clarify my thoughts. I am aiming to accomplish the following:

I want to construct a table with 4 columns and multiple td (rows). However, I require each row to adjust its column width automatically based on its content.

Typically, the size of columns is determined by the largest td (row) within them:

https://i.sstatic.net/qUxO3.png

My goal, however, is as follows:

While I still want to arrange elements in a Table (columns) for organizational purposes such as alphabetical sorting, I need the columns to appear like this:

https://i.sstatic.net/8XhU3.png

Any suggestions?

Answer №1

To create columns using <td>, you can apply the CSS property display:inline. However, a more efficient approach would be to utilize <div> elements with flexbox.

td {
  border:2px solid red;
  display: inline;
}
td:nth-child(1) {
  background:yellow;
}
td:nth-child(2) {
  background:blue;
}
td:nth-child(3) {
  background:grey;
}
td:nth-child(4) {
  background:orange;
}
table {
  border:1px dashed black;
}
<table>
  <tr>
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>D</td>
  </tr>
  <tr>
    <td>Ant</td>
    <td>Bear</td>
    <td>Cat</td>
    <td>Dog</td>
  </tr>
</table>

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

What are the best methods for efficiently extracting web page content that is nested within HTML pages under the `<body>` tag?

Is there a way to easily extract embedded content like images, PDFs, videos, and documents from HTML web pages without including CSS, CSS background images, or JavaScript? I am in the process of migrating content from an old site to a new site and need to ...

Step-by-step guide on creating a blank horizontal line in an HTML table

Can anyone help me figure out how to draw a horizontal line connecting from td 1 to td 4 without relying on content CSS? I'm envisioning a progress bar like the one shown in the following image: https://i.sstatic.net/on8Bi.png <table> < ...

The primary navigation item is narrower than the submenu item

Here is the issue I am facing: The sub-menu words on my website are too long and it is affecting the overall design. I need help to make the navigation look better. This is the CSS code I have been using: /*navigation styles */ nav { background: url( ...

How to Convert Pixel Units to Rem and Em in CSS?

Until recently, I developed my website exclusively using pixels, which has made it less responsive than I'd like. Is there a method to efficiently convert pixel measurements to rem/em units, allowing for quicker and easier modifications? ...

How to adjust the background picture opacity in HTML

Is it possible to adjust the transparency of a background image in HTML? I came across one solution that involved adding a box on top of the background picture and changing the opacity of the box, which in turn changed the opacity of the background pictur ...

Why my attempts to alter numerous CSS elements using JQuery are failing?

Here's the code snippet I'm working with: var showThis = $(this).attr('id'); // div0, div1, div2 etc. $('#' + showThis).attr('style', 'background-color: #063 !important, height: 520px'); I am trying to mo ...

Having difficulty accessing the live server on VS Code for download

I'm having trouble installing the live server extension in my VS Code. When I search for "live server" in the extensions marketplace, I can't seem to find it among the results. Can someone please suggest an alternative method for installation? ...

Utilizing Regular Expressions to remove specific CSS attributes from an HTML content

I'm facing a challenge with my telerik RadEditor where users can input HTML that gets saved to my database. While this usually works well, I encounter issues when the CSS attributes position: absolute; or z-index: 100; (any # for z-index) are present ...

To activate two separate click events, one for an absolute element and another for the element positioned behind it, simply click on the desired absolute element to trigger both actions

Is it possible to trigger click events for both of these elements? position: relative container ELEMENT 1: standard div ELEMENT 2: position: absolute div In real life, element 2 is a transparent backdrop designed to capture clicks on top of the header ...

"JavaScript that doesn't rely on a specific browser

Why does this code behave differently across browsers? What modifications are needed to ensure consistent behavior in all of them? In Firefox, it functions correctly, using the URL /some/path/?searchedText=sometext. However, in IE, nothing happens when cli ...

Creating dynamic cell colors in a table using PHP values

Currently, I am working on a personal project outside of school and attempting to create a table where the color of the cells changes based on the values retrieved from the database. The color scheme is as follows: 1-3 = red 4-6 = yellow 7-10 = green ...

What is the best method for finding and observing the Javascript code being utilized on a webpage?

Is there a way to find and access specific Javascript used on a webpage? Imagine browsing through a webpage's source code and stumbling upon an element like this: <img border="0" alt="" onmouseout="hidetrail();" onmouseover="showtrail('imag ...

What is the best way to title an uploaded chunk with HTML5?

Here is the script I am working with: function upload_by_chunks() { var chunk_size = 1048576; // 1MB function slice(start, end) { if (file.slice) { return file.slice(start, end); } else if (file.webkitSlice) { ...

Utilizing AngularJS: Running a controller function within a directive

I'm brand new to AngularJS and I'm trying something new: I want to bind a scroll event to an element using a custom directive. Here's the code snippet: First, let's look at my controller: var officeUIApplication = angular.module(&apo ...

tips for incorporating staticfiles processing into css files on django

Within my CSS stylesheets, I often have code snippets like the following: #defaultCountdown span.countdown_section { color:#fff; padding:7px 15px!important; margin-bottom:2px; font-weight:300; background:url(../img/bg-white.png); t ...

Create an Angular material table with expandable rows that become sticky when scrolled past, then automatically unstick once they are no longer in view

Currently, I am working with Angular Material Table v11.1.0 which includes a main row with expandable rows. I want the main row to become sticky once an expandable row is opened and remain sticky while scrolling through the table. My goal is for the main ...

Do we need to duplicate structured data when listing nested objects, or is it better to avoid doing so?

We are currently focused on implementing JSON structured data for a one-page website that contains extensive information about a local business, including address, pricing, reviews, and services. The JSON snippet provided below represents the structured da ...

Implementing jQuery stylesheet using a bookmarklet

Here's a question that may seem silly, but I'm looking to quickly apply CSS styles to a webpage by clicking on a bookmarklet. It will definitely save me time :) For example, this code does the trick: javascript:void( jQuery( ".fancybox-overlay- ...

Incorporating a feature that displays one show at a time and includes a sliding animation into an

Hey there! I have this show/hide script on my website that's been working well. However, there are a couple of things I need help with. I want to modify the script so that only one div can be shown at a time. When a div appears, I'd love for it ...

Issues accessing my Facebook account due to domain problems

I have developed an HTML5 game that I am currently running in my web browser. My objective is to integrate Facebook login functionality, so I followed a tutorial and replaced the necessary information with my App ID. However, upon running it, an error mes ...