滚动条位于绝对位置的顶部

时间:2017-12-18 18:09:19

标签: html css

我正在尝试创建一个简单的搜索框,它应该在滚动时位于可搜索组件的顶部。问题是搜索框不适合外部容器,只是通过它(position: absolute)并重叠滚动条的up arrow

如何使搜索框适合外部容器(整个宽度)并且不与滚动条重叠?

.container {
  border-color: rgb(221, 221, 221);
  height: 140px;
  text-align: left;
  overflow: auto;
  border: 1px solid #a0a7b2;
  border-radius: 4px;
}

.outer {
  position: relative;
  display: block;
    float: none;
    width: 120px;
}

input {
  border: none;
  box-shadow: none;
  background-color: #f1f1f1;
  border-radius: 4px;
  height: 18px;
  display: inline-block;
  width: auto;
}

.search {
    background-color: #f1f1f1;
    position: absolute;
    width: 99%;
}

.list {
  margin-top: 20px;
}
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title></title>
</head>
<body>
   <div class="outer">
     <div class="container">
       <div class="search">
         <input type="text"/>
       </div>
       <div class="list">
         <ul>
           <li>Hola</li>
           <li>Hola</li>
           <li>Hola</li>
           <li>Hola</li>
           <li>Hola</li>
           <li>Hola</li>
           <li>Hola</li>
           <li>Hola</li>
           <li>Hola</li>
         </ul>
       </div>
   </div.
  </div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要做出的更改:

  • 修改overflow to hidden in container class并将其设为position:relative
  • 设置position of search and list to absolute
  • 设置height of list to 100%overflow-y:scroll
  • width of input更改为100%。

这些修改以下列方式更改代码:

  1. 您的子容器现在绝对与主容器有关。

  2. 您不会滚动主容器,只会滚动包含无序列表的列表。

  3. &#13;
    &#13;
    .container {
      border-color: rgb(221, 221, 221);
      height: 140px;
      text-align: left;
      border: 1px solid #a0a7b2;
      border-radius: 4px;
      position:relative;
      overflow:hidden;
    }
    
    .outer {
      position: relative;
      display: block;
      float: none;
      width: 120px;
    }
    
    input {
      border: none;
      box-shadow: none;
      background-color: #f1f1f1;
      border-radius: 4px;
      height: 18px;
      width:100%;
    }
    
    .search {
      background-color: #f1f1f1;
      position: absolute;
      width: 99%;
    }
    
    .list {
      position:absolute;
      margin-top: 20px;
      width:100%;
      height:100%;
      overflow-y: scroll;
    }
    &#13;
    <html>
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title></title>
    </head>
    
    <body>
      <div class="outer">
        <div class="container">
          <div class="search">
            <input type="text" />
          </div>
          <div class="list">
            <ul>
              <li>Hola</li>
              <li>Hola</li>
              <li>Hola</li>
              <li>Hola</li>
              <li>Hola</li>
              <li>Hola</li>
              <li>Hola</li>
              <li>Hola</li>
              <li>Hola</li>
            </ul>
          </div>
          </div. </div>
    </body>
    
    </html>
    &#13;
    &#13;
    &#13;