固定了手机上的导航栏

时间:2018-06-25 08:44:25

标签: css bootstrap-4 media-queries

仅在用户使用移动设备访问网站时,我试图将导航栏固定在顶部。到目前为止,我已经尝试通过媒体查询来做到这一点。当我使用Google chrome功能“检查”以查看我的网站在移动设备上的外观时,它可以工作,但是当我上传网站并从实际的移动设备访问它时,导航栏不再固定在顶部。 link to the site

更新:固定的导航栏在safari中根本不起作用,并且在谷歌浏览器中的错误位置

<!--NAVIGATION BAR-->
<!DOCTYPE html>

   <nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

 <div class="collapse navbar-collapse" id="navbarSupportedContent">
   <ul class="navbar-nav mr-auto">
  <li class="nav-item active">
    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Dropdown
    </a>
    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <div class="dropdown-divider"></div>
      <a class="dropdown-item" href="#">Something else here</a>
    </div>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled" href="#">Disabled</a>
  </li>
</ul>
<form class="form-inline my-2 my-lg-0">
  <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
  <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
CSS


 @media only screen and (max-width: 768px) {
     .navbar {
         position: fixed;
         z-index: 1;
         width: 100%;
     }
 }

 @media only screen and (max-width: 768px) {
     .carousel {
         margin-top: 60px;
     }
 }

 .navbar {
     background-color: #2A2F35;
     padding: 0 !important;
 }

 .navbar-nav a {
     font-family: 'Open Sans', sans-serif;
     font-style: normal;
     font-size: 14px;
     font-weight: 400;
     line-height: 22px;
     letter-spacing: 0.5px;
     color: #ffffff !important;
     padding: 25px 0px 25px 0px;
     margin: 0px 15px 0px 15px;

}

2 个答案:

答案 0 :(得分:1)

以此为参考

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

 <div class="collapse navbar-collapse" id="navbarSupportedContent">
   <ul class="navbar-nav mr-auto">
  <li class="nav-item active">
    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Dropdown
    </a>
    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <div class="dropdown-divider"></div>
      <a class="dropdown-item" href="#">Something else here</a>
    </div>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled" href="#">Disabled</a>
  </li>
</ul>
<form class="form-inline my-2 my-lg-0">
  <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
  <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>

答案 1 :(得分:0)

我刚刚在手机上进行了测试,导航栏似乎没有问题。也许添加此元标记将对您有所帮助 <meta name="viewport" content="width=device-width, initial-scale=1">