Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS class won't override border-style

I have styled all my text fields with a gray border, and for the fields with class="form_field_error", I want the border-color to change to red.

I have tried the following code, but I can't get my class to override the previously defined border? What am I missing?

HTML:

<input type="text" name="title" id="title" class="form_field_error">

CSS:

input[type="text"] {
    display: block;
    height: 15px;
    font-weight: normal;
    color: #777;
    padding: 3px;
    border-top: 1px solid #aaa;
    border-left: 1px solid #aaa;
    border-bottom: 1px solid #ccc;
    border-right: 1px solid #ccc;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
}

.form_field_error {
    border: 1px solid #f00;
}

I created a jsFiddle to illustrate the problem.

like image 876
Andersson Avatar asked Jun 25 '13 17:06

Andersson


People also ask

How do I override a border in CSS?

We can specify the no border property using CSS border: none, border-width : 0, border : 0 properties. Approach 1: We will give border-color, border-style properties to both headings, for showing text with border and no-border. For no border heading, we will use the border-width : 0 which will result in no border.

What does border-style solid none do?

border-style : Specifies the type of line drawn around the element, including: solid : A solid, continuous line. none (default): No line is drawn.

How do you increase CSS specificity without important?

Increasing specificity by duplicating selectorDuplicating id, class, pseudo-class or attribute selectors within a compound selector will increase specificity when overriding very specific selectors over which you have no control. Use this sparingly, if at all.

Which of the following is not a valid border-style property value?

border - style is the answer . it is not the valid border property.


3 Answers

The input[type="text"] css takes precedence over the .form_field_error css.

Change it to input.form_field_error and the border will work.

like image 192
Dmiller89 Avatar answered Oct 24 '22 05:10

Dmiller89


Try this:

.form_field_error {
    border: 1px solid #f00 !important;
}
like image 41
Gimmy Avatar answered Oct 24 '22 06:10

Gimmy


I would recommend using:

input[type="text"].form_field_error {
    border: 1px solid red;
}

The "!important" rule should only be used as a last resort - nuclear option - because it will surpass all other attempts to target an element based on precise and relevant specificity, reducing the control you have and creating potential roadblocks for future developers. Therefore the proper way, and the best way to handle it is to start with the same selector as the original one you are trying to override, then simply add the one thing that distinguishes it from the original. This way the specificity will be precisely what you want.

like image 23
Alan Coughlin Avatar answered Oct 24 '22 06:10

Alan Coughlin