简体   繁体   English

无法使用CSS colgroup保持表格列宽固定

[英]Unable to keep the table column width fix with CSS colgroup

 p { width: 40px; margin: 0; padding: 0; } td { padding: 0; margin: 0; } 
 <table style="width:100px;"> <colgroup> <col style="width:20px;"> <col style="width:40px;"> <col style="width:40px;"> </colgroup> <tr> <th>ID</th> <th>NAME</th> <th>Email</th> </tr> <tbody> <tr> <td>1</td> <td>john</td> <td> <p>johntheman@example.com dsdsdsdsdsd dsdsdsdddddddddddddddddddddddsd dsdsssssssssssssdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsds sadddddddddddddddddddddddddd </p> </td> </tr> </tbody> </table> 

I have simple table with fix width. 我有固定宽度的简单桌子。 I also have fix width for columns in it to do that I have used CSS colgroup. 我还为其中的列设置了固定宽度,以达到使用CSS colgroup的目的。

Problem 1: 问题一:

Width of my columns if I check in developer's tool is different than that of what I have given in colgroup. 如果我签入开发人员工具,则列宽与colgroup中给出的列宽不同。

What I have tried 我尝试过的

While fixing it I have found that the width changes with the text in it if I increase the text td increases and vice versa. 修复它时,我发现如果我增加文本td增加,宽度会随着其中的文本而变化,反之亦然。

Problem 2: 问题2:

When I enter the text in td unless I don't break it on new line by hitting enter the width of td goes on increasing with text. 当我在td中输入文本时,除非不按回车键将其换行,否则td的宽度会随着文本的增加而增加。

What I have tried 我尝试过的

To tackle this problem I have wrap my text in ap tag with fix width and then put it in td but still no luck. 为了解决这个问题,我将文本用固定宽度的ap标签包裹起来,然后放入td中,但还是没有运气。 What I see is width is getting applied to P tag but text is overflowing. 我看到的是宽度正在应用于P标签,但文本溢出。

What I expect : 我期望的是:

I would like to know that why text is not breaking itself on new line after the fixed width of td. 我想知道为什么在固定宽度的td之后文本不会在新行上中断。 Why text overflows out of P even after fix width? 为什么即使在固定宽度之后,文本也会从P溢出? Why td has to increase even after fix width? 为什么即使固定宽度后td也必须增加?

I don't know what I am missing to apply here. 我不知道要在这里申请什么。

for the table use css property 对于表使用css属性

table-layout: fixed;

Then provide fixed with to your td columns. 然后提供固定到您的td列。

The width of the <p> tag and <td> are fixed they are not increasing. <p>标签和<td>的宽度是固定的,不会增加。

Until there is no space in your text content it will not wrap to next line. 除非文本内容中没有空格,否则它将不会换行到下一行。 You have to use ellipsis which will put ... in place of overflowing text content. 您必须使用省略号,以将...代替溢出的文本内容。 Use below css for your <p> tag. 在CSS下面使用<p>标记。

p {
    width: 40px;
    margin: 0;
    padding: 0;
    text-overflow: ellipsis;
    overflow: hidden;       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM