CSS div 内的文本悬停下划线动画

CSS text hover underline animation inside of div

我正在尝试为我网页上的某个词制作下划线动画。它有效,但下划线显示在 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>

我不使用括号,而是在里面放一个 div。

<div style="background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); float: right;"><div id="hoverDiv">Photos</div></div>

然后在 CSS 中,您只需在悬停时以蓝色显示 div 的底部边框。

抱歉,我没有时间完成所有代码,但这是初稿...希望它能给你一个好主意!

在父级修改了锚点的行高。

恢复默认值

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>

你可以使用 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;
}
<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>

你也可以使用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;
}
<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>

如果将 <a> 转换为 inline-block 元素,则两个示例都可以使用伪 :after