Positioning :after-pseudo behind text in a link - html

I was wondering if it's possible to position the z-index of a :after-pseudo element so that it's behind a link's text. For example;
HTML
Here's a link
SCSS
a {
background: #666:
height: 40px;
padding: 0px 20px;
position: relative;
color: #FFF;
&:before {
/* this is occupied */
}
&:after {
content: "";
width: 100%;
height: 100%;
position: absolute;
display: block;
top: 0; left: 0;
background: #000;
}
}
What I'm trying to achieve here is to display the link's text. This is currently not happening because the :after element is overlapping it. I'd like to put the text to the front without using something like a <span> tag. Note: it should overlap its original background, but not the text.
Is there a way to achieve this, or is this simply impossible?

I found a proper solution. I'll use a box-shadow: inset 0 -3.125rem 0 #000; on the element instead. This way I don't have to use the :after element.
Thank you all for the comments.

You just need to add z-index:-1; to the :after-pseudo
a {
background: #666:
height: 40px;
padding: 0px 20px;
position: relative;
color: #FFF;
}
a:after {
content: "";
width: 100%;
height: 100%;
position: absolute;
display: block;
top: 0; left: 0;
background: #000;
z-index:-1;
}
Here's a link
https://jsfiddle.net/d8htv6a9/

It's as easy as setting the z-index: -1; for the :after pseudo-element.
&:after {
content: "";
width: 100%;
height: 100%;
position: absolute;
display: block;
top: 0;
left: 0;
z-index: -1; /* Set this */
background: #000;
}
Here's a JSFiddle: https://jsfiddle.net/thepio/tm9n0x5g/1/
EDIT:
Based on your comment, there is one trick you could use but I don't know if it will go along your animation. You could use a title attribute in the HTML itself and use it as the content of the :after pseudo-element.
a {
position: relative;
background: #666;
height: 40px;
padding: 0px 20px;
position: relative;
color: #FFF;
z-index: 1;
}
a:after {
content: attr(title);
width: 100%;
height: 100%;
position: absolute;
display: block;
top: 0;
left: 0;
background: #000;
color: white;
text-align: center;
}
Here's a link
Then you can perhaps fade it in/out or whatever you prefer.
Here's a new JSFiddle: https://jsfiddle.net/thepio/tm9n0x5g/1/

Related

create multiple pseudo elements in the same container

In this way, the last statement will be the only one visible due to the css cascade.
Isn't there an alternative way to have all three pseudo-elements in the same container?
div class="container"</div>
.container {
position: relative;
}
.container:after {
position: absolute;
content: "some text";
top: 10%;
left: 20%;
}
.container:after {
position: absolute;
content: "";
border: 5px solid yellow;
width: 50%;
top: 10%;
left: 20%;
}
.container:after {
position: absolute;
content: "";
border: 5px solid blue;
width: 60%;
top: 30%;
left: 50%;
}
As it is not possible to have multiple before or after pseudo elements on one element, this snippet takes a different approach to placing the yellow and blue underlines.
They are drawn using CSS background-image with two linear-gradients on one pseudo element. Each is sized and positioned appropriately.
The snippet produces this:
body {
width: 100vw;
display: flex;
justify-content: center;
}
.container {
--h: 0.5em;
/* set this to the height of each underline */
font-family: sans-serif;
font-size: 36px;
text-align: center;
position: relative;
display: inline-block;
}
.container::before {
content: '';
position: absolute;
width: 300%;
height: 2em;
left: 50%;
transform: translateX(-50%);
background-image: linear-gradient(#ffff66, #ffff66), linear-gradient(cornflowerblue, cornflowerblue);
background-size: 95% var(--h);
background-position: left 0.75em, right 1em;
background-repeat: no-repeat;
z-index: -1;
}
<div class="container">Some text</div>
Obviously you will want to set the various dimensions to suit your particular case.

::before element aligned to the right

I have a button which already uses an ::after element (underneath) , but I want to add a shape/element to the right side of the div.
a {
position: relative;
display: inline-block;
padding: 15px 10px;
margin: 0 5px;
color: #222;
font-size: 0.9em;
font-weight: 700;
}
a:after {
content: "";
position: absolute;
display: inline-block;
background-color: #d11e5d;
margin: 0 auto;
height: 3px;
width: 0;
bottom: 6px;
left: 0;
right: 0;
}
a.btn-contact::before {
background: #0c0;
content: '';
display: inline-block;
float: left;
height: 10px;
width: 10px;
position: absolute;
right: 0;
top: 0;
}
button
I got it to the right of the link, but I want it to vertically align on the right
EDIT: full css/html provided. Wordpress site with .btn-contact class applied to just one button (the one I want the shape beside)
i changed a bit the html you provided ( guessing that it's what you want )
do not use float to absolute elements. it doesn't have any effect.
use top:50% which means half of li a height ( the item with position relative ) and also transform:translateY(-50%) which moves up :before width half of it's height.
these 2 styles together vertical-align in the middle the before pseudo-element
check snippet below and let me know if this is what you were looking for
li a {
position: relative;
display: inline-block;
padding: 15px 10px;
margin: 0 5px;
color: #222;
font-size: 0.9em;
font-weight: 700;
}
li a:after {
content: "";
position: absolute;
display: inline-block;
background-color: #d11e5d;
margin: 0 auto;
height: 3px;
width: 0;
bottom: 6px;
left: 0;
right: 0;
}
li.btn-contact a::before {
background: #0c0;
content: '';
display: inline-block;
height: 10px;
width: 10px;
position: absolute;
right: 0;
top: 50%;
transform:translateY(-50%)
}
<li class="btn-contact">button</li>

Drop lined shadow on button text

I want to drop lined shadow on a link or button. Kind of flat style. You can see this implemented already here
a.button {
position: relative;
z-index: 2;
display: inline-block;
padding: 10px;
text-decoration: none;
color: #fff;
font-family: arial;
}
a.button:before {
content: "";
display: block;
position: absolute;
z-index: 1;
width: 100%;
height: 100%;
right: 0px;
bottom: 0;
border: 3px solid #000;
}
a.button:after {
content: "";
display: block;
position: absolute;
z-index: 0;
width: 100%;
height: 100%;
background-color: red;
border: 3px solid red;
right: -6px;
bottom: -6px;
}
a.button:hover:after {
background-color: green;
border: 3px solid green;
}
Click Here
But the problem is that the box and the shadow are overlapping on the button text so the button text is not visible. Is there any way to fix that? I don't want to add any extra html tags like span into this as the links are going to be auto generated using Wordpress. So that is why I have used before-after pseudo css.
Set :after z-index to -1. to send it backward futher
a.button:after {
z-index: -1;
}

Mix Blend Mode button hover event issue

I'm trying to get a mix-blend-mode working so that a background slide happens. I have put together a jsfiddle of it sort of working.
Problem is, need it to look more like this.
But I do not want to get rid of the skew on it or the slide in from the right. I've tried using the same blend modes as in that example, but no matter what I do, it doesn't maintain the red slide color on hover and white text under the red color. I would like to use pseudo elements only and keep the html to a bare minimum here. I would think this is possible using the pseudo elements, however the blend modes are not cooperating with me and not sure how to get it to look more like the second fiddle. My html is as follows:
View Project
CSS is:
a {
position: relative;
text-decoration: none;
display: inline-block;
padding: 15px 30px;
border: 1px solid #f16251;
background: black;
color: #f16251;
font-size: 30px;
font-family: arial;
mix-blend-mode: normal;
overflow:hidden;
z-index: 1;
}
a:before {
content: '';
position: absolute;
top: 0; bottom: 0; right: 0;
width: 100%; height: 100%;
background: red;
mix-blend-mode: multiply;
transform-origin:0 0 ;
transform:translateX(100%) skewX(30deg);
transition: transform .25s;
z-index: 3;
}
a:hover:before {
transform: translateX(45%) skewX(30deg);
}
a:hover:after {
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
width: 100%;
height: 100%;
background: white;
}
a:after {
content: '';
position: absolute;
top: 0; bottom: 0; right: 0;
width: 100%; height: 100%;
background: white;
mix-blend-mode: difference;
z-index: 2;
}
How do I get the background text to display in white only when the red color slides over that text? My mix-blend-mode code has to be wrong, but it looks like it is possible to do here.
Well, this was fun :)
a {
position: relative;
text-decoration: none;
display: inline-block;
padding: 15px 30px;
border: 1px solid #f16251;
background: white;
color: black;
font-size: 30px;
font-family: arial;
mix-blend-mode: normal;
overflow:hidden;
z-index: 1;
}
a:before {
content: '';
position: absolute;
top: 0; bottom: 0; right: 0;
width: 100%; height: 100%;
background: white;
mix-blend-mode: difference;
transform-origin:0 0 ;
transform:translateX(100%) skewX(30deg);
transition: transform .25s;
z-index: 3;
}
a:hover:before {
transform: translateX(45%) skewX(30deg);
}
a:after{
content: '';
display:block;
top: 0;
left:0;
bottom:0;
right:0;
position: absolute;
z-index: 100;
background: #f16251;
mix-blend-mode: screen;
}
View Project

Heading with line afterwards, with 2 different sizes

I am trying to achieve the following, with pure CSS and no images:
As you can see, its a heading with a line afterwards. The problem is, that the line should has 2 different colors and more important, 2 different heights.
The first parts color is orange, has a height of 3px and a fixed width of 100px (padding-left: 15px)
The sedond parts color is #E1E1E1 and should fill the rest of the line.
My first try was this:
<h1><span>OUR ARTICLES</span></h1>
<style>
h1 {
overflow: hidden;
}
h1 span {
display: inline-block;
position: relative;
}
h1 span:after {
content: "";
position: absolute;
height: 1px;
top: 45%;
width: 999px;
background: #E1E1E1;
border-left: 100px solid orange;
left: 100%;
margin-left: 15px;
}
</style>
See http://jsfiddle.net/oyxmxoLs/
But as you can see, I can't make the orange part thicker than the grey one.
Any ideas?
Another way: Flexbox
With display: flex you don't have to give the line a certain width and you can make sure it is always responsive.
We are going here with an progressive enhancement approach. We'll make a cut after IE8 by using ::before instead of :before. In IE9 only the grey line will be shown (underneath the title).
h1 {
align-items: center;
color: #444;
display: flex;
font: 18px/1.3 sans-serif;
margin: 18px 15px;
white-space: nowrap;
}
h1::before {
background-color: orange;
content: "";
height: 4px;
margin-left: 10px;
order: 2;
width: 100px;
}
h1::after {
background-color: #E1E1E1;
content: "";
display: block;
height: 2px;
order: 3;
width: 100%;
}
<h1>Our articles</h1>
Do not forget to add vendor-prefixes!
You can solve this by using :before and :after
http://jsfiddle.net/oyxmxoLs/1/
h1 {
overflow: hidden;
}
h1 span {
display: inline-block;
position: relative;
}
h1 span:before {
content: "";
position: absolute;
height: 1px;
top: 45%;
width: 999px;
background: #E1E1E1;
left: 100%;
margin-left: 15px;
}
h1 span:after {
content: "";
position: absolute;
height: 3px;
top: 45%;
width: 100px;
background: orange;
left: 100%;
margin-left: 15px;
margin-top:-1px;
}
<h1><span>OUR ARTICLES</span></h1>
You can also use the :before pseudo-element to add the orange line.
h1 {
overflow: hidden;
}
h1 span {
display: inline-block;
position: relative;
}
h1 span:after, h1 span:before {
content: "";
position: absolute;
height: 1px;
left: 100%;
top: 45%;
margin-left: 15px;
}
h1 span:after {
width: 999px;
background: #E1E1E1;
}
h1 span:before {
height: 3px;
z-index: 1;
margin-top: -1px;
border-radius: 2px;
width: 100px;
background: orange;
}
<h1><span>OUR ARTICLES</span></h1>