.searchbar是我搜索栏的背景,但我不知道如何将我的搜索栏垂直居中到他的背景。
这是我的CSS
.searchbar {
display: block;
width: 100%;
height: 100px;
overflow: hidden;
background-color: lightgray;
text-align: center;
position: relative;
}
.searchbar input[type="text"] {
color: black;
text-align: left;
text-decoration: none;
width: 50%;
height: 40px;
font-size: 20px;
align-self: center;
position: relative;
vertical-align: middle;
}
这是html
<div class="searchbar">
<input type="text" placeholder="Search..." />
</div>
.searchbar {
width: 100%;
height: 100px;
overflow: hidden;
background-color: lightgray;
text-align: center;
}
.searchbar input[type="text"] {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
display: block;
color: black;
text-align: left;
text-decoration: none;
width: 50%;
height: 40px;
font-size: 20px;
align-self: center;
position: relative;
vertical-align: middle;
}
<div class="searchbar">
<input type="text" placeholder="Search..." />
</div>
这实际上是有效的
.searchbar {
display: block;
width: 100%;
height: 80px;
overflow: hidden;
background-color: lightgray;
text-align: center;
position: relative;
display: flex;
align-items: center;
}
.searchbar input[type="text"] {
color: black;
text-align: left;
text-decoration: none;
width: 50%;
height: 40px;
font-size: 20px;
align-self: center;
position: relative;
vertical-align: middle;
border-radius: 50px;
border: none;
margin: auto;
}