如何在导航栏中垂直对齐按钮?
How can I align button vertically in nav bar?
我正在练习创建网站,目前正在尝试复制 this site。目前我正在处理导航栏,但我无法在导航栏中垂直对齐按钮。
.header {
background-color: red;
padding: 40px 20px;
}
.header h1 {
background-color: yellow;
float: left;
}
.header h1 img {
display: block;
}
.header__nav {
background-color: aqua;
float: right;
}
.header__nav li {
float: left;
height: 38px;
}
.header__nav li a {
padding: 0 20px;
display: inline-block;
line-height: 38px;
}
.contents {
background-color: green;
}
.footer {
background-color: blue;
}
<header class="header clearfix">
<h1>
<a href="#"><img src="https://pixelicons.com/wp-content/themes/pexelicons/assets/pic/site-logo/logo.png" alt="Logo"></a>
</h1>
<nav class="header__nav">
<ul class="clearfix">
<li><a href="#">View icons</a></li>
<li><a href="#">Buy now</a></li>
<li><button class="menu">menu</button></li>
</ul>
</nav>
</header>
<section class="contents">
contents
</section>
<footer class="footer">
footer
</footer>
My working image
我想垂直对齐导航栏右上角的按钮。我知道这可以使用 display: flex;
解决,但我想用另一种方式解决。有没有合适的方法解决这个问题?
即使您使用 display: flex
的解决方案对我来说非常好,您也可以在所有项目(两个链接和按钮)上使用 vertical-align: middle;
和 display: inline-block
来修复它
.header {
background-color: red;
padding: 40px 20px;
}
.header h1 {
background-color: yellow;
float: left;
}
.header h1 img {
display: block;
}
.header__nav {
background-color: aqua;
float: right;
}
.header__nav li {
float: left;
height: 38px;
}
.header__nav li a {
padding: 0 20px;
display: inline-block;
<!-- Remove - line-height: 38px; -->
}
.contents {
background-color: green;
}
.footer {
background-color: blue;
}
<!-- new added class -->
.clearfix{
display: flex;
align-items: center;
}
<header class="header clearfix">
<h1>
<a href="#"><img src="https://pixelicons.com/wp-content/themes/pexelicons/assets/pic/site-logo/logo.png" alt="Logo"></a>
</h1>
<nav class="header__nav">
<ul class="clearfix">
<li><a href="#">View icons</a></li>
<li><a href="#">Buy now</a></li>
<li><button class="menu">menu</button></li>
</ul>
</nav>
</header>
<section class="contents">
contents
</section>
<footer class="footer">
footer
</footer>
我正在练习创建网站,目前正在尝试复制 this site。目前我正在处理导航栏,但我无法在导航栏中垂直对齐按钮。
.header {
background-color: red;
padding: 40px 20px;
}
.header h1 {
background-color: yellow;
float: left;
}
.header h1 img {
display: block;
}
.header__nav {
background-color: aqua;
float: right;
}
.header__nav li {
float: left;
height: 38px;
}
.header__nav li a {
padding: 0 20px;
display: inline-block;
line-height: 38px;
}
.contents {
background-color: green;
}
.footer {
background-color: blue;
}
<header class="header clearfix">
<h1>
<a href="#"><img src="https://pixelicons.com/wp-content/themes/pexelicons/assets/pic/site-logo/logo.png" alt="Logo"></a>
</h1>
<nav class="header__nav">
<ul class="clearfix">
<li><a href="#">View icons</a></li>
<li><a href="#">Buy now</a></li>
<li><button class="menu">menu</button></li>
</ul>
</nav>
</header>
<section class="contents">
contents
</section>
<footer class="footer">
footer
</footer>
My working image
我想垂直对齐导航栏右上角的按钮。我知道这可以使用 display: flex;
解决,但我想用另一种方式解决。有没有合适的方法解决这个问题?
即使您使用 display: flex
的解决方案对我来说非常好,您也可以在所有项目(两个链接和按钮)上使用 vertical-align: middle;
和 display: inline-block
来修复它
.header {
background-color: red;
padding: 40px 20px;
}
.header h1 {
background-color: yellow;
float: left;
}
.header h1 img {
display: block;
}
.header__nav {
background-color: aqua;
float: right;
}
.header__nav li {
float: left;
height: 38px;
}
.header__nav li a {
padding: 0 20px;
display: inline-block;
<!-- Remove - line-height: 38px; -->
}
.contents {
background-color: green;
}
.footer {
background-color: blue;
}
<!-- new added class -->
.clearfix{
display: flex;
align-items: center;
}
<header class="header clearfix">
<h1>
<a href="#"><img src="https://pixelicons.com/wp-content/themes/pexelicons/assets/pic/site-logo/logo.png" alt="Logo"></a>
</h1>
<nav class="header__nav">
<ul class="clearfix">
<li><a href="#">View icons</a></li>
<li><a href="#">Buy now</a></li>
<li><button class="menu">menu</button></li>
</ul>
</nav>
</header>
<section class="contents">
contents
</section>
<footer class="footer">
footer
</footer>