2

I'm new to CSS and I will appreciate any help.

I'm trying to style the asterisk * inside the span element of a required element based on the input:valid property.

<input value="someValue" id="firstnameId" required />
<label class="lbStyle" for="FirstName">
    First Name
    <span class="ddff" style="color:red;">*</span>
</label>

I tried to do :

input:valid .ddff {
    color: palegreen;
}

But it's not working.

I want to change the color of the * from red to green if the input is valid or keep it red otherwise.

How can I achieve it using CSS?

Thanks!

2
  • In your span you have an inline css. It overwrite your css rule, so you can remove it or set your color into css like this input:valid .ddff { color: palegreen !important } Commented May 14, 2019 at 7:17
  • Can you change the HTML code? Commented May 14, 2019 at 8:15

5 Answers 5

1

your css is inline, try with !important

input:valid + label .ddff {
      color: palegreen !important;
 }
<input value="someValue" id="firstnameId" required />
   <label class="lbStyle" for="FirstName"> First Name
    <span class="ddff" style="color:red;">*</span>
</label>

Sign up to request clarification or add additional context in comments.

4 Comments

please don't use !important
@xmastertje inline CSS how can overwrite?
The initial red color of the span should not be inline here in the first place, and then no !important is needed either.
Thank you!! I removed the inline style and the !important also and it works great! I can't vote for you @04FS because I newbie and I dont have that option here. Thanks for the tip.
1

You can use :valid and :invalid selector for form elements with limitation such as required.

Try this:

input[required]:invalid + label:after{content:"*";color:red}
input[required]:valid + label:after{content:"*";color:green}
<input placeholder="Placeholder" required />
<label> First Name</label>

4 Comments

this is the best answer, but the input should have the someValue as placeholder text
You can add default value as you want. If add value, input will be :valid at first
It's not ideal to start off with the asterisk showing as valid. Placeholder text is recommended if user input is required
Use placeholder instead
0

it can works because .ddff is not an input child

<input value="someValue" id="firstnameId" required />
   <label class="lbStyle" for="FirstName"> First Name
    <span class="ddff">*</span>
</label>

.ddff{
      color: red;
}
input:valid + label .ddff {
      color: green;
 }

input:invalid +label .ddff {
      color: red;
 }

1 Comment

Thank you very much for your answer. The first comment helped me to solve this issue.
0

Your CSS selector is not correct, you have to use the Adjacent sibling combinator:

input + label .ddff {
    color: red;
}

input:valid + label .ddff {
    color: palegreen;
}
<input value="someValue" id="firstnameId" required />
<label class="lbStyle" for="FirstName">
    First Name
    <span class="ddff">*</span>
</label>

Comments

0

can you please try:

CSS:

input:valid + label span.ddff {
      color: palegreen !important;
 }

Note: You can use an inline CSS. please overwrite your CSS.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.