Sebbomatico
Sebbomatico

Reputation: 33

Pseudo-elements and padding

I want to get some practice with pseudo elements in combination with transitions and animations. But I have one little problem.

Have a look at this:

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

the two red lines are my pseudo elements. But I want them to have width 100% but it's not working in combination with the padding.

It should look like this but then I have no padding :/
http://codepen.io/anon/pen/RPOaRj

I have tested it on Firefox 39 and Chrome 43, both on OSX yosemite. Result is the same on both. Any Idea how this problem could be solved?

Cheers

Upvotes: 3

Views: 6136

Answers (1)

Jacob G
Jacob G

Reputation: 14152

You can fix that by doing this.
Add position:relative; to the <a>:

#navBar a {
    text-decoration: none;
    display: inline-block;
    position:relative;
}

Then, change your :before and :after to position:absolute; and change their position from top and bottom:

.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

Upvotes: 1

Related Questions