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

When state updates in React, the component will rerender without affecting its style

There seems to be a minor oversight on my part. The issue arises in the parent component where I maintain a state of selected items, which are added from the child component. The background color of the child component changes when an item is selected. Ad ...

Assist me in temporarily altering the color of menu items in a sequential manner upon the page's loading

Currently, I'm working on a website that features a subtle dark grey menu at the top of every page. The menu is built using HTML and CSS with a list structure. To highlight the corresponding menu item based on the current page, I am utilizing the ID a ...

Seamless animation when collapsing element using angular and css exclusively

I am attempting to incorporate a collapsible feature into my application. Rather than relying on external libraries like jQuery, I want to exclusively utilize Angular. For demonstration purposes, I have created a very basic example here: http://jsfiddle.n ...

Error in HTML5 video: Unable to access property '0' as it is undefined

I am trying to create a simple block displaying an HTML5 video tag. I want the ability to play different videos along with their titles from a JSON file by using previous and next buttons. Clicking the next button should play the next video, and the same g ...

The Angular checked functionality is not working as expected due to the presence of ngModel

Just getting started with Angular here. I’m working on a checkbox table that compares to another table and automatically checks if it exists. The functionality is all good, but as soon as I add ngModel to save the changes, the initial check seems to be ...

Positioning HTML elements using CSS and avoiding the use of tables

I'm struggling with my clear CSS declarations. This is how I'd like it to appear: View the Fiddle demo HTML: <div id="timesheeteditor"> <div id="weekselector"> <div>Week 1</div> <div>Week 2</div> ...

CSS3 keyframes hover animation for Firefox

I implemented keyframes animation for a div on mouse hover using pure CSS3. The animation runs smoothly on all browsers (Google Chrome, Safari, IE, Opera) except for Firefox! I'm puzzled as to why it doesn't work only in Firefox. The animation ...

Can you explain the purpose of :not(style) ~ :not(style) in CSS?

Upon investigating, I found that Mui automatically included a :not(style) ~ :not(style) selector which required the use of !important in my sx to override. .css-1rb8ayf-MuiStack-root > :not(style) ~ :not(style) { margin-top: 40px; } .css-d13d9m-MuiSta ...

Learn a valuable trick to activate CSS animation using a button - simply click on the button and watch the animation start over each time

I already know how to do this once. However, I would like the animation to restart or begin again when the user clicks on it a second time. Here is what I have: function animation() { document.getElementById('ExampleButton').className = &apo ...

Having difficulty submitting a form with ajax, while accomplishing the same task effortlessly without ajax

I have been experimenting with submitting a form using ajax to the same .php file. When I submit the form without ajax directly (form action), the database gets updated. However, when I try the same process with ajax, there is no change in the database. H ...

JQuery ID Media Queries: Enhancing responsive design with targeted

Is it possible to integrate a media query into the selection of an ID using JQuery? For example: $('#idname') $('@media (max-width: 767px) { #idname }') In essence, can you target the #idname with that specified media query in JQuery ...

Arranging two <ul> elements within an angular template

I need assistance with aligning two ul blocks. Currently, they are displaying next to each other, but their alignment is starting from the bottom instead of the top: <div class="ingredients-container"> <div style="display: inline-block; width: ...

What is the best way to include the border-radius CSS property to a dropdown element in Ant Design?

Adding the border-radius CSS property to my dropdown component from ant design has been a bit challenging for me. I attempted to modify the antd-css file and include a style object directly into the component, but unfortunately, I did not achieve the des ...

centering headers using tailwind styles

I am facing a challenge with positioning my main title, logo, and subtitle. I want the subtitle to be centered regardless of the logo's width. Currently, the position of the sub title changes based on the logo's width, resulting in it not aligni ...

What is the best way to use HTML and CSS to center images and headings on a webpage?

This task is really testing my patience... I'm attempting to create a layout that resembles the following: Logo img|h1|img The horizontal lines flanking the subtitle serve as a visual guide, like this --- Subtitle --- Below is the snippet of H ...

Row in Bootstrap 3 fails to display correctly on medium-sized devices

Having trouble with divs of different sizes? I want a row that shows 3-column wide divs on medium & large screens and 6-column wide divs on small devices. <div class="container"> <div class="row"> <div class="service col-sm-6 col-md-3"& ...

The nuSelectable plugin enhances the functionality of jQuery

I am currently experimenting with the jQuery nuSelectable plugin in order to enable users to select multiple items simultaneously. Unfortunately, I am encountering difficulties in making the selection work as intended. You can find the plugin here. After ...

What is the best way to center align the button on my card?

I am having trouble aligning the "Check Github" button in the center of the card. I have tried using item-align: center;, justify-content:center;, and margin-right: 50px; but none of them seem to work on this particular element. Can you please provide guid ...

Incomplete width allocation for the floating div positioned to the left

I've encountered an issue with the placement of the side-text div in relation to the image. Currently, without specifying the width of the side-text div, it automatically moves to the bottom. To address this problem, I attempted using display:inline- ...

Transform your current website layout from a fixed 960-grid system with 12 columns into a fluid and

As a newcomer to web development, I have successfully created my portfolio website. I initially set the body width to 1600px because that matched the size of my banner background in Photoshop. I'm using a 960gs 12-column grid system, but when I view t ...