css - 导航菜单中的 Bootstrap 表单控件似乎无法正确调整大小

标签 css twitter-bootstrap menu responsive-design search-form

我用 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!

因此,如果您知道如何处理此问题,请告诉我。基本上我想要的只是避免在较小的设备中可能发生的这类问题(如您在此图像中所见)...

print screen

最佳答案

每个 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/

相关文章:

html - 在不使用媒体查询的情况下定位平板电脑

html - 仅在 id 内应用 "webkit-keyframes"

javascript - 如何在没有 src 属性的情况下在 iframe 中设置 div?

jquery 菜单有一个白色的边框/背景。怎么上色?

delphi - 如何为Delphi的WebBrowser制作自定义菜单?

html - 使用 html 和 css 创建固定标题

css - 什么是 SVG,它是如何工作的?

html - 将表单与 Bootstrap 4 中的中心对齐

html - Bootstrap 4 导航栏选项卡未激活选项卡内容

ios - 使用 addChildViewController 时 IBAction 不起作用