Setting the width of table cell td is invalid and the internal content always stretches it

Setting the width of table cell td is invalid and the internal content always stretches it
When making a table page, sometimes the width set for td is invalid. The width of td is always expanded by the internal content. You can set padding, but setting width directly is invalid. Let's take a closer look at this example:

Copy code
The code is as follows:

<div>
<table border="1px">
<tr>
<td width="100px" style="width: 100px !important;">1000800</td>
<td>1000000</td>
<td>1000000</td>
</tr>
<tr>
<td>1000000</td>
<td>10000300</td>
<td>1000000</td>
</tr>
</table>
</div>
<table border="1px">
<tr>
<td width="100px">1000000</td>
<td>1000000</td>
<td>1000000</td>
</tr>
<tr>
<td>1000000</td>
<td>10000300</td>
<td>1000000</td>
</tr>
</table>
* {margin: 0; padding: 0;}
.div1 {position: relative; width: 150px; height: 100px; overflow: scroll; border: 1px solid red;}


We can see that although the width of the first cell in class div1 is set, it is invalid. The cell content is always determined by the content. Since it is determined by the content, we should find a way to let the "content" expand the cell. That's it.

We can add a div in td and set the width of the div. Let's try it:

Modify part of the code in class div1:

Copy code
The code is as follows:

<td width="100px" style="width: 100px !important;">1000800</td>

Modified to

Copy code
The code is as follows:

<td><div>1000800</div></td>

Then write in the style:

Copy code
The code is as follows:

td div {
width:100px;
}

Refresh the page, and the effect is as follows:

We can see that the width of the cells in class div1 has taken effect.

<<:  How to make a tar file of wsl through Docker

>>:  Example code for building an admin dashboard layout using CSS and JavaScript

Recommend

Detailed explanation of fetch network request encapsulation example

export default ({ url, method = 'GET', da...

Layim in javascript to find friends and groups

Currently, layui officials have not provided the ...

Graphic tutorial on installing CentOS7 on VMware 15.5

1. Create a new virtual machine in VMware 15.5 1....

Better looking CSS custom styles (title h1 h2 h3)

Rendering Commonly used styles in Blog Garden /*T...

Docker Detailed Illustrations

1. Introduction to Docker 1.1 Virtualization 1.1....

Example code of Vue3 encapsulated magnifying glass component

Table of contents Component Infrastructure Purpos...

Installation tutorial of mysql 8.0.11 compressed version under win10

This article shares the installation tutorial of ...

Two solutions for Vue package upload server refresh 404 problem

1: nginx server solution, modify the .conf config...

Analysis of Linux boot system methods

This article describes how to boot the Linux syst...

XHTML three document type declarations

XHTML defines three document type declarations. T...

CSS Tutorial: CSS Attribute Media Type

One of the most important features of a style she...