【问题标题】:Input text & button with span inline [closed]带跨度内联的输入文本和按钮[关闭]
【发布时间】:2015-06-04 10:13:13
【问题描述】:

我们必须在同一行显示一个输入字段和一个带有内部跨度的按钮。 按钮分别。它的内部跨度必须有width:auto,输入文本必须填满剩余的宽度。

小提琴:http://jsfiddle.net/1xfxpm55/

HTML:

<div class="wrapper">
    <input type="text">
    <button> <span>Auswählen</span>

    </button>
</div>

CSS:

.wrapper {
    width:100%;
    display: table;
}
input {
    display: table-cell;
    width: 100%;
    float: left;
}
button {
    display: table-cell;
    width: auto;
}

感谢任何提示

【问题讨论】:

标签: html css


【解决方案1】:

将按钮放在宽度较小的div中:

* {
    box-sizing:border-box;
}
*:before, 
*:after {
    box-sizing:border-box;
}
.c1 {
    display: table-cell;
    padding-right:10px;
}
input{
    width:100%;
}
.c2 {
    display: table-cell;
    padding-left:10px;
    width:1%
}
<div class="wrapper">
    <div class="c1">
        <input type="text">
    </div>
    <div class="c2">
        <button><span>Auswählen</span></button>
    </div>
</div>

【讨论】:

  • 已经接近我们的代码,谢谢!不过,c2 目前的高度太高了......将能够解决这个问题:)
【解决方案2】:

你可以使用javascript吗?如果我的问题是正确的,输入文本字段必须填满整行 - 输入按钮的宽度,对吗?这是一个建议:

function getRemainingWidth(button, elm) {
    elm.style.width = "calc( 100vw - " + realWidth(button) + "px)";
    alert(elm.style.width);
}

function realWidth(elm) {
    var width = elm.offsetWidth + 
        parseInt(window.getComputedStyle(elm, null).getPropertyValue('padding-left'), 10) +
        parseInt(window.getComputedStyle(elm, null).getPropertyValue('padding-right'), 10) + 
        parseInt(window.getComputedStyle(elm, null).getPropertyValue('margin-left'), 10) + 
        parseInt(window.getComputedStyle(elm, null).getPropertyValue('margin-right'), 10);

    return width + 10;
}

window.onload = getRemainingWidth(document.getElementById('btn'), document.getElementById('textfield'));

在这里 http://jsfiddle.net/1xfxpm55/4/ 做了一个工作 jsfiddle(它是您提供的演示的更新)。

【讨论】:

  • 不,我们最好不要用JS来做这个……
  • 看看 PoseLab 的解决方案,它运行良好。如果您删除填充值,您将获得与此相同的效果,但没有 javascript。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 2017-11-13
  • 2021-06-10
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 2018-11-05
相关资源
最近更新 更多