gpt4 book ai didi

html - CSS 下划线悬停 - 为什么填充下划线?

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:43 25 4
gpt4 key购买 nike

我正在为我的网站设计一个导航栏,我希望链接在悬停时带有下划线。为什么下划线 CSS 规则适用于 nav-section 类?

我怎样才能阻止内边距被加下划线?

请看下面附上的代码...

body {
font-family: "Lato","Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 24px;
color: #424242;
}

.title {
float: left;
padding: 10px;
font-size: 30px;

}

.nav-section {
color: #757575;
float: left;
padding-top: 16px;
padding-right: 15px;
position: relative;
text-decoration: none;
cursor: pointer;
}

.nav-section:hover {
color: #616161
}


.nav-link:before {
content: "";
position: absolute;
width: 100%;
height: 2px;
bottom: 0;
left: 0;
background-color: #616161;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}


.nav-link:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
<div class="title">Title</div>
<div class="nav-section">
<a class="nav-link">Link 1</a>
</div>
<div class="nav-section">
<a class="nav-link">Link 2</a>
</div>
<div class="nav-section">
<a class="nav-link">Link 3</a>
</div>
<div class="nav-section">
<a class="nav-link">Link 4</a>
</div>

最佳答案

padding 用于增加元素的大小。您应该使用 margin-rightmargin-top 来创建所需的分隔。

body {
font-family: "Lato","Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 24px;
color: #424242;
}

.title {
float: left;
padding: 10px;
font-size: 30px;

}

.nav-section {
color: #757575;
float: left;
margin-top: 16px;
margin-right: 15px;
position: relative;
text-decoration: none;
cursor: pointer;
}

.nav-section:hover {
color: #616161
}


.nav-link:before {
content: "";
position: absolute;
width: 100%;
height: 2px;
bottom: 0;
left: 0;
background-color: #616161;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}


.nav-link:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
<div class="title">Title</div>
<div class="nav-section">
<a class="nav-link">Link 1</a>
</div>
<div class="nav-section">
<a class="nav-link">Link 2</a>
</div>
<div class="nav-section">
<a class="nav-link">Link 3</a>
</div>
<div class="nav-section">
<a class="nav-link">Link 4</a>
</div>

关于html - CSS 下划线悬停 - 为什么填充下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com