我正在尝试为我的网页上的单词添加下划线。它有效,但下划线显示文本在里面的div。你可以在代码片段上看到div应该填满整个显示,但现在你必须向下滚动才能看到下划线。如何在文本下面显示它?
body {
margin: 0;
padding: 0;
}
div {
width: 50%;
height: 100vh;
text-align: center;
line-height: 100vh;
font-size: 150%;
top: 0;
-webkit-transition: font-size 0.5s;
-moz-transition: font-size 0.5s;
-o-transition: font-size 0.5s;
transition: font-size 0.5s;
}
div:hover {
font-size: 200%;
}
a {
display: inline-block;
position: relative;
padding-bottom: 3px;
}
a:after {
content: '';
display: block;
margin: auto;
height: 3px;
width: 0px;
background: transparent;
transition: width .5s ease, background-color .5s ease;
}
a:hover:after {
width: 100%;
background: blue;
}
<div style="background-color: rgb(64, 64, 64); color: rgb(255, 255, 255); float: left;">
<a>Webpage</a>
</div>
<div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); float: right;">
<a>Photos</a>
</div>
答案 0 :(得分:3)
锚点的行高在父项中修改。
将其恢复为默认
body {
margin: 0;
padding: 0;
}
div {
width: 50%;
height: 100vh;
text-align: center;
line-height: 100vh; /* this value is inherited by the a */
font-size: 150%;
top: 0;
-webkit-transition: font-size 0.5s;
-moz-transition: font-size 0.5s;
-o-transition: font-size 0.5s;
transition: font-size 0.5s;
}
div:hover {
font-size: 200%;
}
a {
display: inline-block;
position: relative;
padding-bottom: 3px;
line-height: initial; /* added */
}
a:after {
content: '';
display: block;
margin: auto;
height: 3px;
width: 0px;
background: transparent;
transition: width .5s ease, background-color .5s ease;
}
a:hover:after {
width: 100%;
background: blue;
}
<div style="background-color: rgb(64, 64, 64); color: rgb(255, 255, 255); float: left;">
<a>Webpage</a>
</div>
<div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); float: right;">
<a>Photos</a>
</div>
答案 1 :(得分:1)
我不会使用括号,而是将div放入其中。
<div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); float: right;"><div id="hoverDiv">Photos</div></div>
然后在CSS中,您只需在悬停时以蓝色显示div的下边框。
抱歉,我没有时间做所有的代码,但它是初稿......希望它可以给你一个好主意!
答案 2 :(得分:1)
你可以使用flex和background-size:
body {
margin: 0;
height: 100vh;
display: flex;
}
div {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}
a {
padding-bottom: 3px;
background: linear-gradient(to left, currentcolor, currentcolor) bottom center no-repeat;
transition: 0.5s;
background-size: 0 3px;
}
a:hover {
background-size: 100% 3px;
}
&#13;
<div style="background-color: rgb(64, 64, 64); color: rgb(255, 255, 255); "><a>Webpage</a>
</div>
<div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); "><a>Photos</a>
</div>
&#13;
您也可以使用display:table
html {
margin: 0;
height: 100%;
width: 100%;
}
html {
display: table;
table-layout: fixed;
}
body {
display: table-row;
}
div {
display: table-cell;
text-align: center;
vertical-align: middle;
}
a {
padding-bottom: 3px;
background: linear-gradient(to left, currentcolor, currentcolor) bottom center no-repeat;
transition: 0.5s;
background-size: 0 3px;
}
a:hover {
background-size: 100% 3px;
}
&#13;
<div style="background-color: rgb(64, 64, 64); color: rgb(255, 255, 255); "><a>Webpage</a>
</div>
<div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); "><a>Photos</a>
</div>
&#13;
如果您将:after
转换为<a>
元素
inline-block