Pseudo-elements and padding - html

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">
Test
</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

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

Related

How to display pseudo element with 100% height in Firefox

Only Firefox seems to ignore the bottom: 0; declaration. Even when I try to set the height of the h1:before element to 100%, it won't change anything in Firefox.
Is there any explanation for this behaviour and is there any workaround out there?
Chrome screenshot
Firefox screenshot
.title {
position: absolute;
}
.title h1 {
position: relative;
display: inline;
color: #fff;
line-height: 0.8;
white-space: pre-wrap;
border-top: 0.3em solid #0089C0;
border-bottom: 0.1em solid #0089C0;
background-color: #0089C0;
}
.title h1:before {
content: "";
background-color: red;
display: inline-block;
width: 0.3em;
height: 100%;
padding-top: 0.1em;
position: absolute;
left: -0.3em;
top: 0;
}
.title h1:after {
background-color: #0089C0;
}
.title h1 span {
position: relative;
z-index: 1;
}
<div class="title">
<h1><span>Almost before <br>we knew it, we had <br>left the ground. </span></h1>
</div>
Set the .title h1 to display: inline-block; like this:
.title h1 {
position: relative;
display: inline-block;
}
Or...set the position to fixed like this:
.title h1 {
position: fixed;
display: inline;
}

css3 after is on the text in a element

My problem is that the :after element is on the text in an element. Here is an example:
On CodePen
I want the :after to be below the text.
How can I fix this?
body {
text-align: center;
}
a {
display: inline-block;
text-decoration: none;
padding: 10px;
background: #222;
color: #f3f3f3;
font-size: 2em;
margin: 20px auto;
position: relative;
}
a:after {
content: "";
width: 100%;
height: 50%;
background: #00A3A3;
position: absolute;
left: 0;
top: 0;
}
Home page
Just change top:0 to top:50px; in after css. By keeping top:0
The top property sets the top edge of an element to a unit above/below the top edge of its nearest positioned ancestor.
you are making the element to stay on the top by keeping it 0
body {
text-align: center;
}
a {
display: inline-block;
text-decoration: none;
padding: 10px;
background: #222;
color: #f3f3f3;
font-size: 2em;
margin: 20px auto;
position: relative;
}
a:after {
content: "";
width: 100%;
height: 50%;
background: #00A3A3;
position: absolute;
left: 0;
top: 50px;
}
Home page
Use top: 100% on the :after pseudo element to place it exactly below the main element.
body {
text-align: center;
}
a {
display: inline-block;
text-decoration: none;
padding: 10px;
background: #222;
color: #f3f3f3;
font-size: 2em;
margin: 20px auto;
position: relative;
}
a:after {
content: "";
width: 100%;
height: 50%;
background: #00A3A3;
position: absolute;
left: 0;
top: 100%;
}
Home page

::after pseudo-element not appearing on Chrome Mobile

Pretty simple but odd bug, I have a pseudo-element which is appearing fine on desktop browsers but disappears on mobile Chrome (not sure about iOS, no iPhone to test with)
Here's the basic CSS:
a {
text-decoration: none;
display: inline-block;
color: #000;
overflow: visible;
}
a::after {
content: '';
display: block;
height: 8px;
width: 98%;
background: #8BC8F690;
margin-top: -9px;
margin-left: 2px;
}
<a>hello</a>
jsfiddle:
https://jsfiddle.net/w4d1jteb/
Check your background. This is not a valid value. It's 8 chars long, but only can have 6.
a {
text-decoration: none;
display: inline-block;
color: #000;
overflow: visible;
}
a::after {
content: '';
display: block;
height: 8px;
width: 98%;
background: #8BC8F690; <-- This is not valid. *1*2
margin-top: -9px;
margin-left: 2px;
}
*1: You could try background-color: rgba(139, 200, 246, 0.565) instead.
*2: Or just use #8BC8F6.
See these examples below:
a {
text-decoration: none;
display: inline-block;
color: #000;
overflow: visible;
}
a::after {
content: '';
display: block;
height: 8px;
width: 98%;
background-color: rgba(139, 200, 246, 0.565);
margin-top: -9px;
margin-left: 2px;
}
#test {
text-decoration: none;
display: inline-block;
color: #000;
overflow: visible;
}
#test::after {
content: '';
display: block;
height: 8px;
width: 98%;
background: #8BC8F6;
margin-top: -9px;
margin-left: 2px;
}
<a>hello</a>
<a id="test">hello2</a>

Child a tag not taking the table-cell parent height

I arranged the main menu navigation using table property. Having anchor tag inside the li tag. And provided drop-down arrow for anchor tag using "after" method. Now I am facing an issue that my anchor tag not taking its parent li full height which has display: table-cell. And I can't set position absolute to my anchor as I already set position: relative to display arrow in its after.
The reason I need my anchor tag to get full height is. My menu may have two lines like 1. XXxxxxxxxx xxxxx 2.xxxxxx so the first link will come two line as no enough width. and second one will come in single line.
ul.tb-megamenu-nav.nav{
display: table;
width: 100%;
table-layout: fixed;
position: relative;
}
.tb-megamenu .nav > li.level-1 {
vertical-align: middle;
min-height: 48px;
float: none;
display: table-cell;
min-width: 16.56686626746507%;
position: static;
list-style: none;
}
.tb-megamenu .nav > li > a {
background-color: transparent;
padding: 8px 7%;
text-align: center;
color: #fff;
border: 0;
position: relative;
cursor: pointer;
}
.tb-megamenu .tb-megamenu-nav > li.dropdown > a:after {
content: "";
width: 0;
height: 0;
position: absolute;
top: 40px;
left: 50%;
margin-left: -14px;
border-top: 14px solid #0092d0;
border-left: 14px solid transparent;
border-right: 14px solid transparent;
z-index: 999999;
}
* {
outline: 1px solid blue;
}
ul.tb-megamenu-nav.nav {
display: table;
width: 100%;
table-layout: fixed;
position: relative;
}
.tb-megamenu .nav > li.level-1 {
vertical-align: middle;
min-height: 48px;
float: none;
display: table-cell;
min-width: 16.56686626746507%;
position: relative;
list-style: none;
}
.tb-megamenu .nav > li > a {
background-color: transparent;
padding: 8px 7%;
text-align: center;
color: #fff;
border: 0;
position: relative;
cursor: pointer;
display: block;
}
.tb-megamenu .tb-megamenu-nav > li.dropdown > a:after {
content: "➤";
width: 0;
height: 0;
position: absolute;
top: 40px;
left: 50%;
margin-left: -14px;
border-top: 14px solid #0092d0;
border-left: 14px solid transparent;
border-right: 14px solid transparent;
z-index: 999999;
}
<ul class="tb-megamenu-nav nav">
<li class="level-1"></li>
<li class="dropdown">
<a href='#'></a>
</li>

Centering a hamburger nav styled with pseudo before/after selectors

I have a hamburger nav where its top and bottom bars are generated via pseudo before/after selectors. The issue occurs when trying to center it inside a parent element. Although the pseudo selectors center the original div element does not.
HTML
<a class="nav-container" href="##">
<div class="hamburger"></div>
</a>
CSS
html {
text-align: center;
}
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.nav-container {
display: inline-block;
padding: 20px 0;
}
.hamburger {
background: black;
display: inline-block;
position: relative;
height: 5px;
width: 30px;
}
.hamburger:before {
background: #000;
content: '';
position: absolute;
top: -10px;
width: 30px;
height: 5px;
}
.hamburger:after {
background: #000;
content: '';
position: absolute;
top: 10px;
width: 30px;
height: 5px;
}
http://jsfiddle.net/ht1d7y9f/
Actually, I think it's the pseudo elements that are out of position.
.hamburger:before {
background: #000;
content: '';
position: absolute;
top: -10px;
left: 0;
width: 30px;
height: 5px;
}
.hamburger:after {
background: #000;
content: '';
position: absolute;
left: 0;
top: 10px;
width: 30px;
height: 5px;
}
html {
text-align: center;
}
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.nav-container {
display: inline-block;
padding: 20px 0;
text-align: center;
border: 1px solid green;
}
.hamburger {
background: black;
position: relative;
height: 5px;
width: 30px;
}
.hamburger:before {
background: #000;
content: '';
position: absolute;
top: -10px;
left: 0;
width: 30px;
height: 5px;
}
.hamburger:after {
background: #000;
content: '';
position: absolute;
left: 0;
top: 10px;
width: 30px;
height: 5px;
}
<a class="nav-container" href="##">
<div class="hamburger"></div>
</a>