2

Here is my HTML:

<input type='submit' class='clsname'>

This CSS makes a blue border for that input: Demo

input{
  border: 1px solid red;
}
.clsname{
  border: 1px solid blue;
}

But this makes a red border for that input: Demo

input[type='submit']{
  border: 1px solid red;
}
.clsname{
  border: 1px solid blue;
}

I don't know why exactly, But I know it is related to the priority of selectors. I think input[type='submit'] has a bigger priority than classname, but input doesn't have a bigger priority than classname.

Now I have a selector like this: input[type='submit'] and I need to give a bigger priority to class name. How can I do that? I mean something like this:

input[type='submit'] { }
.clsname { /* bigger priority here */ }

Note: I can do that by !important, But I want to know is there any approach else? Because using !important is not recommended.

1
  • 3
    Use input[type='submit'].clsname, .clsname. Fiddle Commented Apr 22, 2016 at 18:56

2 Answers 2

2

It's specificity. Read more here on W3.

Use input[type='submit'].clsname, .clsname selector.

Here's the fiddle.

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

1 Comment

Thx ... As I said in the comments, it's exactly what I needed. +1
1

You can overqualify your selector intentionaly to give it higher priority.

input[type="submit"].clsname

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.