我正在尝试创建一个简单的搜索框,它应该在滚动时位于可搜索组件的顶部。问题是搜索框不适合外部容器,只是通过它(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>
答案 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%。这些修改以下列方式更改代码:
您的子容器现在绝对与主容器有关。
您不会滚动主容器,只会滚动包含无序列表的列表。
.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;