Tips on adjusting the width of a table cell to fit its contents

How can I display a SQL query result in an HTML table without cutting off long content?

The issue I am facing is that the cell length in my table does not adjust to fit the length of the content, making it difficult to read.

In each cell, there is an <input> tag so I can directly modify data and trigger an update query with the onChange event.

This is how I generate my table:

        $texte = "<table class='table table-bordered table-sm' ><thead>$table_header</thead>";
        $texte .= "<tbody>";

        $nb_ligne ="Number of rows: ".pg_num_rows($result);

        while($row = pg_fetch_row($result)){
            $texte .= "<tr>";
            foreach ($row as $value){
                $texte .= "<td><input class='form-control' value='$value' onchange=''></td>";
            }
            $texte .= "</tr>";
        }
        $texte .= "</tbody></table>";

$table_header is defined above within a switch{} case'' statement where different strings are used depending on the case. For example:

switch ($query){
            case 'Aiguillage_Etat':
                $requete = "select projet.projet_nom, aiguilalge.aig_etat from projet inner join aiguillage on aiguillage.aig_id = projet.projet_id where aiguillage.aig_etat $filtre;";
                $table_header = "<th>Nom projet</th><th>Etat aiguillage</th>";
                break;
}

I utilize AJAX to change the content of a <div>, where my table is displayed:

<div id="tableau_resultat"></div>

Even after trying to adjust the width of the <th> elements, I have been unsuccessful in adapting to the length of the content.

If possible, please provide guidance on what I may be doing wrong in this approach or if there are any insights I may be missing.

Answer №1

The issue stems from the content within $table_header (which was not provided). To address this, it is necessary to add CSS white-space: nowrap to the <td> or <th> elements inside it.

My suggestion is to utilize a <style> tag to specify it once, as shown below:

<style type="text/css">
    .table-sm > thead > tr > th { white-space: nowrap; }
</style>

Alternatively, you will need to apply inline styles individually to each inner <td> or <th> element.

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

Utilizing Asp.Net for seamless file uploading with HTML5's drag and drop feature

Struggling to upload a file using HTML5's Drag and Drop (DnD) and File API. Unsure about how to send form data to the server, attempted using XMLHttpRequest but encountered issues. Here is what I have so far: <body> <form id="fo ...

The picture within the div is not appearing as expected

I am facing an issue with inserting an image in a div container using CSS and HTML. The image is not displaying properly. Here is the HTML code: <div id="wrapper"> <div id="quoteContainer"> <span class="start_img"></span> </di ...

Determining the size of <li> elements within Bootstrap 4's tab-content feature

I need some assistance with this issue. I am attempting to calculate the length of a list using Bootstrap 4's tab-content feature. However, when the tab is inactive upon page load, the length is showing as 0. Even after opening the tab, it remains at ...

The hovering of the mouse over a dropdown menu causes a division on the page to shift

Creating a website with a dropdown menu and slider division can be tricky. When hovering over the menu, the submenu displays but causes the slider division to shift down. Does anyone have suggestions on how to fix this issue? Below is the code: <html& ...

Drop down menus fail to appear after the screen has been resized

Creating responsive menus involves using ordered and unordered lists, along with CSS for styling. I have added a script to dynamically generate dropdown menus, but encountered an issue where nothing appears on the screen upon resizing - only the backgrou ...

Navigational Menu in PHP

My goal is to have one link that retrieves data from a table with a specific value and another identical link that pulls data from the same table but with a different value. However, both dropdowns are displaying in the link. <div class="col-sm-9"> ...

Unexpected behavior: Bootstrap 4 tooltip fails to appear on dynamically-generated elements

The tooltips appearing are not the expected Bootstrap 4 style tooltips. The tooltips currently displayed: https://i.sstatic.net/60Ubm.png compared to the tooltips that should be shown: https://i.sstatic.net/koayu.png I have enabled the tooltips in the ...

Select a style option from the drop-down menu to be disabled once it is checked

Looking at the image below, I am seeking for Exp. Year (the disabled option) to appear grey as a placeholder when the page loads, and then turn black when an option like 2016 is clicked on. Is there a way to achieve this without using JavaScript? JSFiddle ...

What is the best way to access the inner html of every cell within a table row that I have selected?

I want to implement a feature on my website where users can click a "save" button on a specific row in a table and save the entire row's innerHtml onto another page as their favorite hiking trails. I've been trying to achieve this by adding clic ...

Problem with the rel='imageName' attribute when there is an apostrophe in the name

My webpage has a table displaying card names, costs, sets, and rarities. All this information is retrieved from a database table. I have implemented a feature where hovering over any card name in the table triggers an image popup of the card, which closes ...

Issue with align-items-* class in Bootstrap 4 causing alignment problems

Currently in the process of constructing a website utilizing the Bootstrap 4 framework and encountering difficulties with aligning items properly. For instance: Refer to Bootstrap 4 official documentation for row positioning Compare to my row positionin ...

"Exploring the Relationship Between Parent and Child Elements in CSS

Is it possible for an id to act as a parent in CSS? For instance: <div id="link"> <a href="www.example.com"></a> </div> The CSS code: #link > a{ /* style would be applied here*/ } Can this actually be achieved? ...

The hamburger icon is not visible on smaller screens when using the Bootstrap 5 navbar

'The stylesheets for Bootstrap CSS and JS have been included in the file. The content shows up fine with default colors, but not when I customize them.' ''' <li class="nav-item"> <a class= ...

How can I incorporate margins or adjust scaling within dompdf?

After creating a PDF using dompdf with the paper size set to A6, I am experiencing issues when trying to print it on A4 and letter paper sizes. The left and top borders are being cut off. Is there a way to generate a PDF that is compatible with A4, A6, l ...

Making a form select element responsive within a bootstrap 4 card component

Check out the code snippet I have here: This is how I envisioned it to look, and it works perfectly at large resolutions. However, when I scale it down to tablet size, the resizing of the select element doesn't seem to work properly. Here's a s ...

Extract the price value from the span element, then multiply it by a certain number before adding it to a div element

On the checkout page of my website, I have the following HTML: <tr class="order-total"> <th>Total</th> <td><strong><span class="woocommerce-Price-amount amount"> <span class="w ...

Are there any other CSS properties that can achieve the same functionality as "contain: content;"?

Searching for an alternative to the contain: content; CSS property that is compatible with older browsers. Is there a solution out there? Perhaps a CSS hack or workaround? ...

Combining my CSS and JS files is causing code issues (works on my Mac, but not on the server)

Currently, I am in the process of merging my CSS and JS files before minifying them with the YUI compressor. My web application functions perfectly when each file is linked individually. Now, I am attempting to combine all the files into one single CSS fi ...

"The application is experiencing issues with loading styles correctly when using jQuery append (Fiddler has been

When attempting to load a div tag using the .append() method, the styles are not loading correctly. Here's the code that was tested: <section id="year-2020" class="tab-panel"> <div class="row accordion"> <header> ...

`Take action on a row by selecting the Edit option in CodeIgniter`

In my table, I have data displayed with an edit button in each row. What I want is for a pop-up or lightbox to appear when the edit button is clicked, without refreshing the page, and display all fields within that box. I am familiar with updating in the c ...