代码之家  ›  专栏  ›  技术社区  ›  Joel Coehoorn

设置输入元素的样式以填充其容器的剩余宽度

  •  161
  • Joel Coehoorn  · 技术社区  · 17 年前

    <div style="width:300px;">
        <label for="MyInput">label text</label>
        <input type="text" id="MyInput" />
    </div>
    

    这不是我的确切代码,但重要的是在固定宽度的容器中的同一行上有一个标签和一个文本输入。如何设置输入的样式以填充容器的剩余宽度,而不进行包装,也不知道标签的大小?

    7 回复  |  直到 17 年前
        1
  •  163
  •   Bill-G Luis Masuelli    6 年前

    这是一个简单而干净的解决方案,无需使用JavaScript或表布局黑客。这与这个答案类似: Input text auto width filling 100% with other elements floating

    display:block . 下一件事是按钮必须首先出现,输入字段必须其次出现。

    然后您可以将按钮向右浮动,输入字段将填充剩余的空间。

    form {
        width: 500px;
        overflow: hidden;
        background-color: yellow;
    }
    input {
        width: 100%;
    }
    span {
        display: block;
        overflow: hidden;
        padding-right:10px;
    }
    button {
        float: right;
    }
    <form method="post">
         <button>Search</button>
         <span><input type="text" title="Search" /></span>
    </form>

    http://jsfiddle.net/v7YTT/90/

    更新1: 如果你的网站只针对现代浏览器,我建议使用 flexible boxes . 在这里你可以看到 the current support .

    这甚至适用于多个按钮或与输入字段共享完整信息的其他元素。这是 an example .

        2
  •  150
  •   cobbal    13 年前

    虽然每个人都不喜欢表格的布局,但它们确实可以帮助处理类似的事情,可以使用显式的表格标记,也可以使用display:table-cell

    <div style="width:300px; display:table">
        <label for="MyInput" style="display:table-cell; width:1px">label&nbsp;text</label>
        <input type="text" id="MyInput" style="display:table-cell; width:100%" />
    </div>
    
        3
  •  62
  •   leishman    11 年前

    但一定要添加正确的供应商前缀!

    form {
      width: 400px;
      border: 1px solid black;
      display: flex;
    }
    
    input {
      flex: 2;
    }
    
    input, label {
      margin: 5px;
    }
    <form method="post">
      <label for="myInput">Sample label</label>
      <input type="text" id="myInput" placeholder="Sample Input"/>
    </form>
        4
  •  49
  •   basarat    10 年前

    请使用flexbox进行此操作。您有一个容器,它将把它的子容器伸缩成一行。第一个孩子根据需要占用空间。第二个弯曲以占用所有剩余空间:

    <div style="display:flex;flex-direction:row">
        <label for="MyInput">label&nbsp;text</label>
        <input type="text" id="MyInput" style="flex:1" />
    </div>
        5
  •  17
  •   llange    13 年前

    CSS:

    label{ float: left; }
    
    span
    {
        display: block;
        overflow: hidden;
        padding-right: 5px;
        padding-left: 10px;
    }
    
    span > input{ width: 100%; }
    

    HTML:

    <fieldset>
        <label>label</label><span><input type="text" /></span>
        <label>longer label</label><span><input type="text" /></span>
    </fieldset>
    

    http://jsfiddle.net/JwfRX/

        6
  •  4
  •   Whome    11 年前

    非常简单的技巧是使用 CSS calc

    <div style='white-space:nowrap'>
      <span style='display:inline-block;width:80px;font-weight:bold'>
        <label for='field1'>Field1</label>
      </span>
      <input id='field1' name='field1' type='text' value='Some text' size='30' style='width:calc(100% - 80px)' />
    </div>
    
        7
  •  3
  •   Andik    10 年前

    您可以尝试以下方法:

    div#panel {
        border:solid;
        width:500px;
        height:300px;
    }
    div#content {
    	height:90%;
    	background-color:#1ea8d1; /*light blue*/
    }
    div#panel input {
    	width:100%;
    	height:10%;
    	/*make input doesnt overflow inside div*/
    	-webkit-box-sizing: border-box;
           -moz-box-sizing: border-box;
                box-sizing: border-box;
    	/*make input doesnt overflow inside div*/
    }
    <div id="panel">
      <div id="content"></div>
      <input type="text" placeholder="write here..."/>
    </div>
        8
  •  1
  •   DharmaTurtle    6 年前

    <form class="d-flex">
      <label for="myInput" class="align-items-center">Sample label</label>
      <input type="text" id="myInput" placeholder="Sample Input" class="flex-grow-1"/>
    </form>
    

    更好的方法是使用Bootstrap内置的功能:

      <form>
        <div class="input-group">
          <div class="input-group-prepend">
            <label for="myInput" class="input-group-text">Default</label>
          </div>
          <input type="text" class="form-control" id="myInput">
        </div>
      </form>
    

    https://jsfiddle.net/nap1ykbr/