Understanding the visibility characteristics of pseudo-elements

After doing some searching online, I'm still puzzled by the behavior of pseudo elements like :before and :after.

Let's say I have a div with various elements inside - paragraphs, links, icons, etc. If I add an :after pseudo element to one of the links and fill it with content such as an icon.

a:after{
    content:"\f001";
    font-family: "Awesome icon font";
    display: inline-block;
}

I can toggle the visibility of this element with a button click.

The problem arises when the element is hidden - the pseudo element doesn't appear in the DOM. It only shows up once I toggle its visibility from display:none; to something else like display: inline-block;.

For example, I have a navigation menu that slides up or down when clicking on a 'hamburger' icon. One of the menu items is a shopping cart icon. While all the text menus slide smoothly, the shop icon appears late, giving a sloppy appearance.

Why does the display value of the containing element affect the visibility of pseudo elements, even though they themselves have a different display value than none? Additionally, if the container has display: none;, the pseudo element is not shown in the DOM.

EDIT:

Here is an example fiddle for reference: http://jsfiddle.net/at4u56fy/

Answer №1

Let's refer to the provided documentation...

Regarding display: none;

This specific value results in the element not being visible within the formatting structure. In visual media, no boxes are generated for the element and it does not affect layout. Descendant elements also do not generate any boxes; the element and its content are completely removed from the formatting structure. The 'display' property set on descendants cannot override this behavior.

It is important to note that setting display to 'none' does not create an invisible box; it eliminates any presence of a box at all. CSS offers functionalities allowing an element to generate influential boxes in the formatting structure without being visibly present. For more information, please refer to the section on visibility.

Further exploration into formatting structure reveals:

The formatting structure is derived from the annotated document tree. It typically mirrors the document tree closely, but may vary significantly especially when authors utilize pseudo-elements and generated content. The nature of the structure is implementation-dependent and might not conform to typical "tree-shaped" structures. Information contained in the formatting structure may differ from that of the document tree. For instance, if an element in the document tree has 'display' property set to 'none', then it will not contribute anything to the formatting structure. Conversely, a list element could provide additional details such as content and list style information (e.g., bullet image).

In this phase, it is worth noting that CSS user agents do not modify the document tree. Content produced by style sheets during this process is not reintegrated back into the document language processor for reevaluation.

Therefore, setting an element to display: none; appears to lead user agents to disregard CSS-generated content. Consider utilizing visibility: hidden; as an alternative approach.

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

Utilizing Shadow Root and Native Web Components for Seamless In-Page Linking

An illustration of this issue is the <foot-note> custom web component that was developed for my new website, fanaro.io. Normally, in-page linking involves assigning an id to a specific element and then using an <a> with href="#id_name&quo ...

How can I prevent my carousel div from aligning with the NavBar on the same line?

I recently created a simple HTML landing page template, which includes a navigation bar in the header section and a carousel displaying various icons in the body. However, I am facing an issue where both elements are appearing together instead of on separa ...

Preview your uploaded image before finalizing

I am currently working on implementing a new upload feature for users of our forum. This feature will allow them to upload a personal picture. Here is what I have developed so far: HTML <label>Profile image</label> <div class="phot ...

Fixing malfunctioning bootstrap dropdown menus in a few simple steps

For a while, I have been using the bootstrap dropdown code as a template and it was working perfectly fine. However, after ensuring everything is up to date, all my dropdowns have suddenly stopped working. Even when I tried pasting the bootstrap code dire ...

JavaScript-powered horizontal sliderFeel free to use this unique text

I'm new to JS and trying to create a horizontal slider. Here's the current JS code I have: var slideIndex = 0; slider(); function slider() { var i; var x = document.getElementsByClassName("part"); for (i = 0; i < x.length; i++) { x[i].styl ...

The HTML/JS output displays a table with missing horizontal borders

Why aren't my horizontal borders showing up in the output section? Take a look at the code and screenshot below: I want to include horizontal borders and prevent the date fields from moving to the row below. https://i.sstatic.net/qO1KC.png I would li ...

Unable to load JavaScript files on the client side

Currently, I am in the process of learning how to develop an NPM package. Initially, I assumed it would be a simple task: create JS files with object literals, export modules, and then have users require the file and utilize the object like objectname.meth ...

Struggling to identify which css or bootstrap statement is impacting the arrangement of the form layout

I'm facing an issue with setting a border and background color behind a form. When I attempt to do so, the form is aligned to the right and I can't pinpoint which part of my code is causing this misalignment. Here's an example: https://jsfi ...

Image container unable to resize to fit within a CSS grid cell

I'm currently working on organizing a CSS grid for my images on a tribute page project from free code camp. I was able to set up the grid as intended, but I am facing some difficulties in making the images fit perfectly within each cell. Some images a ...

Tips for highlighting the final word in the headline using an underline

I'm attempting to create a title style similar to what is shown below https://i.sstatic.net/Qao4g.png The issue I'm facing is achieving the underline effect. I tried using title:after but it didn't give me the exact result I'm looking ...

Creating an image slider that pauses on mouse hover

Here is a code snippet that I'd like to share <body> <div id="slideshow"> <div> <img src="assets/images/home-banner.jpg" width="995" height="421" alt=""/> </div> <div&g ...

Error retrieving data for Android RSS reader description

I am currently working on an Android RSS reader, and I've encountered a challenge while trying to retrieve data from RSS feeds that have HTML content in the description section. Here's an example: <item> <title>Katima&#45;Com ...

Tips for setting up a hierarchical mat-table within a parent table that supports expandable rows using Angular Material

Here is the data that I am working with: [ { "_id": "c9d5ab1a", "subdomain": "wing", "domain": "aircraft", "part_id": "c9d5ab1a", "info.mimetype": "application/json", "info.dependent": "parent", ...

Adapt the dimensions of the iframe to perfectly match the content within

Looking for a way to dynamically adjust the size of an iframe to perfectly fit its content, even after the initial load. It seems like I'll need some kind of event handling to automatically adjust the dimensions based on changes in the content within ...

Importing global variables in Angular SCSS files

Considering a transition to SCSS within my Angular project, I am primarily interested in utilizing the variables feature. Currently, I have been relying on CSS variables, which are conveniently declared in :root in my styles.css file and can be accessed in ...

Error: The program is unable to find the specified property to match, resulting in an undefined error

I am currently working on a project in React JS where I need to display text instead of a profile image if it is not available. To achieve this, I have created a function that takes the current customer's name as input and extracts the first character ...

The term "Function is not defined" suggests that the

Here is the code I am working with: // Javascript file verbatims.js : class Verbatims { list() { return ["c'est super", "j'aime pas", "ça marche bien"]; } } module.exports = Verbatims; <!DOCTYPE html&g ...

Utilizing ease-in effect on show more button clicks in CSS

When I click "show more," I want to have a smooth ease-in/out animation for 3 seconds. However, I am facing difficulties achieving this because I am using overflow: hidden and -webkit-line-clamp: 2; Are there any other methods to accomplish this? https: ...

Arranging column contents neatly on small screens

Although there are similar answers available, I want to address those who are trying to prevent their columns from stacking on XS display. Despite having two columns, the content is still stacking at certain displays. This issue is occurring while using B ...

When it comes to printing, the @media rule for portrait orientation will not be effective if landscape orientation is also indicated

My goal is to create a div that will occupy the entire A4 page when printing, regardless of whether portrait or landscape mode is selected in the print dialog window. test.html: <!DOCTYPE html> <html lang="en"> <head> <me ...