简体   繁体   English

如何让我的搜索栏填充剩余的 100% 水平 div 空间?

[英]How do I get my search bar to fill remaining 100% horizontal div space?

I would like my "Search by Name" #left div to extend the entire red area:我希望我的“按名称搜索”#left div 扩展整个红色区域:

搜索栏跨越红色区域

 #main { border: 1px solid black; font-size: 20px; font-weight: bold; color: white; } #left { width: 100%; background-color: red; } #center { float: right; width: 230px; background-color: blue; } #right { float: right; width: 45px; background-color: green; }
 <div id="main"> <div id="right"> <button name="button" type="submit" class="btn btn-default"><i class="glyphicon glyphicon-search"></i></button> </div> <div id="center"> <div class="dropdown bootstrap-select show-tick select-picker navbar-margins"> <select name="tag_id[]" id="tag_id_" class="select-picker navbar-margins" data-actions-box="true" data-selected-text-format="count > 1" data-title="No Tags Selected" data-deselect-all-text="Select None" multiple="multiple" tabindex="-98"></select> </div> </div> <div id="left"> <input type="text" name="q" id="q" placeholder="Search by Name or Contact Info"> </div> </div>

When I add the following to span my #q div then the element gets bumped down:当我添加以下内容以跨越我的 #q div 时,元素会被撞倒:

css: css:

#left #q {
    width: 100%;
  }

在此处输入图片说明

Stop using floats and use flex instead:停止使用浮动并使用 flex 代替:

 #main { border: 1px solid black; font-size: 20px; font-weight: bold; color: white; display: flex; /* use this instead of floats */ } #left { flex-grow: 1; /* make this grow to fill remaining space */ } #left input { width:100%; /* optional - make the input fill the div */ } #center { width: 230px; background-color: blue; } #right { width: 45px; background-color: green; }
 <div id="main"> <div id="left"> <input type="text" name="q" id="q" placeholder="Search by Name or Contact Info"> </div> <div id="center"> <div class="dropdown bootstrap-select show-tick select-picker navbar-margins"> <select name="tag_id[]" id="tag_id_" class="select-picker navbar-margins" data-actions-box="true" data-selected-text-format="count > 1" data-title="No Tags Selected" data-deselect-all-text="Select None" multiple="multiple" tabindex="-98"></select> </div> </div> <div id="right"> <button name="button" type="submit" class="btn btn-default"><i class="glyphicon glyphicon-search"></i></button> </div> </div>

What has happened here is you have told the input inside left to be 100% of the red container, however the red container stretches all the way across the screen.这里发生的事情是你已经告诉左边的输入是红色容器的 100%,但是红色容器一直延伸到整个屏幕。

Then when you tell the input to also be 100% it hits into the other containers and drops down.然后,当您告诉输入也是 100% 时,它会进入其他容器并下降。

You could probably get this idea working by setting it to 85% instead of 100% but you will hit into issues every time you resize things.您可能可以通过将其设置为 85% 而不是 100% 来实现这个想法,但是每次调整大小时都会遇到问题。

A better way might be to look into a css property called flex.更好的方法可能是查看名为 flex 的 css 属性。 This allows you to do what you without having to set fixed values.这使您无需设置固定值即可执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM