[英]Margin-top doesn't work when display is set to inline
I am working from a tutorial but wanted to try it out myself first. 我正在使用教程,但想先自己尝试一下。 The problem is that while using hover over the link, the background color change touches the topmost of the navbar but doesn't touch the bottom of it.
问题在于,在链接上使用鼠标悬停时,背景颜色更改会触及导航栏的最顶部,但不会触及导航条的底部。
margin-top
doesn't work when the display is set to inline and I've tried padding and similar solutions. 当显示设置为嵌入式并且我尝试了填充和类似的解决方案时,
margin-top
无效。 Here is my code: 这是我的代码:
*{
padding: 0;
margin:0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.logo .text-primary {
color: #85b84b;
}
a:hover{
background: #85b84b;
border-radius: 5px;
}
nav{
background: #444;
color: #f4f4f4;
display: flex;
justify-content: space-between;
text-align: center;
padding: 1rem;
}
nav li{
list-style: none;
display: inline;
padding: 1rem;
}
nav a{
text-decoration: none;
color: #f4f4f4;
padding: 1rem;
}
Its html code is: 它的html代码是:
<nav>
<h1 class="logo">
<span class="text-primary">
<i class="fas fa-book-open"></i> Edge</span>Ledger
</h1>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">What</a></li>
<li><a href="#">Who</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
margin-top
and margin-bottom
doesn't work for inline elements. margin-top
和margin-bottom
对内联元素不起作用。 Try changing the display to inline-block
to provide margins or paddings vertically(top, bottom). 尝试将显示更改为
inline-block
以垂直(上,下)提供边距或填充。
<style>
ul#menu
{
margin: 0 0 5px;
padding: 0;
text-align: right;
float: right;
}
ul#menu li
{
float: left;
list-style: none;
padding-left: 15px;
}
</style>
<nav>
<h1 class="logo">
<span >
<i ></i> Edge</span>Ledger
</h1>
<ul id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">What</a></li>
<li><a href="#">Who</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
Try this one it works for me. 试试这个对我有用的。 Use
float
property 使用
float
属性
you just have to make align-items:center
it will make the things center. 您只需要使
align-items:center
就能使事物居中。
*{ padding: 0; margin:0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } .logo .text-primary { color: #85b84b; } a:hover{ background: #85b84b; border-radius: 5px; } nav{ background: #444; color: #f4f4f4; display: flex; justify-content: space-between; align-items: center; padding: 1rem; } nav li{ list-style: none; display: inline; padding: 1rem; } nav a{ text-decoration: none; color: #f4f4f4; padding: 1rem; }
<nav> <h1 class="logo"> <span class="text-primary"> <i class="fas fa-book-open"></i> Edge</span>Ledger </h1> <ul> <li><a href="#">Home</a></li> <li><a href="#">What</a></li> <li><a href="#">Who</a></li> <li><a href="#">Contact</a></li> </ul> </nav>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.