The :after pseudo-element is not being shown beneath its relative parent

Encountering a puzzling CSS challenge... I am trying to enhance the appearance of an element by adding visual thickness. This task is usually straightforward, using an :after pseudo-element with absolute positioning within a parent that has relative positioning. However, despite adjusting z-index values and positioning, the pseudo-element remains hidden beneath the text of its parent element's content rather than displaying on top as expected.

You can view the issue on this page: . Look for the violet "Combine and Publish" button at the end of the page, which should exhibit the desired thickness when implemented correctly.

For a demonstration, refer to this example: http://jsfiddle.net/sugardaddy/vq2x2uue/

HTML

<a href="#" class="ctabutton ctabutton-primary ctabutton-standard "><i class="ft-webicons-coffee"></i>Read Jane’s story</a>

CSS

.ctabutton-primary::after {
    background-color: #521a4a;
}
.ctabutton::after {
    border-radius: 0.4em;
    bottom: -3px;
    content: "";
    height: 20px;
    left: 0;
    position: absolute;
    width: 100%;
    z-index: -1;
}
.ctabutton-primary, .ctabutton-primary:hover {
    background-color: #852a79;
    color: #ffbb1a !important;
}
.ctabutton, .newsform .mktoForm .mktoButtonWrap.mktoRound .mktoButton {
    border-radius: 0.3em;
    display: inline-block;
    font-weight: 400;
    padding: 0.5em 1em;
    position: relative;
    transition: all 200ms ease-in-out 0s;
    vertical-align: middle;
}

Any assistance would be greatly appreciated!

UPDATE: Upon further investigation, it seems that applying a negative z-index to the pseudo-element causes it to disappear only in specific areas of the page, while working perfectly for buttons within menus. This may suggest conflicts with z-index rules applied to parent elements, though the exact cause remains unclear.

UPDATE 2: It appears that the issue may stem from nested relative elements affecting z-index inheritance. By setting each level with z-index:inherit, the pseudo-element becomes visible. However, observation suggests that it is only visible when the first-level parent lacks a background color (such as the beige one). Investigation continues...

Answer №1

Imaginary tags known as pseudo elements require the 'display' property to be explicitly defined in order for them to function properly. Without this declaration, they will not work as expected. Simply include the following code:

.ctabutton::after, .ctabutton-primary::after {
    display:block;
}

Answer №2

For a solution, follow these instructions: 1. Enclose this text within a span tag

 <span class="cmbSpan">Combine & Publish</span>

Add the following style to your stylesheet.

 .cmbSpan, .ft-webicons-sources{
     z-index: 999;
     position: inherit;
 }

Remove the z-index from this class .ctabutton::after{}

Check out the Fiddle here

Answer №3

Essentially, the :after & :before pseudo elements are considered as part of an element's content box, meaning they can only be stacked within the element itself and not outside of it.

A more effective solution would be to wrap the text "Combine & Publish" in a separate wrapper element like , and then position :before underneath the element.

Below is an example code snippet:

.ctabutton::after {
  bottom: -3px;
  content: "";
  height: 20px;
  left: 0px;
  position: absolute;
  width: 100%;
  border-radius: 0.4em;
}
.cont {
  position: relative;
  z-index: 1;
}
<a class="ctabutton ctabutton-primary ctabutton-standard " href="/combine-and-publish/">
  <span class="cont">  <i class="ft-webicons-sources"></i>Combine &amp; Publish </span>
</a>

Answer №4

I managed to achieve this by making a slight modification to my DOM structure based on some of the suggestions provided here. I enclosed the content of the "button" within a span and applied all the necessary properties to ensure it visually overlaps the pseudo-element.

HTML

<a href="#" class="ctabutton ctabutton-primary ctabutton-standard "><span><i class="ft-webicons-coffee"></i>Read Jane’s story</span></a>

CSS

.ctabutton, 
.newsform .mktoForm .mktoButtonWrap.mktoRound .mktoButton {
    border-radius: 0.3em;
    font-weight: 400;
    display: inline-block;
    vertical-align: middle;
    position: relative;
    -webkit-transition: all 200ms ease-in-out;
    -moz-transition: all 200ms ease-in-out;
    transition: all 200ms ease-in-out;
}
.ctabutton span {
    display: block;
    padding: 0.5em 1em;
    border-radius: 0.3em;
    position: relative;
    z-index: 1;
}

.ctabutton::after {
    bottom: -3px;
    content: "";
    height: 20px;
    left: 0px;
    position: absolute;
    width: 100%;
    border-radius: 0.4em;
}

.ctabutton-primary span, 
.ctabutton-primary:hover span {
    background-color: #852a79;
    color: #ffbb1a !important;
}

.ctabutton-primary::after { background-color: #521a4a; }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Upon loading the page, IE11 activates a CSS transition even when a media query that is not applied is present

Encountering a unique situation with IE11 where a panel (DIV) transition is only activated on pageload if a media query matched the highest CSS specificity for the element. The issue does not occur on Chrome, Firefox, or Safari on tablets and phones. The d ...

Problem with personalized Domino login page

I have been working on creating a custom login page in domcfg.nsf. Everything is functioning as expected, except for the Style Sheets. I am attempting to load them from the same Domino server, but it appears they are unable to load until I actually log in ...

I'm having trouble getting my website to align in the center of the

Struggling to center my website no matter what I try. I experimented with different CSS properties but haven't had much luck. margin: auto 0; I also attempted the following: margin-left: 50%; margin-right: 50%; The closest I got was aligning every ...

The appearance of the drop-down menu in IE is not the same as in other

I have implemented css bootstrap 3.2.0 on my website. However, I have noticed some discrepancies when viewing the site in Internet Explorer. The menu options have a thick black border and the dropdown menu button appears different compared to other browse ...

Unable to center div container with margin set to auto

I'm struggling to center my container on the website. My goal is to have it centered and take up about 80% of the page so I can incorporate an image slider and text inside. Using margin: 0 auto doesn't seem to be achieving what I want. The backgr ...

Make sure to always nest child divs within their parent div to maintain

Hey there, I have a question that might seem silly to some. I've come across similar questions but none of the solutions worked for me. The problem I'm facing is with two nested divs - one inside the other. I want the inner div (taskbar-bar) to ...

Leveraging jQuery's .animate method to create engaging search box animations

Greetings esteemed individuals... I am currently working on enhancing the search component for my application... http://jsfiddle.net/SkyKOG/K8utq/24/ input{ display:none; width:0px; color:#999; } Initially, I only had a simple search box so ...

Positioning Images with CSS Backgrounds

I am looking to create a fluid background that stretches widthwise, while adjusting its height based on the content. If the content is smaller, I want the background to be truncated. However, if there is a lot of information, I would like the background to ...

The Popover in Twitter Bootstrap disappears beneath the navigation bar

I have a help button located in my navigation bar that triggers a popover feature. <div class="navbar navbar-fixed-top"> <div class="navbar-inner"> <div class="container"> <div class="nav pull-right"> ...

Improper layout on laptop screens with smaller dimensions

The design looks great on a 24-inch laptop, but on an 11-inch screen, the layout becomes distorted. The right column shifts down... Check out the layout here Below is my code: <div class="row" style="margin-left: 325px;"> <div class="span12 ...

The jQuery click event to change the color function is functioning properly, but there seems to be an issue with

I'm having trouble changing the font-family using the code below, even though the color is changing properly. What am I missing here and how can I correct it? $(document).ready(function(){ $("body").on("click",".selectableColor", function(){ ...

ReactJs CSS - This file type requires a specific loader for processing. There are currently no loaders configured to handle this file

I've noticed that this issue has been raised multiple times before. Despite going through all the questions, I still can't seem to resolve it. The transition from Typescript to Javascript went smoothly until I reached the implementation of CSS. U ...

What is the reason for my HTML elements inheriting the opacity of their parent elements?

After inserting an image tag into my HTML document, I noticed that it was inheriting the opacity from the parent div element. Despite multiple attempts to adjust this, I have been unsuccessful. Here is an example: <div style="opacity: 0.7"> <img ...

Revisiting the Issue: Jquery Hover Effect Fails to Function in Internet Explorer

Can anyone provide assistance with this issue? The code snippet works perfectly in all browsers except for IE, where the menu does not fade in and looks unattractive. html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

The calculator I designed using HTML, CSS, and JavaScript is experiencing difficulty adjusting to various screen sizes

I recently built a calculator using HTML, CSS, and JavaScript. It works perfectly on PC or big screens, but when viewed on smaller devices like phones or tablets, the display gets cut off and does not adjust properly. Here are some example pictures for ref ...

The styling of the Material UI autocomplete listbox does not affect its appearance

I am working on an autocomplete feature that groups items by titles. I am trying to adjust the height of the first group to be different from the rest, but I am facing some challenges since there is no unique identifier for the ul component. To work around ...

What is the best way to incorporate text below grid columns in html?

Can you show me how to insert text below a column using the grid system? Here is what I am trying to accomplish Here is the code I have created: .grid{ display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-auto-rows: 240px 240px; row-g ...

Add a hovering effect to images by applying the absolute positioning property

On my React website, I am utilizing Tailwind CSS and I am attempting to showcase an image that has interactive elements. I want certain parts of the image to increase in size slightly when hovered over. My approach to achieving this was to save each part o ...

Is it possible to smoothly switch between two states using just a single animation class?

I am trying to trigger an animation with a single class, and then have that animation play in reverse when the class is removed. To better explain, I have created a CodePen example of my current progress. In the CodePen, you can see that when the class . ...

Turn off the ability to view the content of .css and .js files within the browser

Earlier, I inquired about how to disable file and folder listing and discovered that it can be achieved using a file named .htaccess. To disable folder listing, I entered Options -Indexes in the .htaccess file located in the parent folder. Additionally, to ...