Managing the width of an HTML table column based on its text content dynamically

Here is an example of an html table :

<table width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
      <td width="480px"  class="societe" ><div style="float:left;font-size:12px;padding-top:2px" ><?php echo $_SESSION[PROJET_REF] ?> - [<?php echo $_SESSION[MANUEL_REF] ?>]</div>
      </td>
   </tr>
   ...
</table>

The css class "societe" looks like this:

.societe{

    text-align:left;
    padding-left:23px;
    font-size:11px;
    font-weight:bold;

    background: url(../include/images/societe.png) repeat-x;

    }

In the column definition :

<td width="480px"  class="societe" >
, the column's width is hard-coded to 480px. To make it dynamic and fit with the column's text width, you can avoid hard-coding. In this case, the text is
<?php echo $_SESSION[PROJET_REF] ?> - [<?php echo $_SESSION[MANUEL_REF] ?>
.

Answer №1

To achieve a dynamic width for your td based on text content, avoid setting a fixed width for the td.

In this coding example (link), note that the first td contains lengthy text, leading other td's in the same column to adjust their widths accordingly.

The decision ultimately lies on user preference. Should the user prefer varying widths for td's or equal dimensions with tooltips displaying complete text if it exceeds the cell's width? The choice is yours to make :)

Answer №3

Imagine you have a div in your code like this:

<div id="example" style="margin-top:10px;font-size:14px;"><?php echo $_SESSION[EXAMPLE] ?> - [<?php echo $_SESSION[SAMPLE] ?>]</div>

Next, let's say you want to determine the character length and adjust the width accordingly:

if($("#example").text().length <= 100)
{
   $('.container').attr("width", "500px");
}
if($("#example").text().length <= 200)
{
   $('.container').attr("width", "600px");
}

You can continue this process based on your desired measurements.

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

Optimizing the rendering of Font-awesome CDN JS for better performance on Pagespeed Insights

Instead of directly linking to the Font Awesome CSS, I have chosen to leverage the JavaScript provided by the trustworthy and efficient Font Awesome CDN. This allows for asynchronous loading of icons on my homepage, ensuring a seamless user experience. How ...

What is the best way to merge multiple attributes into a single attribute in HTML?

Is there a way to set a margin for a section of my site without it treating each attribute individually? I want to treat them as one attribute instead. Here's an example: <div style="background-color: grey; padding: 13px;"> <h1>This p ...

Avoid activating the hover effect on an element situated below another element without the use of JavaScript

After reviewing various posts, it seems like the only solution involves using javascript. Is there a way to hover over one element without affecting the style of another element below it, all without relying on javascript? I'm looking for a solution u ...

Is there a way to incorporate the ::after or ::before pseudo-elements into an image tag?

While working on my project, I attempted to incorporate the ::after tag (I had used ::before as well) for an image within an img tag. However, I am facing difficulties as it doesn't seem to be working. Any advice or guidance would be greatly appreciat ...

Displaying a dynamic splash screen during the resource-loading process on an Android application

I would like to implement an animated image (currently using a set of PNGs) as a splash screen while my resources are loading. I have successfully displayed the splash screen using this method. However, the issue I am facing is that the splash screen appe ...

the sorting process took a turn for the worse

I am new to XSL. I am attempting to organize a list of books based on the 'number of pages'. I have created a simple XSL file for this purpose, but the output is not as expected. Some elements are sorted while others remain unsorted. What could b ...

What is the best way to incorporate three divs into a single line while maintaining a responsive design

Struggling with adding a logo above a login form? As a PHP developer without design expertise, achieving a responsive layout like the image below may seem daunting. To create this layout, consider using the following code: <style> .container { ...

The div is not positioned on the left side

I'm struggling with aligning three divs next to each other in HTML and CSS. I've set them to float: left, but they are not obeying this style rule. .threethings { width: 20%; } .threethings div { text-align: center; position: relative; ...

Sending a form without the need to reload the page

While it's known that Ajax is the preferred method to submit a form without refreshing the page, going through each field and constructing the Post string can be time-consuming. Is there an alternative approach that utilizes the browser's built-i ...

Ignored are the white spaces within a pre tag

From what I understand, the 'pre' tag is supposed to collapse all white spaces and tabs into one space. However, I have noticed that this doesn't happen for me. I'm curious as to why this may be the case. Could it possibly be dependent ...

What is the best way to align a value within CardActions in Material UI?

I'm currently facing an issue with my website design. Here's a snapshot of how it looks: Additionally, here is the code snippet related to the problem: <CardActions> <Typography style={{ fontWeight: 'bold', c ...

Steps to execute Java code (.class) with PHP and showcase on the identical web page

I've been working on running a Java program using a PHP script. Initially, the PHP script presents a form for users to input two values: Price and Sales Tax Rate. Then, it processes these values and sends them to a precompiled Java program (saved as ...

Attempting to adjust the width of a text animation loaded with jQuery using Textillate, but encountering difficulties

I found a captivating animation on this website: http://codepen.io/jschr/pen/GaJCi Currently, I am integrating it into my project. #content { position: relative; margin-left: auto; margin-right: auto; width: 1000px; height: 700px; } ...

Deleting content from cells in table for all even td items

This problem has been causing me frustration; I've attempted various methods using jquery and CSS, but to no avail. Essentially, I have the following table structure: <table id="mainTable"> <thead> <tr> <th>Arrival ...

After a brief delay, I aim to eliminate a className or restart the class animation when a certain condition is satisfied

Objective: My goal is to create a feature where the price number highlights with a color for a brief moment when there is a price change using toggleClassName. Challenge: The issue arises when the iteration is UP UP or DOWN DOWN, as the element already ha ...

Having trouble with the PHP code for sending an email using a basic form

This is an example from my HTML page. <form method="POST" action="sm.php"> <p>Login</p><br /> Username: <input type="text" size="10" maxlength="50" name="un"> <br /> Password: <input type="text" size="10 ...

Why does one of the two similar javascript functions work while the other one fails to execute?

As a new Javascript learner, I am struggling to make some basic code work. I managed to successfully test a snippet that changes text color from blue to red to ensure that Javascript is functioning on the page. However, my second code attempt aims to togg ...

Material UI button component does not have the applied class

I'm encountering an issue with applying a CSS class (redirectButton) to a Material UI button. Despite my efforts, the class is not being successfully applied. Below is the HTML code in question: {(isDataAdapterAdmin && !dataSources?.length) & ...

Next.js encountering page not found error due to broken link URL

Currently, I am working on implementing a login system in next.js. In the login page, I have included a Link to the Register page using the Link href attribute. However, every time I click on that link, it displays a message saying "404 page not found." Al ...

Is there a way to include text beneath the navigation bar and other elements on the page?

I am stuck trying to add some text below my header and navigation bar. Here is the code I've been working with: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta htt ...