【问题标题】:How to expand input field to 100% of the parent如何将输入字段扩展到父级的 100%
【发布时间】:2012-07-27 19:34:51
【问题描述】:

对于一个可能很愚蠢的问题,很抱歉,我一直在寻找各种方法来解决这个问题,但它仍然不能像我想要的那样工作。

这是输入字段保持非常短(如 ~150 像素)的问题之一,即使它的框要宽得多(如宽显示器上的 ~1300 像素)。

我最初的 html 和 CSS 显示在: http://jsfiddle.net/jjoensuu/qSz5x/5/

为了解决这个问题,我在以下位置找到了解决方案: How to make text input box to occupy all the remaining width within parent block?

我在上面的线程中创建了我认为类似于“wdm”的答案,但我遗漏了一些东西,因为结果仍然是一个狭窄的输入字段。我的创作在这里: http://jsfiddle.net/jjoensuu/rJ45P/8/

我还在同一个线程中尝试了“Marty Wallace”的解决方案,但无法让它发挥作用。

因此,作为对我在 jsfiddle 网站上的代码的参考,“topsearch”框的宽度约为 1300 像素,但“field-topsearch”输入字段保持在 156 像素左右。

在我尝试的一些解决方案中,“开始”按钮会转到下面的下一行。任何有关如何解决此问题的帮助将不胜感激。

【问题讨论】:

  • 这小提琴好吗?? jsfiddle.net/surendraVsingh/rJ45P/11
  • 感谢 SVS,是的,它比我设法做到的要好得多。谢谢!唯一的问题是“开始”按钮仍然在下一行结束,但是通过将“开始”按钮上的“显示”设置为“内联”并将输入字段的宽度设置为 80%,问题就解决了。跨度>

标签: css


【解决方案1】:

这里是工作小提琴:http://jsfiddle.net/surendraVsingh/rJ45P/18/

CSS

.topsearch
    {
    height: 40px;
    line-height: 35px;
    padding-right: 10px;
    width:100%;
    display:table;
    }

.topsearch label
    {
    color: #c98116;
    text-transform: uppercase;
    font-weight: normal;
    font-size: 0.9em;
    margin: 0 5px;
    }

.topsearch label, .topsearch form
    {
    display: inline;
    } 

.topsearchfield
    {
    border: none;
    height: 24px;
    font-size: 15px;
    width: auto;
    } 
#field-topsearch{width:80%; display:inline-block;}
.button-start-search submit
    {
    display: table-cell;
    }

​

【讨论】:

    【解决方案2】:

    我采取了稍微不同的方法。在我看来,您希望输入文本字段扩展,但标签和输入按钮在输入字段的两侧保持相同大小。这对于生成包含多行的表单而不诉诸表格布局可能很有用。此外,之前的建议在将元素保持在一行方面也不是很有效。这种技术将允许包含“盒子”的大小随意调整为您喜欢的任何内容。

    我确实需要稍微调整一下您的 html,但我觉得标签应该在与之相关的元素旁边。为了清楚起见,我还省略了 html 表单,因为它不是布局的一部分:

    <div class="input">
        <label for="field-topsearch">Search</label>
        <div class="value">
            <div>
                <input id="field-topsearch" maxlength="256" type="text" name="search_str" value=""/>
            </div>
            <input type="submit" class="button-start-search submit " value="Go!"/>
        </div>
    </div>
    

    还有css:

    .value {
        position: relative;
    }
    .value>div {
        margin-left: 60px;
        margin-right: 40px;
    }
    label {
        position: absolute;
        top: 0px;
        left: 0px;
    }
    input.submit {
        position: absolute;
        top: 0px;
        right: 0px;
    }
    input[type="text"] {
        width: 100%;
    }
    

    小提琴可用here

    【讨论】:

      猜你喜欢
      • 2013-06-17
      • 2020-01-05
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多