伪元素和填充
Pseudo-elements and padding
我想结合过渡和动画对伪元素进行一些练习。但是我有一个小问题。
看看这个:
HTML:
<nav id="navBar" class="clearfix">
<a href="#" class="nav">Test</a>
</nav>
CSS:
.clearfix:after {
content: ".";
clear: both;
display: block;
visibility: hidden;
height: 0px;
}
#navBar {
width: 100%;
padding: 30px;
border-top: 1px solid #808080;
border-bottom: 1px solid #808080;
box-sizing: border-box;
}
#navBar a {
text-decoration: none;
display: inline-block;
}
a.nav {
padding: 10px;
background-color: #7492b6;
color: #fff;
float: left;
font-weight: bold;
font-size: 1em;
min-width: 150px;
text-align: center;
}
a.nav::before {
content: "";
display: block;
position: relative;
top: 0;
left: 0;
width: 100%;
height: 3px;
background-color: #fff;
padding: 0;
}
a.nav::after {
content: "";
display: block;
position: relative;
top: 0;
left: 0;
width: 100%;
height: 3px;
background-color: #fff;
padding: 0;
}
http://codepen.io/anon/pen/rVbeLa
两条红线是我的伪元素。但我希望它们的宽度为 100%,但它不能与填充结合使用。
它应该看起来像这样但是我没有填充:/
http://codepen.io/anon/pen/RPOaRj
我已经在 Firefox 39 和 Chrome 43 上测试过,两者都在 OSX yosemite 上。两者的结果相同。
知道如何解决这个问题吗?
干杯
您可以通过这样做来解决这个问题。
将 position:relative;
添加到 <a>
:
#navBar a {
text-decoration: none;
display: inline-block;
position:relative;
}
然后,将您的 :before
和 :after
更改为 position:absolute;
并从顶部和底部更改它们的位置:
.navGroup1 a.nav::before {
content: "";
display: block;
position: absolute;
top: 10px;
left: 0;
width:100%;
height: 3px;
background-color: red;
}
.navGroup1 a.nav::after {
content: "";
display: block;
position: absolute;
bottom:10px;
left: 0;
width: 100%;
height: 3px;
background-color: red;
}
Updated Codepen
我想结合过渡和动画对伪元素进行一些练习。但是我有一个小问题。
看看这个:
HTML:
<nav id="navBar" class="clearfix">
<a href="#" class="nav">Test</a>
</nav>
CSS:
.clearfix:after {
content: ".";
clear: both;
display: block;
visibility: hidden;
height: 0px;
}
#navBar {
width: 100%;
padding: 30px;
border-top: 1px solid #808080;
border-bottom: 1px solid #808080;
box-sizing: border-box;
}
#navBar a {
text-decoration: none;
display: inline-block;
}
a.nav {
padding: 10px;
background-color: #7492b6;
color: #fff;
float: left;
font-weight: bold;
font-size: 1em;
min-width: 150px;
text-align: center;
}
a.nav::before {
content: "";
display: block;
position: relative;
top: 0;
left: 0;
width: 100%;
height: 3px;
background-color: #fff;
padding: 0;
}
a.nav::after {
content: "";
display: block;
position: relative;
top: 0;
left: 0;
width: 100%;
height: 3px;
background-color: #fff;
padding: 0;
}
http://codepen.io/anon/pen/rVbeLa
两条红线是我的伪元素。但我希望它们的宽度为 100%,但它不能与填充结合使用。
它应该看起来像这样但是我没有填充:/
http://codepen.io/anon/pen/RPOaRj
我已经在 Firefox 39 和 Chrome 43 上测试过,两者都在 OSX yosemite 上。两者的结果相同。 知道如何解决这个问题吗?
干杯
您可以通过这样做来解决这个问题。
将 position:relative;
添加到 <a>
:
#navBar a {
text-decoration: none;
display: inline-block;
position:relative;
}
然后,将您的 :before
和 :after
更改为 position:absolute;
并从顶部和底部更改它们的位置:
.navGroup1 a.nav::before {
content: "";
display: block;
position: absolute;
top: 10px;
left: 0;
width:100%;
height: 3px;
background-color: red;
}
.navGroup1 a.nav::after {
content: "";
display: block;
position: absolute;
bottom:10px;
left: 0;
width: 100%;
height: 3px;
background-color: red;
}