设计导航栏,如何用css增加悬停字段的大小?
Designing a navbar, how to increase the size of the hovered field with css?
我的 header 看起来像这样:
如果我将鼠标悬停在它上面,它会稍微增加,变成这样:
需要在 CSS 中更改什么 属性 以便 on-hovering 它占据蓝色条的整个高度?我尝试使用 width
,因为我使用的是 border-box
,我认为蓝色条的大小不会改变,但它确实改变了。
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-image: url(background.jpg);
background-size: cover;
background-position: center;
font-family: sans-serif;
}
.menu-bar {
background: rgb(3, 50, 112);
text-align: center;
}
.menu-bar ul {
display: inline-flex;
list-style: none;
color: #fff;
}
.menu-bar ul li {
width: 120px;
margin: 15px;
padding: 0px;
}
.menu-bar ul li a {
text-decoration: none;
color: white;
}
.active,
.menu-bar ul li:hover {
background-color: #2bab0b;
border-radius: 3px;
}
<link rel="stylesheet1" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/fontawesome.min.css">
<div class="menu-bar">
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Investors</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Training </a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
编辑:
正如评论中所建议的那样。我将 height:30px;
包含在悬停 class 中,因为将其包含在 menu-bar
class 中似乎不起作用。然而,似乎正在发生的是绿色突出显示的大小在增加,而蓝色条的大小:
这就是您想要的效果吗?请参阅下面的代码片段。我从 li
中删除了边距并在 a
上添加了一个填充。
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-image: url(background.jpg);
background-size: cover;
background-position: center;
font-family: sans-serif;
}
.menu-bar {
background: rgb(3, 50, 112);
text-align: center;
}
.menu-bar ul {
display: inline-flex;
list-style: none;
color: #fff;
}
.menu-bar ul li {
width: 120px;
}
.menu-bar ul li a {
text-decoration: none;
color: white;
display:block;
padding:15px;
}
.active,
.menu-bar ul li:hover {
background-color: #2bab0b;
border-radius: 3px;
}
<div class="menu-bar">
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Investors</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Training </a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
删除 li
的边距。
*
{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body
{
background-image: url(background.jpg);
background-size: cover;
background-position : center;
font-family: sans-serif;
}
.menu-bar
{
background: rgb(3, 50, 112);
text-align: center;
}
ul {
display: inline-flex;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
width: 120px;
padding: 0px;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #2bab0b;
border-radius: 3px;
}
.active {
background-color: #2bab0b;
border-radius: 3px;
}
<html>
<head>
<title> Title of page </title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet1" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/fontawesome.min.css">
</head>
<body>
<div class="menu-bar">
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Investors</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Training </a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</body>
</html>
这就是您要找的。我 修改了 您的代码并在 hovering
上添加了一些 transition
。看下面的代码:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-image: url(background.jpg);
background-size: cover;
background-position: center;
font-family: sans-serif;
}
.menu-bar {
background: rgb(3, 50, 112);
text-align: center;
}
.menu-bar ul {
display: flex;
flex-direction: row;
list-style: none;
color: #fff;
}
.menu-bar ul li {
padding: 10px 12px;
display: flex;
flex-direction: column;
justify-content: center;
transition: all 0.4s ease;
}
.menu-bar:hover li {
padding: 14px 12px;
transition: all 0.4s ease;
}
.menu-bar ul li a {
text-decoration: none;
color: white;
}
.active,
.menu-bar ul li:hover {
background-color: #2bab0b;
border-radius: 3px;
}
<link rel="stylesheet1" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/fontawesome.min.css">
<div class="menu-bar">
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Investors</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Training </a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
我的 header 看起来像这样:
如果我将鼠标悬停在它上面,它会稍微增加,变成这样:
需要在 CSS 中更改什么 属性 以便 on-hovering 它占据蓝色条的整个高度?我尝试使用 width
,因为我使用的是 border-box
,我认为蓝色条的大小不会改变,但它确实改变了。
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-image: url(background.jpg);
background-size: cover;
background-position: center;
font-family: sans-serif;
}
.menu-bar {
background: rgb(3, 50, 112);
text-align: center;
}
.menu-bar ul {
display: inline-flex;
list-style: none;
color: #fff;
}
.menu-bar ul li {
width: 120px;
margin: 15px;
padding: 0px;
}
.menu-bar ul li a {
text-decoration: none;
color: white;
}
.active,
.menu-bar ul li:hover {
background-color: #2bab0b;
border-radius: 3px;
}
<link rel="stylesheet1" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/fontawesome.min.css">
<div class="menu-bar">
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Investors</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Training </a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
编辑:
正如评论中所建议的那样。我将 height:30px;
包含在悬停 class 中,因为将其包含在 menu-bar
class 中似乎不起作用。然而,似乎正在发生的是绿色突出显示的大小在增加,而蓝色条的大小:
这就是您想要的效果吗?请参阅下面的代码片段。我从 li
中删除了边距并在 a
上添加了一个填充。
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-image: url(background.jpg);
background-size: cover;
background-position: center;
font-family: sans-serif;
}
.menu-bar {
background: rgb(3, 50, 112);
text-align: center;
}
.menu-bar ul {
display: inline-flex;
list-style: none;
color: #fff;
}
.menu-bar ul li {
width: 120px;
}
.menu-bar ul li a {
text-decoration: none;
color: white;
display:block;
padding:15px;
}
.active,
.menu-bar ul li:hover {
background-color: #2bab0b;
border-radius: 3px;
}
<div class="menu-bar">
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Investors</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Training </a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
删除 li
的边距。
*
{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body
{
background-image: url(background.jpg);
background-size: cover;
background-position : center;
font-family: sans-serif;
}
.menu-bar
{
background: rgb(3, 50, 112);
text-align: center;
}
ul {
display: inline-flex;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
width: 120px;
padding: 0px;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #2bab0b;
border-radius: 3px;
}
.active {
background-color: #2bab0b;
border-radius: 3px;
}
<html>
<head>
<title> Title of page </title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet1" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/fontawesome.min.css">
</head>
<body>
<div class="menu-bar">
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Investors</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Training </a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</body>
</html>
这就是您要找的。我 修改了 您的代码并在 hovering
上添加了一些 transition
。看下面的代码:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-image: url(background.jpg);
background-size: cover;
background-position: center;
font-family: sans-serif;
}
.menu-bar {
background: rgb(3, 50, 112);
text-align: center;
}
.menu-bar ul {
display: flex;
flex-direction: row;
list-style: none;
color: #fff;
}
.menu-bar ul li {
padding: 10px 12px;
display: flex;
flex-direction: column;
justify-content: center;
transition: all 0.4s ease;
}
.menu-bar:hover li {
padding: 14px 12px;
transition: all 0.4s ease;
}
.menu-bar ul li a {
text-decoration: none;
color: white;
}
.active,
.menu-bar ul li:hover {
background-color: #2bab0b;
border-radius: 3px;
}
<link rel="stylesheet1" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/fontawesome.min.css">
<div class="menu-bar">
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Investors</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Training </a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>