Choose all pseudo elements from elements belonging to the class or ID labeled as 'x'

I am seeking a solution to target all the pseudo elements of an element that carries the class galleryBlock, as well as a method to target all the pseudo elements of an element with a specific id.

My code structure consists of a grid made up of multiple interconnected boxes, some spanning multiple columns, some spanning multiple rows.

Each box is assigned the class galleryBlock, along with a secondary class — the specific secondary class depends on the box's ratio, as the pseudo elements of these second classes are utilized to provide the box with a particular aspect ratio.

For instance, .square utilizes a pseudo element defined by :after to bestow the box with a square ratio, and so forth.

I aim to have the ability to change the opacity of any .galleryBlock element to 0.5 when hovering over it. Additionally, I wish to be able to customize the color of each pseudo element individually — assigning a distinct color to each pseudo element.

Below is the code snippet I have been experimenting with:

.galleryBlock:hover *:after {
    opacity:0.5
}
.galleryBlock *:after {
    transition: opacity 0.2s;
    -webkit-transition: opacity 0.2s;
    -moz-transition: opacity 0.2s;
}
#one *:after {
    background: linear-gradient(red, blue);
}
#two *:after {
    background: linear-gradient(green, yellow);
}
/* more code blocks here */

However, the implementation does not seem to be functioning as expected. I suspect that I am not targeting the pseudo elements correctly. How can I achieve this?

Answer №1

When it comes to boxes with the class .galleryBlock — whether they have specific IDs or not — the ::after pseudo-elements can be targeted in the following way:

.galleryBlock:after {
    -webkit-transition: opacity 0.2s;
    -moz-transition: opacity 0.2s;
    transition: opacity 0.2s;
}

And when in the :hover state:

.galleryBlock:hover:after {
    opacity: 0.5;
}

In this scenario, it is important to avoid white-spaces between simple selectors as white-space is utilized as a descendant combinator.

In simpler terms, using .galleryBlock:hover :after instructs the browser to target each ::after pseudo-element within descendants of elements with the .galleryBlock class when in the hover state. However, our intention is to focus on the ::after pseudo-element of the .galleryBlock elements themselves.


Additionally, it's essential to keep in mind that only one pseudo-element can be appended at the end of a CSS selector:

4. Selector syntax

A selector is a chain of one or more sequences of simple selectors separated by combinators. One pseudo-element may be appended to the last sequence of simple selectors in a selector.

A simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.

Therefore, the correct selector format should be: .galleryBlock:hover:after, not .galleryBlock:after:hover.

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

Refreshing a specific div within an HTML page

I am facing an issue with the navigation on my website. Currently, I have a div on the left side of the page containing a nav bar. However, when a user clicks a link in the nav bar, only the content on the right side of the page changes. Is there a way t ...

Do not apply styling to a particular page in CSS and HTML

Utilize the teachable.com platform and take advantage of their code Snippets feature (refer to attached photo #1) https://i.stack.imgur.com/dW1lB.png I inserted the following code: div { direction: rtl; } To modify the website's direction to be ...

How to use JQuery UI sortable to automatically scroll to the bottom of the page

Having trouble with a few sortable tables and here is how I initialized the sortable object: var options = { helper: customHelper, handle: ".moveTargetDeliverables", containment: "#fieldset_deliverables_summary", tolerance: 'pointer&a ...

Align the checkbox input in the center of a flexbox container

I am in the process of creating a side menu with filters, and I need to design a row that includes an input checkbox along with some accompanying text. However, I am facing an issue where the checkbox does not center properly within the flex container when ...

Tips for aligning a Material UI FAB button in the center and keeping it in the center while resizing the window

Is there a way to center a MaterialUI FAB button and keep it centered while resizing the window? The current positioning is not centered, as seen in the screenshot below, and it does not adjust with window size changes. Below is the code for the current F ...

Showcasing Portfolio Work in a User-Friendly Mobile Design

Currently revamping my portfolio website and looking for ways to optimize the display of my personal projects. I have a card-like interface in place that works well on desktop but only shows one project at a time on mobile devices. Seeking solutions to imp ...

I am struggling to remove the div from its normal flow as it stubbornly remains positioned below

Is there a way to add a "contact" card to my website's right-top corner in a sticky position? I currently have a div block in that area which is blocking the contact card. Can anyone suggest a solution for this? https://i.stack.imgur.com/umC7B.jpg &l ...

Tips on aligning multiple elements vertically in a justified manner

I'm struggling to articulate what I need, but I'll give it a shot. I want to vertically align three different elements, each wrapped in their own divs. This is my current code: .service_info { margin-top: 45px; clear: both; ...

Stack 2 cards inside a DIV vertically using Materialize CSS

My form and table are currently positioned side by side in 2 columns: https://i.sstatic.net/croh3.png The layout of this form/table is built using datatable + materialize CSS. I am looking to place the Form and table within a div to ensure that the bott ...

The dropdown menu extends beyond the boundaries of the page

I am attempting to incorporate a dropdown menu in the upper right corner. Here is what I currently have: https://i.sstatic.net/soHgc.png The dropdown menu that appears extends beyond the page boundaries. This is the code I am using: <link rel="st ...

Show an HTML email message on a webpage without disrupting the existing page layout

Looking for a solution to display mail messages on a web page originating from an exchange server with various style elements that might interfere with the existing page layout. Attempting to load these mail messages within a div results in the style elem ...

What are some solutions for adjusting this sidebar for mobile devices?

My mobile version sidebar is not functioning properly on my website. To see the issue, you can visit Zinexium. As I am new to HTML, I don't know how to fix it. Here is a link to the code. Thank you! <!DOCTYPE html> // Code snip ...

Tips for keeping div and input tags selected even after a user clicks

I am working on a quiz script and I am trying to customize it so that when a user clicks on the div or input tags, they remain selected with a different background color. Currently, I have achieved changing the background color of the div when clicked, ...

Issues with Internet Explorer causing headaches with CSS :first-child selector malfunctioning, experiencing strange behavior when utilizing Google Web Fonts

Having some trouble with the layout of a website, particularly in IE. The issue can be observed at . When comparing the page in IE to Firefox, there are noticeable differences. Firstly, the top module on the right side is not displaying correctly in IE. I ...

Do CSS Stylesheets load asynchronously?

Is there a difference in how stylesheets are loaded via the link tag - asynchronously or synchronously? In my design, I have two stylesheets: mura.css and typography.css. They are loaded within the head section of the page, with typography.css being load ...

Display elements on hover of thumbnails using CSS

I'm struggling with the logic of displaying images when hovering over corresponding thumbnails using only CSS. If necessary, I can do it in JavaScript. Here's my latest attempt. <div id='img-container' class='grd12'> ...

On iPhones, the links display as oversized blank containers

Recently, a client who switched from an Android phone to an iPhone reached out to me with a complaint. She mentioned that the links on her website, which I built for her, appear as large empty boxes. You can view her website here. To illustrate the issue ...

Efficient scripting on Visual Studio

In Visual Studio, a helpful feature is the option box on the left side that allows you to collapse code within curly braces { } using a "-" to minimize and a "+" to expand. This keeps the code organized and clean. I'm curious if there's a way to ...

What is the best way to style a select element to achieve this appearance?

https://i.sstatic.net/4yGqf.png Currently working on converting a .psd file to html/css and encountering difficulty with a unique looking select element. Struggling to find a suitable example online for reference. Wondering if this design element is indee ...

Prevent screen from loading without JavaScript using an overlay

Is there a way to darken and lock the page, preventing clicking or scrolling, while displaying white text notifying the user that JavaScript is not enabled? I understand that the recommended approach is to gracefully degrade all elements when JavaScript i ...