我对链接元素上的伪元素:after
有点问题。
div {
width: 250px;
border: 1px solid red;
}
/* line 1, sass/partials/_sidebar.sass */
.pagenav {
list-style-type: none;
}
/* line 3, sass/partials/_sidebar.sass */
.pagenav ul {
list-style-type: none;
margin-left: 0;
margin-top: 30px;
}
/* line 7, sass/partials/_sidebar.sass */
.pagenav ul li {
padding-bottom: 10px;
}
/* line 9, sass/partials/_sidebar.sass */
.pagenav ul li a {
text-decoration: none;
padding-bottom: 8px;
position: relative;
-webkit-transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
transition: all .3s ease-in-out;
color: #404040;
font-size: 18px;
}
/* line 16, sass/partials/_sidebar.sass */
.pagenav ul li a:hover,
.pagenav ul li a:focus,
.pagenav ul li a.active {
color: darkgreen;
}
/* line 18, sass/partials/_sidebar.sass */
.pagenav ul li a:hover:after,
.pagenav ul li a:focus:after,
.pagenav ul li a.active:after {
height: 3px;
opacity: 1;
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
-ms-transform: translateY(0px);
-o-transform: translateY(0px);
transform: translateY(0px);
}
/* line 22, sass/partials/_sidebar.sass */
.pagenav ul li a:after {
display: inline-block;
-webkit-transition: all .3s;
-moz-transition: all .3s;
transition: all .3s;
position: absolute;
top: 100%;
left: 12.5%;
width: 75%;
height: 1px;
background: darkgreen;
content: "";
opacity: 0;
-webkit-transform: translateY(-8px);
-moz-transform: translateY(-8px);
-ms-transform: translateY(-8px);
-o-transform: translateY(-8px);
transform: translateY(-8px);
}
<div>
<li class="pagenav">
<ul>
<li>
<a href="#">No problem here</a>
</li>
<li>
<a href="#">A text longer than the first is a problem</a>
</li>
<li>
<a href="#">No problem</a>
</li>
</ul>
</li>
</div>
如您所见,如果链接长于div,则会有换行符,但伪元素集的大小是从文本的开头到结尾。
我不知道为什么,我已经测试过将display : block
放到我的链接上,但这不是我想要的,因为:after
的大小不会“响应”到链接的大小。
答案 0 :(得分:0)
您应该能够通过将链接设为inline-block
元素来解决您的问题:
div {
width: 250px;
border: 1px solid red;
}
/* line 1, sass/partials/_sidebar.sass */
.pagenav {
list-style-type: none;
}
/* line 3, sass/partials/_sidebar.sass */
.pagenav ul {
list-style-type: none;
margin-left: 0;
margin-top: 30px;
}
/* line 7, sass/partials/_sidebar.sass */
.pagenav ul li {
padding-bottom: 10px;
}
/* line 9, sass/partials/_sidebar.sass */
.pagenav ul li a {
display:inline-block;
text-decoration: none;
padding-bottom: 8px;
position: relative;
-webkit-transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
transition: all .3s ease-in-out;
color: #404040;
font-size: 18px;
}
/* line 16, sass/partials/_sidebar.sass */
.pagenav ul li a:hover,
.pagenav ul li a:focus,
.pagenav ul li a.active {
color: darkgreen;
}
/* line 18, sass/partials/_sidebar.sass */
.pagenav ul li a:hover:after,
.pagenav ul li a:focus:after,
.pagenav ul li a.active:after {
height: 3px;
opacity: 1;
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
-ms-transform: translateY(0px);
-o-transform: translateY(0px);
transform: translateY(0px);
}
/* line 22, sass/partials/_sidebar.sass */
.pagenav ul li a:after {
display: block;
-webkit-transition: all .3s;
-moz-transition: all .3s;
transition: all .3s;
position: absolute;
top: 100%;
left: 12.5%;
right: 12.5%;
height: 1px;
background: darkgreen;
content: "";
opacity: 0;
-webkit-transform: translateY(-8px);
-moz-transform: translateY(-8px);
-ms-transform: translateY(-8px);
-o-transform: translateY(-8px);
transform: translateY(-8px);
}
&#13;
<div>
<li class="pagenav">
<ul>
<li>
<a href="#">No problem here</a>
</li>
<li>
<a href="#">A text longer than the first is a problem</a>
</li>
<li>
<a href="#">No problem</a>
</li>
</ul>
</li>
</div>
&#13;