首页 > 解决方案 > 如何垂直居中搜索栏?

问题描述

.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>

标签: htmlcss

解决方案


这实际上是有效的

.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;
}

推荐阅读