如何使文本输入框占据其右侧按钮留下的所有剩余空间?

发布于 2024-12-08 21:02:24 字数 862 浏览 5 评论 0原文

我很想在这里求助于表格,因为我似乎无法想出一些 CSS 来完成我在这里尝试创建的内容。

我有一个

容器,其中包含两个内容:

  • 一个文本输入框 ()
  • 一个按钮 (

我希望按钮右对齐(我没有给它固定的宽度,但我很容易就能做到)。文本输入框应位于按钮的正左侧并占据剩余空间。这是我正在寻找的示例:

在此处输入图像描述

不幸的是,以下 HTML 无法实现此目的(请原谅内联样式 - 仅用于演示目的):

<input type="button" value="Test" style="float: right;" />
<input type="text" style="display: block; width: 100%;" />

尝试添加

包装器,但这也不起作用。我做错了什么?我可以使用 布局轻松完成此任务,但我确信我一定错过了一些东西,并且有更好的方法来做到这一点。

I'm tempting to resort to tables here since I cannot seem to come up with some CSS that will accomplish what I'm trying to create here.

I have a <div> container that contains two things:

  • a text entry box (<input type='text' />)
  • a button (<input type='button' />)

I want the button right-aligned (I haven't given it a fixed width, but I easily could). The text entry box should be directly left of the button and take up the remaining space. Here is an example of what I'm looking for:

enter image description here

Unfortunately, the following HTML isn't accomplishing this (pardon the inline styles - for demonstration purposes only):

<input type="button" value="Test" style="float: right;" />
<input type="text" style="display: block; width: 100%;" />

I tried adding a <div> wrapper, but that didn't work either. What am I doing wrong? I could accomplish this easily with a <table> layout, but I'm sure I must be missing something and that there is a better way of doing it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所的.畏惧 2024-12-15 21:02:24

没有 也是可以的,但不是很明显。

参见: http://jsfiddle.net/thirtydot/wE7jc/2/

<input type="button" value="Test" style="float: right;" />
<div style="overflow: hidden; padding-right: 8px">
    <input type="text" style="width: 100%;" />
</div>

原因此处对此作品进行了解释。

It's possible without <table>, but it's not very obvious.

See: http://jsfiddle.net/thirtydot/wE7jc/2/

<input type="button" value="Test" style="float: right;" />
<div style="overflow: hidden; padding-right: 8px">
    <input type="text" style="width: 100%;" />
</div>

The reason this works is explained here.

离笑几人歌 2024-12-15 21:02:24

只要您不介意固定宽度的按钮,就应该这样做:

http://jsfiddle.net/4WghJ/< /a>

基本上只是将输入包裹在一个带有右边距的 div 中以补偿按钮。

As long as you don't mind a fixed width button, this should do:

http://jsfiddle.net/4WghJ/

Basically just wrapped the input in a div with a right margin to compensate for the button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文