Choosing the :after pseudo element that comes after the p element within the container's previous element

Currently, I have a specific HTML code that is not editable:

<div>
    <input type="text" id="name" />
</div>

To add a label after the input using CSS (since "after" doesn't work on input fields), I do the following:

div:after { 
    content:'label' 
}

Now, my goal is to change the label based only on the input field. How can I target the :after element for this purpose?

input.error + * {
    color: red;
}

Unfortunately, I am restricted to using CSS only and cannot utilize JavaScript for this task!

Answer №1

In my opinion, achieving your goal is unattainable without incorporating an additional html element or leveraging jquery.

Answer №2

At the moment, there is no direct way to apply styles to a parent element using CSS.
One workaround is to utilize JavaScript/jQuery to style the parent element.
$('input').parent();

However, in the near future, a :has() pseudo-class will be introduced, allowing for styling of parent elements like so:
div:has(>input), which will target all divs containing a child <input>.


If you prefer not to rely on JavaScript, the HTML structure needs to be modified.
By making the div a sibling of the input field, you can then use the sibling combinators:
The + symbol selects only the immediate adjacent sibling, while the ~ symbol targets any following sibling.
It's important to mention that targeting previous siblings is not supported.

Take a look at this demonstration:

input ~ div {
  background-color: green;
}

input + div {
  color: white;
}
<div>div not selected</div>
<input type="text" id="name" />
<div>div selected by both sibling combinators</div>
<div>div selected only by the general sibling combinator</div>
<div>selected also only by the general sibling combinator</div>

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

Unable to view indicators and slides are not transitioning in Bootstrap carousel

I am in the process of developing a website with bootstrap, but I am encountering issues with the carousel feature. Despite copying the code directly from their official page, the carousel remains non-functional. The indicators are missing, and the slides ...

Create a form with a mailto link that includes a subject line containing "XXXX" followed by user input

Need help with customizing the email subject along with the user's name from a form input <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc8f939199939299bc8593898e8f958899d29f9391">[email protec ...

Safari does not support SVG fill pattern, unlike Firefox and Chrome

Having an issue with Safari 6.1.5 not displaying a pattern in an SVG rectangle. After simplifying the code, here is the test case: <html> <head> <style> .patterned { fill: url("#myid") none; stroke:blue} ...

Preloading images before loading a div using JavaScript

Can you walk me through implementing object first and then mergeObject in JavaScript? I have an interesting scenario where I need to display the original list followed by a merged list after a short delay. How can I achieve this using JavaScript? Specific ...

If the ID (i.e. document.getElementById) is not found, then keep JavaScript running

I'm currently working on a JavaScript project where I need the script to gracefully handle missing div-ids and continue executing. I've looked into various solutions, but many involve either replacing the missing ID or placing information into an ...

What is the best method to locate and reference a specific string within an HTML document using Python?

My specific need involves two separate html pages. One page consists of hyperlinks while the other contains detailed responses corresponding to those hyperlinks. What I am looking for is, when I click on a hyperlink in the first html page, it should dire ...

Password Reset Function Malfunctioning

Could somebody please assist me in resolving the issue with the reset email? When attempting to reset it, I received the following message: An email with further instructions has been sent. However, I did not receive a reset link. Here is the reset file. ...

Make sure to keep the <div> element nested within the image

I'm working on creating a photo album that displays images in full screen and allows users to navigate using arrows. Check out my progress here: https://jsfiddle.net/q49wzey6/ Here's the HTML code I'm using: <div class="main"> &l ...

Javascript: Dynamically Altering Images and Text

One feature on my website is a translation script. However, I'm struggling to activate it and update the image and text based on the selected language. This is the code snippet I am currently using: <div class="btn-group"> <button type ...

I'm having trouble with my dropdown menu functionality

My dropdown menu was functioning properly until yesterday, and now I am unable to determine what went wrong even after double-checking the code. #apDiv2 { position: fixed; width: 100%; height: auto; float: none; z-index: 6; list-style-type: ...

What is the method to dynamically modify the value of location.href using vanilla javascript?

I have a button that looks like this: <button type="button" class="play-now-button" onclick="location.href='www.yahoo.com'">Play Now</button> However, I want to change the location.href value using vanilla JavaScript. The code below ...

Enhancing jQuery Component while making an Ajax request

When a user clicks a button, I am making an ajax call to send out multiple emails. My goal is to update a "Please wait..." div before and after the call with status updates, as well as report any errors that may occur. However, I have encountered an issue ...

What is the best way to display all values in the class when no selection is made in the drop-down menu?

I need assistance as I am facing an issue where all files are not appearing at the same time. It seems that the problem lies with the year option in my code. The year option requires at least one year to be chosen for it to work properly. I want to impleme ...

Populate a map<object, string> with values from an Angular 6 form

I'm currently setting keys and values into a map from a form, checking for validation if the field is not null for each one. I am seeking a more efficient solution to streamline my code as I have over 10 fields to handle... Below is an excerpt of my ...

Can the z-index property be applied to the cursor?

Is it possible to control the z-index of the cursor using CSS or Javascript? It seems unlikely, but it would be interesting if it were possible. Imagine having buttons on a webpage and wanting to overlay a semi-transparent image on top of them for a cool ...

Node.js has no trouble loading HTML files, however, it seems to encounter issues when trying to

Having a bit of trouble with my JavaScript skills as I try to load my index.html file (seems like it should be the 5th easiest thing to do in JavaScript). Let me get straight to the point; when I manually open my index.html, it loads perfectly WITH the CS ...

HTML5 canvas processing causing web worker to run out of memory

Within the Main thread: The source image array is obtained using the getImageData method. It is represented as a uint8ClampedArray to store the image data. Below is the code executed in a web worker: (This operation generates a high-resolution image, but ...

Troubleshooting issue arising from transferring code from CodePen to a text editor

I've attempted to recreate a Codepen example in my HTML files on my computer, but it's not displaying correctly. While the static layout resembles what appears in Codepen, the background with the moving squares isn't functioning as expected ...

Ensure that the text within the ng-repeat is wrapped in a span element and each

When using ng repeat in span, I encountered a word breaking into a new line. Here is the actual output: https://i.stack.imgur.com/k4c9k.png To style this, I implemented the following CSS: border: 1px solid #ECE9E6; border-radius: 3px; text- ...

Arranging several lists in columns with a customized header title

I am looking to have 7 lists displayed side by side, each with a unique styled header title. I want the lists to be neatly organized under their respective titles, including the bullets. I attempted to use text-indent for this purpose, but it seems that ...