如何使文本输入框占据父块内的所有剩余宽度?
如何实现以下目标:
┌────────────────────parent────────────────────┐
│ label [text-box ] [button] │
│ paragraph │
└──────────────────────────────────────────────┘
label
向左对齐button
向右对齐- 内的所有剩余宽度
text-box
占据父级>paragraph
左对齐,也必须与label
左对齐
label
和 button
都应遵循定义的字体属性其他地方尽可能多。 parent
在窗口内居中对齐,并且自然可以具有任意宽度。
请指教。
How do achieve the following:
┌────────────────────parent────────────────────┐
│ label [text-box ] [button] │
│ paragraph │
└──────────────────────────────────────────────┘
label
is aligned to the leftbutton
is aligned to the righttext-box
occupies all remaining width within parentparagraph
is aligned to the left, must be left-aligned withlabel
too
Both label
and button
should obey font properties defined elsewhere as maximum as possible. parent
is center-aligned within window, and, naturally, can have arbitrary width.
Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不要忘记,您可以使用
calc()
。假设label
和button
使用的总宽度为 100px(包括边距),那么宽度为:如果您认为它不支持很多浏览器,那么你确实错了。现在已经支持很多了。时间变了
Don't forget, you can use
calc()
. Let's assume total of width used bylabel
andbutton
is 100px (including margin), then the width is:If you think it doesn't support a lot of browser, well you are wrong indeed. It supports a lot now. Time has changed
如果分配
float: right
并将按钮(或多个按钮按相反顺序)放置在之前 输入框。然后用
float: left
放置标签,给输入框100%宽度并将其包裹在span内与显示:阻止
和溢出:隐藏
。不涉及魔法:
基本思想是所有右侧按钮都以相反的顺序指定在输入框之前。
It works without flex and tables if assign
float: right
and put the button (or several buttons in reverse order) before the input box.Then place the label with
float: left
, give the input box 100% width and wrap it inside a span withdisplay: block
andoverflow: hidden
.No magic involved:
The basic idea that all right side buttons are specified before the input box in the reverse order.
更新 [2016 年 10 月]:Flexbox 版本...
原始答案 [2011 年 4 月]:无表 CSS 版本(表行为)...
CSS...
演示: http://jsfiddle.net/wdm954/626B2/4/
Updated [Oct 2016]: Flexbox version...
Original answer [Apr 2011]: Table-less CSS version (of table behavior)...
CSS...
Demo: http://jsfiddle.net/wdm954/626B2/4/
我不喜欢第一个答案,即实际使用表格单元格的“无表格”版本。也不是使用实际表格的第二个答案。第三个答案也不是使用硬编码宽度。这是使用flex的解决方案。这是迄今为止最简单的:
I don't like first answer with the "table-less" version that actually uses table-cell. Nor the second answer that uses actual tables. Nor third answer that uses hardcoded widths. Here is solution using flex. It is by far simplest:
使用表格。 :我知道人们往往讨厌桌子,但他们会在这种情况下工作......
Use tables. :D I know people tend to hate tables, but they will work in this situation...
我知道如何实现此目的或类似目的的唯一方法是将“文本框”作为块元素,该元素将自动填充父级的整个宽度,然后向左侧和右侧应用等于总宽度的填充左边和右边的容器。然后将“label”和“button”元素的位置设置为相对,并将它们浮动到需要的位置(float:left,float:right)。
例如,
HTML:
CSS:
如果按钮和标签元素不需要设置宽度,则所有元素都可以将其宽度设为百分比值(全部加起来为 100%)。
The only way I know how to achieve this or similar, is to have the "text-box" as a block element that would automatically fill the entire width of the parent, then apply padding to the left and right equal to the total width of the containers on the left and right. Then make the "label" and "button" elements have their position set as relative and float them to where they need to be (float: left, float: right).
Something like,
HTML:
CSS:
If the button and label elements don't need to have a set width, all elements could just have their width as a percentage value (all adding up to 100%).