CSS: Can pseudo elements be combined with the plus sign?

When looking at the markup below:

<div>
    <p>hello world</p>
</div>
<div>
    <h4>hello world</h4>
</div>

Is it possible to achieve the following styling using CSS:

div:after {
   content: "";
   display: block;
   border-bottom: 2px solid red;
}
p + div:after {
   content: "";
   display: block;
   border-bottom: 2px solid blue;
}

This would mean that all :after pseudo elements immediately following a <p> have a blue border, while all others have a red border.

The approach mentioned in this example link doesn't seem to work as expected. This is because the + sign is applying to the 'div' selector rather than the 'div:after' selector as a whole. Is there an alternative way to target these elements in CSS without introducing a new specific class or manipulating the DOM?

Answer №1

In essence, echoing Michael_B's statement:

You cannot directly select a pseudo-element. A pseudo-element is inserted into a selector after matching an element.

The term "target" may be ambiguous, but the second sentence hits the nail on the head. Combinators only function with elements since selectors target elements and not pseudo-elements. What you are truly attempting to accomplish in selector terms is to style the ::after pseudo-element of a div where the last child is a p element (wherein the ::after box follows the p box in the formatting tree):

<div>
    <p>hello world</p>
    div::after <!-- Blue border -->
</div>
<div>
    <h4>hello world</h4>
    div::after <!-- Red border -->
</div>

However, this approach is not viable due to the absence of a parent selector.

An option that might work would involve something like

div:has(> p:last-child)::after
from Selectors 4, assuming :has() becomes part of CSS. Another workaround would be determining which div elements contain a p as their final child and assigning them a unique class name.

For additional insights, refer to:

  • Can I target a :before or :after pseudo-element with a sibling combinator?
  • Is there a CSS parent selector?

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

Header spanning the entire width with varying ends

Hello everyone, I'm currently working on a website and struggling to create a header similar to the image shown above. I want the header to span the full width of the page. Can anyone provide guidance on achieving this using HTML5/CSS3 without relyin ...

How can I add a loading page to my HTML/CSS website?

Currently, I am delving into the realm of coding and have encountered an issue while trying to integrate a loading page into my HTML. Despite referencing the code from this source, it appears that the loading page is not functioning as intended. Any assist ...

What is the best way to completely stretch 2 embedded div elements?

Is there a way to stretch two <div> elements to be 50% wide? Here is a jsFiddle with the solution. HTML <div class="container"> <div class="left">left</div> <div class="right">right</div> </div> CSS di ...

issues with alignment between bootstrap div and canvas

I am currently working on a project with a canvas inside a div using three.js. My goal is to display a purple bar for user settings on the right side of the three.js window. However, I am encountering an issue where Bootstrap is not recognizing that I want ...

"Implementing a seamless transition effect on two slideshows using jQuery's fadeslideshow

I currently have a homepage featuring a fadeslideshow with 5 slides. The fadeslideshow plugin being used can be found at http://plugins.jquery.com/project/fadeslideshow. On the homepage, there is also a menu bar with various menu items. When a user clicks ...

Looking for an iframe that can adapt to varying content sizes and scale seamlessly with different screen dimensions?

I am new to advanced coding and currently working on my first responsive Wordpress site. I have a "Product Search" database/site that I'm trying to integrate into my website using an iFrame. I want the integration to look seamless without scroll bars ...

Incorporating fresh data from a node.js backend to refresh a webpage section

I'm currently working on creating an app that can retrieve the song a user is currently listening to using a web scraper. While I've managed to direct users to the page and display the current song title, they must manually refresh the entire pag ...

Is there a way to determine if content is present within a given div element?

I have created a sample code where clicking on the "Next" button displays the next content of the page. However, if you click one more time on it when there is no content left, an empty page is shown. How can I determine whether content is available insi ...

Utilizing the retina pixel ratio media query in Internet Explorer 8

I'm currently working on a project to develop a responsive website using SASS/Compass, and I am incorporating retina icons by utilizing generated sprites. In my .scss file, I have created a mixin for including these icons. Here is my mixin for retina ...

HTML button failing to connect with CSS stylesheet

I am currently teaching myself CSS and using W3schools for guidance. Recently, I added a button to my website that redirects users to another page upon clicking. Everything functions correctly, but now I want to style the button using CSS. However, despit ...

The bootstrap table did not meet my expectations as I had hoped

I am currently using Bootstrap to create a basic two-column table similar to the one on the Bootstrap website here: http://getbootstrap.com/css/#tables. To achieve this, I have implemented a javascript function to display the table as shown below: $(&bso ...

Utilizing Bootstrap to Showcase a Form

I am facing some confusion. In my MVC5 project, I am trying to display a form with 4 input fields. The display looks perfect without using the @using (HTML.BeginForm(...) statement. @using(Html.BeginForm("Create", "HR", FormMethod.Post)) { When the @ ...

css content exceeds div boundaries

Hello, I am experiencing an issue with my webpage located at . When I zoom out, the content overflows from its containing div. Setting the height to auto or 100% works fine, but the left column ends up being smaller than the right one, and using clear do ...

How to position text in the center of an image and ensure it is responsive?

I am currently in the process of trying to center the text "WHAT" over the image in col-2. <div class="row"> <div class="col-2"> <div class="stackParent"> <img class="stack-Img" ...

Achieve equal distribution of divs with a specified size

After searching through various questions, none of the answers provided a solution that worked for my specific situation. Within my project, I have a series of divs, each with a fixed width of 210px. The container holding these divs is able to resize bas ...

Angular 10: A guide to dynamically highlighting navbar elements based on scrolling position

I am currently working on a single-page application using Angular 10. I have a simple page layout and I want to implement a feature that will highlight the navbar based on the scroll position. How can I achieve this functionality in a single-page applicati ...

Stretchable navigation cell

I am working on a navigation bar called "nav" that consists of a "ul" and "li". I have specified the display property for the "ul" as table and for the "li" as table-cell. Setting the "ul" to take up the full width of the wrapper ensures proper alignment. ...

Tips for designing seamless pagination on a mobile-friendly website with responsive design

My goal is to make pagination easily accessible on mobile devices for users of my website, similar to UIScrollView with paging enabled in iOS. However, it's important that the website remains functional and visually appealing on desktops as well. I h ...

It is important for the button size to remain consistent, regardless of any increase in text content

My goal was to keep the button size fixed, even as the text on it grows. This is the current code I am using: .button { border: none; color: white; padding: 10px 50px; text-align: center; text-decoration: none; display: inline-block; font ...

Region Covered by Mouse Over Does Not Extend Across Whole Div

On my website, there is an arrow located on the middle right side that, when hovered over with the mouse, reveals a sidebar. Clicking on each icon in the sidebar further extends it to reveal more content. However, the issue I am facing is that the sidebar ...