固定输入组Bootstrap 4

时间:2018-06-09 17:51:01

标签: html css twitter-bootstrap bootstrap-4

我的bootstrap有问题,因为我需要保留一个搜索栏,如下所示:

enter image description here

为此,我使用以下代码:

HTML:

<nav class="navbar fixed-top navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
</nav>  
<div class="list-page">
  <div class="row-search-bar">
    <div class="pos-rel">
        <div class="input-group mb-3">
            <input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username" aria-describedby="basic-addon2">
            <div class="input-group-append">
              <button class="btn btn-outline-secondary" type="button">Button</button>
            </div>
        </div>
    </div>
  </div>
  <div class="container">
      <div class="row">
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
        <p>This is an element that is part of the list of results</p>
      </div>
  </div>
</div>

CSS:

@media (max-width: 575.98px) {  
  .list-page {
    padding-top: 24%;
    margin-left: 10%;
    margin-right: 10%;
  } 
}

/* Small devices (landscape phones, 576px and up)*/
@media (min-width: 576px) and (max-width: 767.98px) { 
  .list-page {
    padding-top: 14%;
    margin-left: 10%;
    margin-right: 10%;
  }  
}

/*Medium devices (tablets, 768px and up)*/
@media (min-width: 768px) and (max-width: 991.98px) { 
  .list-page {
    padding-top: 7%;
    margin-left: 10%;
    margin-right: 10%;
  }
 }

/*Large devices (desktops, 992px and up)*/
@media (min-width: 992px) and (max-width: 1199.98px) { 
  .list-page {
    padding-top: 6%;
    margin-left: 10%;
    margin-right: 10%;
  }
 }

/*Extra large devices (large desktops, 1200px and up)*/
@media (min-width: 1200px) { 
  .list-page {
    padding-top: 6%;
    margin-left: 10%;
    margin-right: 10%;
  }
 }

 .pos-rel{
  position: relative;
  width:100%;
 }

 .row-search-bar{
  position: relative;
  z-index: 1030;
  background-color:red;
 }

我之前代码的重要部分是它使用position: relative; css类中的属性row-search-bar,但是当您移动页面内容时,此搜索栏不会保留在原位,它与内容一起移动。对于上述情况,为了实现使搜索栏保持原位的目标,我在position: fixed; css类中使用row-search-bar但是会​​发生这种情况:

enter image description here

那么当我向上/向下滚动搜索栏时,我应该怎样做才能使搜索栏保持原位:搜索栏如下所示:

enter image description here

非常感谢!

2 个答案:

答案 0 :(得分:2)

使用导航栏和搜索表单SELECT [DateOneRow].[Id], min([DateOneRow].[CheckInDate]) [CheckInDate], max([DateOneRow].[CheckInDate]) [CheckOutDate], datediff(d, min([DateOneRow].[CheckInDate]), max([DateOneRow].[CheckInDate])) [DaysBooked] FROM [DateOneRow] GROUP BY [DateOneRow].[Id]; 。在此容器内,使用带有两个container-fluid fixed-top w-100的容器:一个用于导航,一个用于搜索表单。由于您使用rows,因此您需要按下正文的其他内容。将身体的填充顶部设置为fixed-top以执行此操作。此固定容器的高度为160pxdb<>fiddle

&#13;
&#13;
160px
&#13;
body {
  padding-top: 160px;
}
&#13;
&#13;
&#13;

Read more about why you need to so

如果您想要全角度导航,请将导航栏移到<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/> <div class="container-fluid fixed-top w-100"> <div class="container "> <div class="row"> <div class="col"> <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="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <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="#">Features</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul> </div> </nav> </div> </div> <div class="row"> <div class="col"> <div class="input-group mb-3 mt-5"> <input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username" aria-describedby="basic-addon2"> <div class="input-group-append"> <button class="btn btn-outline-secondary" type="button">Button</button> </div> </div> </div> </div> </div> </div>之外,然后删除container的填充。

&#13;
&#13;
container-fluid
&#13;
body {
  padding-top: 160px;
}
&#13;
&#13;
&#13;

https://codepen.io/anon/pen/ZRLXWM

答案 1 :(得分:0)

我认为position: relative;或代码没有问题。

我运行了相同的代码,它似乎对我来说很好。

Have a look

您遇到的问题是因为您没有包含Bootstrap 4文件。

您可能正在使用类似https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css

的Bootstrap 3 cdn

我建议你把它改成

https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css