简体   繁体   English

如何将宽度设置为td?

[英]How can I set width to td?

How can I keep cell phone words in the same line? 如何将cell phone单词保持在同一行?

 .label { width: 300px; padding: 5px 15px; } .input { border: 1px solid red; width: 100%; } .input > input { width: calc(100% - 4px); } 
 <form class="frm-find-people"> <table> <tbody> <tr> <td class="label">Cell Phone</td> <td class="input"><input type="text" name="name"></td> </tr> </tbody> </table> </form> 

As you see, I've set width: 300px for that column. 如您所见,我为该列设置了width: 300px But seems it doesn't apply. 但似乎并不适用。 Why? 为什么?

Add white-space: nowrap; 添加white-space: nowrap; to .label : .label

 .label { padding: 5px 15px; white-space: nowrap; } .input { border: 1px solid red; width: 100%; } .input > input { width: calc(100% - 4px); } 
 <form class="frm-find-people"> <table> <tbody> <tr> <td class="label">Cell Phone</td> <td class="input"><input type="text" name="name"></td> </tr> </tbody> </table> </form> 

the 300px width is not being applied because you have a width:100% on the next column, which tells it to occupy as much space as it can. 300px宽度未应用,因为您在下一列具有width:100% ,它告诉它占用尽可能多的空间。

To fix that, you have to remove the width:100% on the next td . 要解决此问题,您必须在下一个td上删除width:100% Transfer that to the text field instead. 而是将其转移到文本字段。 Finally, set the entire table to have width:100% so it covers the entire parent. 最后,将整个表格设置为width:100%以便覆盖整个父表格。 Below is the corrected CSS. 以下是更正后的CSS。

table {
    width: 100%; /* This should be 100% */
}
    .label {
    width: 300px;
    padding: 5px 15px;
}

.input {
    border: 1px solid red;
    /* remove width 100% from here */
}

.input > input {
  width: 100%; /* this should be 100% */
}

You've set .input to width: 100% , which is shrinking .label down to the smallest size possible (the length of the words inside) 您设定.inputwidth: 100%这是收缩.label下降到可能的最小尺寸(内部的话的长度)

You could use css calc to make .input 100vw - 300px wide 您可以使用CSS .input来使.input 100vw-300px宽

.input {
    width: calc(100vw - 300px);
}

And

.input > input {
  width: calc(100vw - 300px);
}

 .label { padding: 5px 15px; border: 1px solid red; width: 300px; } .input { border: 1px solid red; width: calc(100vw - 300px); } .input > input { width: calc(100vw - 300px); } tr { border: 1px solid blue; } 
 <form class="frm-find-people"> <table> <tbody> <tr> <td class="label">Cell Phone</td> <td class="input"><input type="text" name="name"></td> </tr> </tbody> </table> </form> 

Use following css: 使用以下CSS:

table { width:100%; 桌子{width:100%; } .label { width: 30%; } .label {宽度:30%; padding: 5px 15px; 填充:5px 15px; } .input { border: 1px solid red; } .input {border:1px solid red; width:70%; 宽度:70%; } .input > input { width: calc(100% - 4px); } .input>输入{width:calc(100%-4px); } }

The problem with your code, is that you should have: 您的代码存在的问题是,您应该具有:

table.mytable {
    width: 100%;
}

Because the <td> is a child element of the <table class="mytable"> . 因为<td><table class="mytable">的子元素。 So the parent element has to have a set size to be able to modify child elements such as <td> . 因此,父元素必须具有设置的大小,才能修改诸如<td>类的子元素。 The added mytable class is so that it only affects that table incase you have others. 添加的mytable类是这样,如果您有其他表,它只会影响该表。

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

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