Using CSS to define the pseudo element :after that comes after an input

My HTML code is set and I'm unable to make changes, plus JavaScript isn't an option in this case. That leaves CSS as the only solution. For example, you can see the code here: https://jsfiddle.net/4gKPL/

HTML:

<!-- code for input -->
<div class="form-group complete">
<label>label for text input</label>
<input type="text"/> <span class="error-message">This is an error message</span>

</div>
<!-- code for dropdown -->
<div class="form-group complete">
<label>label for select input</label>
<div class="custom-selectbox custom-selectbox-form">
    <select name="sth" required>
        <option>a</option>
        <option>b</option>
        <option>c</option>
        <option>d</option>
    </select> <span class="selectedValue">&nbsp;</span>
<span class="is-visually-hidden">select to open the list</span>

</div> <span class="error-message">This is an error message</span>

</div>

CSS:

.complete:after {
    content:'OK';
}

I'm looking for a way to add extra content (like 'OK' in the example) specifically for input fields, but not for selects.

The presence of spans after interactive components is not mandatory and they may be absent.

Any suggestions on how to define this selector?

Answer №1

Due to the limitations of using :after with input elements, a somewhat unconventional solution would be to target the element that comes after and use a :before pseudo element to achieve the desired effect.

.complete input[type="text"] + .error-message:before {
    content:'OK';
}

You can view a functional example on jsFiddle here.

UPDATE

The issue arises when the .error-message element is not consistently present, complicating this approach. Using an unprefixed call to :before (+ :before) may lead to the OK message being hidden if the following element is concealed in some way. Additionally, it may inherit styles from the subsequent element. Check out this revised jsFiddle for more details.

This concept is shared for reference purposes, but it seems unlikely to suit your specific needs.

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

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 ...

Horizontal rule located within a table but spanning the entire width

I wrote the following code: <table> {item.awards.map((obj,i) => <tbody> <tr> <td>Name</td> <td>:</td> <td>{obj.title}</td> </tr> ...

Minimizing the gap between icon and label text

I have a React form that I need help with. The issue is that I want to reduce the space between the list icon and the label. Here is the CSS I am using: .form__container { display: flex; flex-wrap: wrap; } .form__container input { color: rgb(115, 0, ...

Alter the arrow to dynamically point towards the location of the click source

I am currently working on creating a popover dialog that should point to the element triggering its appearance. The goal is for the arrow to align with the middle of the button when clicked. While I am familiar with using CSS to create pointing arrows, th ...

Styling Overflow in Coda Slider using CSS

Currently utilizing the Coda Slider for my project. For those unfamiliar with the Coda Slider, it initially hides the overflow-x position until a tab is clicked. This triggers an animation that slides the hidden DIV in either from the right or left side. ...

Adding a button to the right of a text-field input in Vuetify app

I need advice on the best way to use Vuetify in order to display a button to the right of a text-field (attached). I've checked the main site for information but couldn't find anything related to this specific scenario. https://i.sstatic.net/Pp ...

What are the steps to ensure my header spans the full width on both mobile and desktop platforms in an HTML email?

Hello, I am nearly finished with composing this email, but an issue has arisen on mobile where the header is not stretching to full width. My goal is to make the header image fill the entire width of the screen on a mobile device, but I keep encountering o ...

An empty space separating the header and the navigation bar

As a beginner in HTML and CSS, I decided to create a basic website. However, I encountered an issue with the layout - there seems to be a gap between my header image and navigation bar, but I can't figure out what's causing it. HTML <!DOCTYPE ...

What is the best way to choose the parent element when the child element is checked?

I am looking to customize my CheckBox. Below is the code I have written: <div class="check-box-input"> <input type="checkbox"> </div> I also want to add a style to the parent div when the input is checked. This is the code I tried, ...

"Discover a unique online platform showcasing videos and images with a creative

I recently updated my personal website to showcase some of the tools I've developed through short looping videos. Initially, everything was working well with just images, but when I switched to videos, I encountered some unexpected behaviors. Interest ...

List items are not appearing correctly on display

When attempting to design a basic navbar, I encountered an issue where the list items were stacking on top of each other instead of displaying horizontally as intended. The parent container has a position of relative, while the child is set to absolute. ...

What is the best way to expand my container element and add margins to the top and bottom?

Currently, I am utilizing the Material-UI library in combination with ReactJS, but facing some challenges pertaining to flexbox. The objective is to position my Item Container at the center of my div, extending it to occupy the entire available height whi ...

How to style the initial selection in a p-dropdown from PrimeNG using CSS

Struggling with customizing a single option in the p-dropdown element of my primeng app. Not sure how to achieve this, can anyone help? <p-dropdown id="dropdown" [options]="options" optionLabel="name" optionValue="name ...

Initiate fading effect on divs by clicking

I am currently in the process of developing a mobile navigation bar. I have set it up so that when the menu is clicked, it appears above it - all functioning as expected. However, I want to enhance it by adding a feature where each list item fades or slide ...

Creating an Event on ASP.NET Websites

I'm attempting to add a button to my ASP.net website that, when clicked, will trigger a music player to start streaming music on the site. Although I have experience creating buttons and handling events, I am struggling to get my player to play the * ...

Challenge with the Bootstrap 3 grid structure involving nesting

On my .aspx page, I have the code snippet below: .cDiv { background-color: gray; } .tColor { background-color:yellow; } .tColor2 { background-color:blue; } <div class="container"> <div class="row"> <div class="col-sm-9 tColor" ...

Tips for creating a static table header without relying on JQuery and with minimal CSS options

Hey there, I'm currently in need of a solution for fixing a table header in place. I've tried a few strategies, but unfortunately, they haven't worked due to specific functionality limitations. One approach involved copying the header and ...

What is the best way to implement this design using CSS or JavaScript?

I would like to enhance the appearance of my school website during these challenging times of the pandemic by adding some CSS or JavaScript elements. However, I am unsure how to go about it. ...

A guide on adjusting the height of UI elements in Semantic: steps for modifying

One of my current challenges involves using Semantic UI and embedding it within an iFrame tag to display a video. However, I am struggling to adjust the height of the Semantic UI element. <div className="video_box ui embed"> ...

I am attempting to adjust the CSS code so that it does not shrink when clicked on. How can I prevent this from happening?

Here is the element I am attempting to modify. I attempted &:active { flex:7;} but upon releasing the left click button, it reverted back to its original size. I was aiming for something similar to this <h1>Gallery</h1> < ...