What is the process by which the browser displays pseudo-element selectors?

One thing that's been on my mind is the potential resource cost of using *:after. Would the pseudo element be the primary selector, or would all elements still be processed by the client?

In simpler terms, what impact does *:after have compared to just using * in terms of performance?


[edited. silly example. Sorry about that.]

To clarify, I've been experimenting with creating visuals using only CSS and ended up using many pseudo elements. I'm curious if I could save time by implementing:

*:after, 
*:before {
   content:'';
}

and its associated resource implications.

Thank you.

Answer №1

Is the pseudo element considered the key selector in this scenario?

Essentially, the subject of the selector is *, indicating that whichever element is matched by * will be responsible for generating the pseudo-element.

The term "key selector" lacks a clear definition when it comes to pseudo-elements, and unlike subject selectors, it's more of an implementation detail. If we were to interpret "key selector" as the "selector representing the box that is rendered," then we could argue that the pseudo-element serves as the key selector. However, without delving into implementation specifics, I refrain from making definitive statements since I am not involved in the implementation process.

In any case, it is reasonable to assume that the browser traverses the DOM and follows these steps for each selected element:

  1. Firstly, attempt to match the element against the selector, disregarding the pseudo-element at this stage (in this instance, *).

  2. If the element matches the selector, try to create a pseudo-element box as a child of this element's box (applicable to generated pseudo-elements like :before and :after) if relevant (certain elements cannot generate pseudo-elements).

  3. Apply styles to the pseudo-element box and render it similarly to other boxes.

The distinction between your two selectors lies in one containing a pseudo-element while the other does not. Since they serve different functions, comparing their performance may not be entirely fair. Nevertheless, both attempt to match any element in the DOM as they share the same catch-all selector *. Hence, barring specific optimizations, the additional overhead with *:after likely stems from generating and rendering the pseudo-element.

Regarding performance considerations, remember that matching an element to * is nearly instantaneous due to its guaranteed match. The real scalability concerns arise when combining it with complex selectors, but even then, the performance impact primarily hinges on the number of elements in the DOM rather than the actual styling rendering—unless dealing with thousands of elements.


In terms of your particular scenario, I would suggest refraining from creating pseudo-elements for every single element. When utilizing CSS for design purposes, aim to narrow down your selector by context to avoid unnecessary box rendering throughout the entire DOM. For instance, if you are exclusively designing within an element identified by a specific ID:

#mycanvas *:after, 
#mycanvas *:before {
   content:'';
}

Answer №2

Revised Question

I now have a clearer understanding of your query. When you utilize the * selector, it will select all elements, but this may not be precise as it selects everything.. If you prefer, you can utilize something like:

.container *:not(span) {
   color: red;
}

Alternatively (although this will target all nested elements within .container):

.container * {
   color: red;
}

View Demo


Original Question

I'm having trouble grasping your inquiry, but both selectors are TOTALLY DISTINCT.. they serve different purposes, I'm unsure if there's a mistake in your question, however utilizing *:after is often paired with content which should be incorporated either :before or :after.

For instance,

*:after {
    content: " <";
}

See Demo

On the other hand, when you use * .after, it will target elements with the class .after

* .after {
    color: red;
}

Second Demo


Regarding optimization, I don't believe using * on regular websites poses a significant performance issue. I frequently employ the selector below containing properties such as:

* {
   margin: 0;
   padding: 0;
   -moz-box-sizing: border-box;
   -webkit-box-sizing: border-box;
   box-sizing: border-box;
}

Lastly, there doesn't seem to be a valid reason for utilizing *:after unless you intend to add content using :after for each element.

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

I'm having trouble keeping my navbar fixed

Having trouble keeping my navigation bar fixed I attempted using the <nav class="fixed-nav-bar> with no success. I also tried adjusting it in CSS, but it still wouldn't stay fixed. <nav role="navigation" class="navbar navbar-default"> ...

Troubleshooting: Django project not functioning properly post transfer from Windows to Mac

Using Django 3.1.7 on a MacBook Pro now, I recently moved the Django project from a Windows 10 machine. Despite setting up the database and superuser again, running pipenv sync didn't result in serving any of my URLs or Admin page CSS styles. The serv ...

Creating space between flex items in Slick Carousel with Vue

This is my current Slick Carousel. There are 4 items in a row, and I am looking to insert some margin between each item while maintaining the existing aspect-ratio: 1.3/1; I'm struggling with overriding the classes from vue-slick-carousel. Does any ...

What steps do I need to follow in order to modify a CSS style definition to create a border around

I currently have a CSS style defined as follows: [class*="col-"] { border: 1px solid #dddddd; } This particular CSS definition gives borders to all of the bootstrap grid columns on my page. However, I now want to apply borders only to the bootstrap ...

maintain visibility of website menu while scrolling

I am having trouble getting my drop-down menu to stay fixed at the top of the screen while scrolling down on my website. Despite several attempts, I have not been able to achieve this using the current CSS and menu setup. Can someone please assist me wit ...

Unable to access the suggestion list within the modal

I incorporate the PrimeNG AutoComplete feature within a PrimeNG Dialog, displaying a list of elements below the AutoComplete in the dialog. My objectives are: To set the max-height of the modal dialog to 300, and have a visible scrollbar if the total ...

Utilize the .not() filter function in JavaScript

I am trying to apply a filter of not(.pseudo-element) using JavaScript, but I am unsure how to add it. So far, I have been able to extract #app from the DOM with the following code: const app = document.getElementById('app') app.style.filter ...

Is it possible to conceal any spans that are in close proximity to the cursor when hovered over?

Currently, I am working on a project that involves multiple spans placed side by side, with each span containing a letter of the text. My aim is to create a functionality where hovering over one of these spans will not only hide that particular span but al ...

Apply the CSS property to all divs with a shared class using jQuery

I am facing an issue with applying the CSS property 'z-index:99' to multiple divs that have the same class name. I want this CSS to be applied when clicking on a play button using jQuery. Currently, it only works for the first Play Button and I w ...

Left-align elements within a div container that is centered

I want to left-align my elements within a div container that is centered. I used the text-align property to center the overall div, but now I'm having trouble aligning another content container's text to the left within the center-aligned div. H ...

The datepicker on mobile devices fails to display the default text of dd/mm/yyyy

This JSP code uses Bootstrap5: <form action="" method="GET" class="row g-1 mb-3 "> <div class="col-lg-2 col-md-4 col-6 mb-2"> <input type="date" class="form-control" name=" ...

Revealing non-div elements with the CSS hover attribute

I'm looking for a way to utilize the CSS hover property on individual elements within a div in order to affect specific span elements. For example, hovering over "something1" should reveal "text1" and so forth. Is there a way to achieve this without ...

Unable to maintain the height of a div at 100% as it keeps reverting back to 0px

I'm having trouble setting the height of the individual parent columns (and their children) to 100% because for some reason, the div height keeps resetting to 0. The first column contains two child boxes that should each take up 50% of the page heigh ...

What is the process for updating the source in an input box?

How can I use JavaScript to update the src value of an iframe based on input? <input class="url" id="url1" type="url" value="youtube url"> <input onclick="changevideo()" class="add-video" id="add-video1" type="submit" value="add to play ...

What is the best way to emphasize a certain row within XSLT?

I am seeking a solution to emphasize rows containing the "year" element with a value exceeding "2000" in XML and XSLT. Here is the code snippet: XML <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="harrypotterbooks. ...

Utilize Bootstrap-Vue to ensure that an element expands to occupy the available space

In my Bootstrap-Vue application, I have set up the following hierarchy: <b-navbar.../> <b-container > <b-row align-v="center" style="min-height:100vh"> <b-col style="align-items:center"> <h1>404 Error&l ...

Should CSS properties be applied directly to the html tag?

Surprisingly, I recently discovered that it's possible to set the background-color of the html tag. It seems strange since I always thought the body was responsible for containing the first rendered tag. I used to believe that the html tag was just a ...

The picture within the bootstrap popover is spilling out of the popover

I need help resolving an issue on my webpage where images in popovers overflow when they reach a certain size. I used an online demo to create the page, but now I'm encountering this problem. How can I fix it? Check out the Js Fiddle for reference: h ...

Align images to the left in Typora for a cleaner look

Typora typically aligns all images in the center by default. https://i.stack.imgur.com/TrqIM.png In my search through the github.css, I was unable to locate any instances of img. $ grep img "/Users/me/Library/Application Support/abnerworks.Typora/themes ...

What is the best way to transmit the "theme" property to "Components" within a next.js application?

I'm currently working on a next.js app and dealing with the _app.tsx file: import '../styles/globals.css' import type { AppProps } from 'next/app' import { createTheme } from '@arwes/design' import { ThemeProvider, Global ...