居中输入框和图标按钮
Centering input box and icon button
我想让输入框和图标按钮都垂直居中。
我尝试了 flexbox justify-content 和 align-content,但它仍然无法正常工作。
css 有什么方法可以做到这一点吗?谢谢。
你必须用一个容器 div 包裹它们,容器 display:flex;
。使用 flex-direction
属性 总是好的,在你的情况下是 row
。
然后使用 align-item:center;
垂直对齐项目。如果您希望它们也水平居中,则在下面的示例中将 justify-content:center;
添加到 .container
的 css。
我在输入框旁边使用了一张图片和一个字体超棒的搜索图标图标,因为你没有在问题中提到你使用的是哪一个。
.container{
display:flex;
flex-direction:row;
align-items:center;
}
.container input{
line-height:80px;
margin:0 5px;
}
.container img{
height:50px;
margin:0 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="container">
<input type="text" placeholder="Search"/>
<i class="fa fa-search"></i>
<img src="http://www.chinabuddhismencyclopedia.com/en/images/thumb/b/b8/Nature.jpg/240px-Nature.jpg">
</div>
向输入框和图标按钮的父元素添加 属性 line-height 并将其值设置为等于输入框和图标按钮的父元素的高度。
示例:如果它们都在 div 内并且 div 的高度为 40px,则将 属性 行高添加到 div 并且将其设置为 40px。
我想让输入框和图标按钮都垂直居中。 我尝试了 flexbox justify-content 和 align-content,但它仍然无法正常工作。 css 有什么方法可以做到这一点吗?谢谢。
你必须用一个容器 div 包裹它们,容器 display:flex;
。使用 flex-direction
属性 总是好的,在你的情况下是 row
。
然后使用 align-item:center;
垂直对齐项目。如果您希望它们也水平居中,则在下面的示例中将 justify-content:center;
添加到 .container
的 css。
我在输入框旁边使用了一张图片和一个字体超棒的搜索图标图标,因为你没有在问题中提到你使用的是哪一个。
.container{
display:flex;
flex-direction:row;
align-items:center;
}
.container input{
line-height:80px;
margin:0 5px;
}
.container img{
height:50px;
margin:0 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="container">
<input type="text" placeholder="Search"/>
<i class="fa fa-search"></i>
<img src="http://www.chinabuddhismencyclopedia.com/en/images/thumb/b/b8/Nature.jpg/240px-Nature.jpg">
</div>
向输入框和图标按钮的父元素添加 属性 line-height 并将其值设置为等于输入框和图标按钮的父元素的高度。
示例:如果它们都在 div 内并且 div 的高度为 40px,则将 属性 行高添加到 div 并且将其设置为 40px。