我用 Bootstrap 做了一个导航菜单,在这个导航中我把搜索表单放在了菜单的中间。然后我将桌面设备的宽度调整为 500px,而对于平板设备,我将其宽度调整为 300px。
这是 CSS 代码:
@media (min-width: 768px) {
.navbar-nav.navbar-center {
position: absolute;
left: 50%;
transform: translatex(-50%);
}
.form-group input{
width:500px !important;
}
}
@media (min-width: 499px) {
.form-group input{
width:300px !important;
}
}
@media (min-width: 399px) {
.form-group input{
width:200px !important;
}
}
这是导航的 HTML 代码:
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-center">
<form class="navbar-form">
<button type="submit" class="btn btn-default">Submit</button>
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
</form>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="log"><button type="button" class="btn btn-default sign">Default</button></li>
</ul>
</div>
</div>
</nav>
问题是桌面设备中输入的搜索表单的大小不知何故仍为 200px!
因此,如果您知道如何处理此问题,请告诉我。基本上我想要的只是避免在较小的设备中可能发生的这类问题(如您在此图像中所见)...
最佳答案
每个 CSS 媒体查询都会覆盖前一个,因此对于任何大于 398px 的宽度,input
的宽度都将为 200px
。要解决此问题,您只需反转查询的顺序:
@media (min-width: 399px) {
.form-group input{
width:200px !important;
}
}
@media (min-width: 499px) {
.form-group input{
width:300px !important;
}
}
@media (min-width: 768px) {
.navbar-nav.navbar-center {
position: absolute;
left: 50%;
transform: translatex(-50%);
}
.form-group input{
width:500px !important;
}
}
但是,如果您只是想避免输入太宽,您最好使用类似 calc()
的内容.
关于css - 导航菜单中的 Bootstrap 表单控件似乎无法正确调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44486776/