[英]Why does bootstraps d-flex class stop my div from stretching 100% width?
I've got a div wherein I want two elements to set side by side: a search input field and corresponding search submit button. 我有一个div,其中我希望两个元素并排设置:一个搜索输入字段和相应的搜索提交按钮。
So far I've tried to accomplish this with the following code: 到目前为止,我已经尝试使用以下代码完成此操作:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <link rel="stylesheet" href="css/custom.css"> <div class="container w-100 mt-4 border-bottom pb-3 h-25 align-content-center" style="box-sizing: border-box;"> <div class="row"> <div class="col-1 position-relative d-flex align-items-center"><span class="mh-100" style="line-height: 1rem">Class20</span> </div> <div class="col-7 mw-100 align-items-center position-relative d-flex"> <form action="" class="justify-content-center position-relative m-0"> <input type="search" name="" value="" placeholder="search" class="w-100 align-items-center pl-3" id="search"> </form> <button class="btn-outline-primary"type="submit">Search</button> </div> <!--column 7--> <div class="col-4"> <button class="btn-outline-primary"type="submit">Log In</button> <button class="btn-outline-primary"type="submit">Sell your stuff</button> </div> <!-- col-4 --> </div> <!--row--> </div>
The d-flex does what I want. d-flex可以满足我的要求。 It puts the search field and the button side by side.
它将搜索字段和按钮并排放置。 The problem I'm encountering is that it also leaves about 4 columns of white space between that div and the next.
我遇到的问题是,在该div和下一个div之间还留下了大约4列空白。 In other words, it doesn't stretch the full 7 columns I've specified.
换句话说,它不会扩展我指定的全部7列。 How can I resolve that issue, without pushing my button to the next line?
如何解决该问题,而无需将我的按钮按到下一行?
Well, your col-7
( which contains the input and button ) has the right width, the problem is that your input and button do not cover the whole width of their parent ( the col-7 ) . 好吧,您的
col-7
(包含输入和按钮)具有正确的宽度,问题是您的输入和按钮没有覆盖其父对象(col-7)的整个宽度。
So you can add some bootstrap classes to the input ( for eg col-8
) and to the button ( for eg col-4
). 因此,您可以向输入(例如
col-8
)和按钮(例如col-4
)添加一些引导类。 That way they will cover the whole width of their parent. 这样,它们将覆盖其父对象的整个宽度。
You can also style them directly with CSS ( give them custom widths in % ) or use other bootstrap classes ( col-9 col-3 etc. ) 您也可以直接使用CSS设置样式(以%为其提供自定义宽度)或使用其他引导程序类(col-9 col-3等)。
OR you could add 或者您可以添加
.d-flex form{
flex-grow: 1
}
this way , the input will occupy all the remaining space inside the col-7
parent 这样,输入将占据
col-7
父级内部的所有剩余空间
See example in snippet below ( using col-8 and col-4 ) or jsFIddle 请参阅下面的代码段示例(使用col-8和col-4)或jsFIddle
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <div class="container w-100 mt-4 border-bottom pb-3 h-25 align-content-center" style="box-sizing: border-box;"> <div class="row"> <div class="col-1 position-relative d-flex align-items-center"><span class="mh-100" style="line-height: 1rem">Class20</span> </div> <div class="col-7 mw-100 align-items-center position-relative d-flex"> <form action="" class="justify-content-center position-relative m-0 col-8"> <input type="search" name="" value="" placeholder="search" class="w-100 align-items-center pl-3 " id="search"> </form> <button class="btn-outline-primary col-4" type="submit">Search</button> </div> <!--column 7--> <div class="col-4"> <button class="btn-outline-primary" type="submit">Log In</button> <button class="btn-outline-primary" type="submit">Sell your stuff</button> </div> <!-- col-4 --> </div> <!--row--> </div>
Bootstrap splits the elements widths into 12 columns. Bootstrap将元素的宽度分为12列。 100% = 12 col-1.
100%= 12 col-1。 So if you want two side-by-side elements to cover the full width of the parent, their 'column numbers' must add up to 12. ( col-8 and col-4 , col-6 col-6, col-9 col-3 etc. )
因此,如果您希望两个并排的元素覆盖父对象的整个宽度,则它们的“列号”必须加起来为12。(col-8和col-4,col-6 col-6,col-9 col-3等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.