Why does pseudoelement's background hide parent's? - html

The parent's background-color and the border-radius is hidden behind the pseudo element. Why is the pseudo element not behind the parent even though it has a z-index of -1?
.btn {
text-decoration: none;
background-color: royalblue;
font-family: sans-serif;
font-weight: bold;
border-radius: 5px;
color: white;
display: inline-block;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.btn::after {
content: "";
display: inline-block;
width: 100%;
height: 100%;
background-color: cornflowerblue;
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
Download free app

The culprit is the transform property that create a stacking context thus your pseudo element will painted inside this stacking context and will logically be above the background whataver the z-index you will use.
Remove transform to see the difference. I increased the border-radius and changed the color to better see.
.btn {
text-decoration: none;
background-color: red;
font-family: sans-serif;
font-weight: bold;
border-radius: 50px;
color: white;
display: inline-block;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
}
.btn::after {
content: "";
display: inline-block;
width: 100%;
height: 100%;
background-color: cornflowerblue;
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
Download free app
In case you want the pseudo element to be below the parent you should avoid any properties that create a stacking context or it will be impossible.
Or you consider another pseudo element to create the background layer and you will be able to control the stacking like you want:
.btn {
text-decoration: none;
font-family: sans-serif;
font-weight: bold;
color: white;
padding: 20px;
position: absolute;
transform:translate(-50%,-50%);
top: 50%;
left: 50%;
}
.btn::before,
.btn::after{
content: "";
position: absolute;
top: 0;
left: 0;
right:0;
bottom:0;
z-index:-1;
}
.btn::before {
background-color: cornflowerblue;
}
.btn::after {
background-color: red;
border-radius: 50px;
}
Download free app
Some related questions:
Why elements with z-index can never cover its child?
Is there any way to place z-index according not to its parent element

You just need to add overflow: hidden; to the parent element.
.btn {
text-decoration: none;
background-color: royalblue;
font-family: sans-serif;
font-weight: bold;
border-radius: 5px;
color: white;
display: inline-block;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
overflow: hidden;
}
.btn::after {
content: "";
display: inline-block;
width: 100%;
height: 100%;
background-color: cornflowerblue;
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
Download free app

Related

Keep the pseudo element in between the background and main content

I am making buttons by using the anchor tag. I want to make an effect but it is not working as expected. pseudo-element is flowing over the button and text is also being hidden as well. But I want something like this, the pseudo-element will be in between the button and background.
section {
padding: 80px 0;
}
section.one {
background: #76B39D;
}
.button {
color: white;
margin: 10px;
font-size: 17px;
font-weight: 700;
letter-spacing: 1px;
text-transform: capitalize;
}
.button-type-1 {
border: 2px solid #fff;
padding: 10px 33px;
overflow: hidden;
position: relative;
}
.button-type-1:after {
position: absolute;
content: '';
width: 0px;
height: 0px;
background: #fff;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
border-radius: 50%;
z-index: 0;
transition: all 0.4s ease;
}
.button-type-1:hover:after {
width: 200px;
height: 200px;
}
.button-type-1:hover,
.button-type-1:focus {
text-decoration: none;
}
<section class="one">
read
</section>
I like to do, the pseudo element background will be fit to button size on hover.
And the button text will also be seen but i thing z-index is messed up somewhere or anything else.
z-index 1 your relative .button-type-1 and -1 the :after pseudo.
Also, make your button inline-block to prevent the :after element overflow
section {
padding: 80px 0;
}
section.one {
background: #76B39D;
}
.button {
color: white;
margin: 10px;
font-size: 17px;
font-weight: 700;
letter-spacing: 1px;
text-transform: capitalize;
}
.button-type-1 {
border: 2px solid #fff;
padding: 10px 33px;
overflow: hidden;
position: relative;
display: inline-block;
z-index:1;
}
.button-type-1:after {
position: absolute;
content: '';
width: 0px;
height: 0px;
background: #fff;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
border-radius: 50%;
z-index: -1;
transition: all 0.4s ease;
}
.button-type-1:hover:after {
width: 200px;
height: 200px;
}
.button-type-1:hover,
.button-type-1:focus {
text-decoration: none;
color:#000;
}
<section class="one">
read
</section>

How to create a dot / small circle under text?

How can I make a dot under text using only CSS as shown in below picture?
The picture needs to be always in middle with any length of string.
Probably I need to use :before OR :after? I've tried but result was awful.
A transformed pseudo element can be used to create this:
body { text-align: center; }
.text {
display: inline-block;
vertical-align: top;
padding-bottom: 10px;
position: relative;
text-align: center;
padding: 20px 10px;
line-height: 24px;
min-width: 100px;
background: #333;
font-size: 20px;
color: #fff;
}
.text::before {
transform: translateX(-50%);
border-radius: 100%;
position: absolute;
background: blue;
bottom: 10px;
height: 8px;
content: '';
width: 8px;
left: 50%;
}
<div class="text">about</div>
.char {
display: inline-block;
position: relative;
}
.char::before {
content: '.';
display: inline-block;
position: absolute;
bottom: -0.5em;
left: 0;
text-align: center;
width: 100%;
}
After writing this question on stack i come up with idea:) Its works excatly like I want :)

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>

Css header tags styling

I want to style my header tag H1 as per the following design!
Header Design
The basic css to create is box is easy, how do i create the line after the box i'm using this css for creating the box
h1{
background: #a42f2f;
color: #fff;
width: 179px;
padding: 0px 8px;
}
My Fix for a wordpress Template:
.page_box h1{
background: #a42f2f;
color: #fff;
width: auto;
padding: 5px 11px;
position: relative;
z-index: 2;
font-size: 18px !important;
margin: 9px 0;
display:inline-block;
}
.page_box{
overflow-x:hidden;
}
.page_box h1:after{
content: "";
position: absolute;
height: 2px;
top: 14px;
width: 900px;
background: #ff6300;
margin-left: 11px;
}
I think this fiddle should help you.
h1{
color: #fff;
width: 100%;
padding: 0px 8px;
position: relative;
font-family: sans-serif;
text-transform: uppercase;
}
h1:before {
content: ' ';
width: 178px;
background-color: #a42f2f;
position: absolute;
height: 100%;
z-index: -10;
left: -8px;
}
h1:after {
content: ' ';
border-bottom: 2px solid orange;
display: block;
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 100%;
z-index: -20;
}