开发者

Horizontal CSS - Input Field 100% and Margin for button

开发者 https://www.devze.com 2023-03-22 13:17 出处:网络
Task: Make text box 100% width but allow enough room for button. Problem: Button appears on next line and text box exceeds width of its container.

Task: Make text box 100% width but allow enough room for button.

Problem: Button appears on next line and text box exceeds width of its container.

<div class="field">
    <input type="text" name="my-field" />
    <input ty开发者_Python百科pe="button" id="my-button" value="Add +" />
</div>

 .field {
     margin-right: -70px;
     width: 100%;
 }
 .field input[type=text] {
     display: block;
     float: left;
     margin-right: 70px;
 }
 .field input[type=button] {
     display: block;
     float: right;
 }

My primary layout uses the following trick to achieve flexible width with fixed sidebar, but for some reason this is not working on the above.

<div class="outer-wrap">
    <div class="content">
        ...
    </div>
    <div class="sidebar">
        ...
    </div>
 </div>

 .outer-wrap {
     margin-right: -300px;
     width: 100%;
 }
 .content {
     float: left;
     margin-right: 300px;
 }
 .sidebar {
     float: right;
 }

What mistake am I making here?


You have to screw with the HTML a bit, but otherwise this works perfectly in IE7+ and all modern browsers.

See: http://jsfiddle.net/thirtydot/25bZC/

CSS:

.field > span {
    display: block;
    overflow: hidden;
    padding-right: 10px
}
.field input[type=text] {
    width: 100%
}
.field input[type=button] {
    float: right
}

HTML:

<div class="field">
    <input type="button" id="my-button" value="Add +" />
    <span><input type="text" name="my-field" /></span>
</div>


To pull this off you must ensure that the element which you are floating right comes before the one floating left. Like this

<div class="field">
    <input type="button" id="my-button" value="Add +" />
    <input type="text" name="my-field" />
</div>


try giving fixed width to

field input[type=text]

and

.field input[type=button]
0

精彩评论

暂无评论...
验证码 换一张
取 消