简体   繁体   English

输入以填充标签的剩余宽度

[英]Make input to fill the remaining width of label

I am trying to achieve the following in HTML: 我试图在HTML中实现以下内容:

  • input inside label element (rather not use ids and label for) 在标签元素内输入(而不是使用id和标签)
  • input should occupy 100% of whatever width is remaining 输入应占据剩余宽度的100%

It shouldn't be that hard, but I've tried lots of approaches from other examples; 它不应该那么难,但我尝试了很多其他例子的方法; using floats, overflow: hidden, limiting the height of surrounding elements, etc. Not having 100% width works too, but that's not an option. 使用浮动,溢出:隐藏,限制周围元素的高度等。不具有100%宽度也可以,但这不是一个选项。

The reason I rather not use label-for (which does work) is that there will be several identical input fields on the same page. 我之所以不使用label-for(确实有效)的原因是在同一页面上会有几个相同的输入字段。 They are rather complex so I create one manually and then clone it with jQuery N times, which then would break horribly if using IDs. 它们相当复杂,所以我手动创建一个然后用jQuery克隆它N次,如果使用ID则会破坏它。

Basic starting point: 基本出发点:

 input { width: 100%; } 
 <label> name: <input type="text" /> </label> 

Prepared a fiddle here: https://jsfiddle.net/8uuhhcon/ 准备一个小提琴: https//jsfiddle.net/8uuhhcon/

Try Flexbox 试试Flexbox

 label { display: flex; } input { flex: 1; } 
 <label>name: <input type="text" /> </label> 

If you can wrap name: into a span or so, like <span>name:</span> , you can try the CSS table. 如果你可以将name:换成一个span,或者像<span>name:</span> ,你可以试试CSS表。

 label { width: 100%; display: table; } span, input { display: table-cell; } span { width: 1%; /*give a small value*/ white-space: nowrap; /*prevent wrapping ie "user name"*/ vertical-align: middle; /*or top/bottom as needed*/ } input { width: 99%; /*give a large value*/ } 
 <label> <span>name:</span> <input type="text" /> </label> 

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

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