[英]CSS to make an input width responsive when next to fixed width items without using flexbox
How can I use CSS to make an input flexible while next to 2 fixed width buttons? 在2个固定宽度按钮旁边的同时,如何使用CSS使输入变得灵活?
I have 2 buttons which are fixed width, next to an input on the same row which I need to be flexible and always consume the rest of the space available in that row. 我有2个固定宽度的按钮,在同一行的输入旁边,我需要灵活并且总是占用该行中剩余的空间。
Needs to be supported by most browsers back to IE10 era. 早于IE10时代的大多数浏览器都需要支持。
I have attached a screenshot below to illustrate what I mean. 我在下面附上了一个屏幕截图,以说明我的意思。
You can view the site here: http://helenshill.com.au/beta3/shop/ 您可以在此处查看该站点: http : //helenshill.com.au/beta3/shop/
HTML markup: HTML标记:
<form class="cart-num" action="index.html" method="post">
<input type="text" name="quantity" class="form-control input-number quantity" id="quantity-box" value="1" min="1" max="100">
<span class="input-group-btn">
<button id="minus-btn-small" type="button" class="quantity-left-minus quantity-increment btn btn-number" data-type="minus" data-field="">
<span class="glyphicon glyphicon-minus"></span>
</button>
</span>
<span class="input-group-btn">
<button id="plus-btn-small" type="button" class="quantity-right-plus quantity-increment btn btn-number" data-type="plus" data-field="">
<span class="glyphicon glyphicon-plus"></span>
</button>
</span>
</form>
The code below works by using display:table
instead of flexbox, this will adjust to the size of the contents, so by making the 2 buttons a fixed width, the rest of the space will be allocated to the input and will change wit hthe screen size. 下面的代码通过使用display:table
而不是flexbox进行工作,这将调整内容的大小,因此,通过将2个按钮设置为固定宽度,其余空间将分配给输入,并将在屏幕上更改尺寸。
Working Snippet: 工作片段:
section { width: 100%; display: table; padding: 1em 0 0; } div.col { display: table-cell; width: 100%; box-sizing: border-box; vertical-align: middle; border: 1px solid #AAAAAA; } input { width: 100%; padding: .5em 1em; height: 40px; box-sizing: border-box; border: none; } .col.button { height: 40px; padding: 0 15px; text-align: center; } .button:hover { background: #EEEEEE;
<section> <div class="col"> <input type="text" /> </div> <div class="col button">+</div> <div class="col button">-</div> </section>
Snippet with your HTML HTML的摘录
Note that you need to add a container element around your textbox, but thats the only change required for the HTML. 请注意,您需要在文本框周围添加一个容器元素,但这仅是HTML所需的更改。
Also, the + and - don't show here because I don't have whatever library you are using. 另外,+和-不在此处显示,因为我没有正在使用的任何库。
form { width: 100%; display: table; } span.col { display: table-cell; width: 100%; box-sizing: border-box; vertical-align: middle; border: 1px solid #AAAAAA; } input { width: 100%; padding: .5em 1em; height: 40px; box-sizing: border-box; border: none; } .input-group-btn { display: table-cell; width: 100%; box-sizing: border-box; vertical-align: middle; border: 1px solid #AAAAAA; } button { height: 40px; width: 40px; padding: 0; text-align: center; display: block; background: #FFF; border: none; box-sizing: border-box; vertical-align: middle; } button:hover { background: #EEEEEE; }
<form class="cart-num" action="index.html" method="post"> <span class="col"> <input type="text" name="quantity" class="form-control input-number quantity" id="quantity-box" value="1" min="1" max="100"> </span> <span class="input-group-btn"> <button id="minus-btn-small" type="button" class="quantity-left-minus quantity-increment btn btn-number" data-type="minus" data-field=""> <span class="glyphicon glyphicon-minus"></span></button> </span><span class="input-group-btn"> <button id="plus-btn-small" type="button" class="quantity-right-plus quantity-increment btn btn-number" data-type="plus" data-field=""> <span class="glyphicon glyphicon-plus"></span> </button> </span> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.