Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I overwrite the form-control class in Bootstrap 3 with a custom class?

I am trying to override the width in the form-control class of Bootstrap 3.

Snippet:

<div class="col-xs-2">
         <label for="input-lastname" class="">Last Name</label>
         <input type="text" name="lastname" class="form-control my-form-inline" id="input-lastname" value="<?php  echo isset($_GET['lastname']) ? $_GET['lastname'] : 'empty';  ?>">
</div>...

I have this:

.form-control {
    min-width: 0;
    width: 25px;
    color:red;
}

width never works but the color:red does. Using the developer tools in Chrome, I see where .form-control .form-inline have width:auto. As soon as I uncheck width in those classes, I get my custom class.

I have my custom css as the last file loaded, so the order should be right.

I have tried:

.form-control, .my-form-inline {
    min-width: 0;
    width: 25px;
    color:red;
}

.form-control {
    min-width: 0;
    width: 25px;
    color:red;
}

.form-control, .form-inline, .my-form-inline{
    min-width: 0;
    width: 25px;
    color:red;
}

I seem to be able to override everything but the width. My form's class is form-inline.

I have tried putting the form controls in a div with col-md-2 (and xs), but it only seems to affect the label, not the input. I can never override the width:auto unless I do it inline on the control in the HTML. Removing the form-control class also allows me, as expected, to get my custom class.

EDIT: For whatever reason, the forms.less mixin is winning over the style I load last.

EDIT: The problem has to do with my specificity vs. Bootstrap's. I have this inline in the header:

@media (min-width: 768px) {

    .form-inline .form-control {
      display: inline-block;
      width: auto;
      vertical-align: middle;
    }

    .my-form-inline
    {
        min-width: 0;
        width: 25px;
        color:red;
    }
}

But the width is still overridden by some specificity I cannot find.

like image 353
johnny Avatar asked Jun 08 '15 20:06

johnny


People also ask

How do I override a class in Bootstrap?

There are two main ways you can override Bootstrap CSS: Override using a higher specificity selector and properties via CSS. Using Bootstrap Sass variables (recommended)

How do I override a CSS form?

To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.

How do I customize Bootstrap form?

To create a custom checkbox, wrap a container element, like <div>, with a class of .custom-control and .custom-checkbox around the checkbox. Then add the .custom-control-input to the input with type="checkbox".


1 Answers

Try to add !important after width property value as:

.form-control {
    min-width: 0;
    width: 25px !important;
    color:red;
}
like image 51
Kashif Ullah Avatar answered Nov 14 '22 00:11

Kashif Ullah