1

Twitter Bootstrap is ment to easily build on top of it. Now I'm experience a problem which I can not solve. I work with TB v. 2.3.2.

In my own style.less file, I included TB:

@import "../bootstrap/less/bootstrap.less"; 

In the original TB files, forms.less, this LESS can be found:

// INPUT GROUPS
// ------------

// Allow us to put symbols and text within the input field for a cleaner look
.input-append,
.input-prepend {
  display: inline-block;
  margin-bottom: @baseLineHeight / 2;
  vertical-align: middle;
  font-size: 0; // white space collapse hack
  white-space: nowrap; // Prevent span and input from separating

  .add-on {
    display: inline-block;
    width: auto;
    height: @baseLineHeight;
    min-width: 16px;
    padding: 4px 5px;
    font-size: @baseFontSize;
    font-weight: normal;
    line-height: @baseLineHeight;
    text-align: center;
    text-shadow: 0 1px 0 @white;
    background-color: @grayLighter;
    border: 1px solid #ccc;
  }
}

So in my own stylesheet after after the @import of the bootstrap files, I trie to change the background-color for .add-on like this (should become pink instead of @grayLighter you find in the above code):

.input-append,
.input-prepend {
    .add-on {
        color: blue;    
        background-color: pink;
    }
}

Now, the color I have added (blue) is working! This is not overwriting a value, because the .add-on in forms.less did not had any color specified. But as you can see it does have a background-color (@grayLighter), which I want to change! You see that I have specified this to be pink, but it doesn't work. I don't understand this. Can somebody help me out?

I use these Less further down in my stylesheet like this:

div {
    .input-prepend;
    .form-search .input-prepend;

    label {
        .input-prepend > .add-on;
    }
 }

As you can see in below image, the pink background color is not working.

enter image description here

I checked how the output from the LESS in CSS looks, and have written my questions in there as well. The further I'm digging in to this the more I don't understand it, a codepen example DOES SHOW PINK as background, how it should be in my opinion, see working example here: http://codepen.io/willemsiebe/pen/kvmic.

.input-append .add-on,
.input-prepend .add-on {
  display: inline-block;
  width: auto;
  height: 24px;
  min-width: 16px;
  padding: 4px 5px;
  font-size: 14px;
  font-weight: normal;
  line-height: 24px;
  text-align: center;
  text-shadow: 0 1px 0 #fff;
  background-color: #eee;     // FIRST MENTION BG COLOR IN FORMS.LESS
  border: 1px solid #ccc;
}

.input-append .add-on,
.input-prepend .add-on {
  color: blue;
  background-color: pink;     // SECOND MENTION BG COLOR IN MY OWN LESS FILE
}

// THIS IS HOW I REFERRED IT AS YOU CAN SEE IN MY TOPIC! BUT WHY IS BG COLOR PINK SHOWING UP BEFORE BG COLOR OF FORMS.LESS?

#woocommerce_product_search-3 #searchform div label {
  color: blue;
  background-color: pink;   // WHY IS THIS SHOWING UP FIRST?
  display: inline-block;
  width: auto;
  height: 24px;
  min-width: 16px;
  padding: 4px 5px;
  font-size: 14px;
  font-weight: normal;
  line-height: 24px;
  text-align: center;
  text-shadow: 0 1px 0 #fff;
  background-color: #eee;       // WHY IS THIS SHOWING UP LAST?
  border: 1px solid #ccc;
  vertical-align: top;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  margin-right: -1px;
  margin-bottom: 0;
}

20-7-2014: Ok, now I'm really confused! Instead of overwriting it in my own style.less directly, I put the same code in a different less file and imported it with @import, and now its working... but the output in CSS is exactly the same, except from the fact that the background color PINK is now mentioned last!

.input-append .add-on,
.input-prepend .add-on {
  display: inline-block;
  width: auto;
  height: 24px;
  min-width: 16px;
  padding: 4px 5px;
  font-size: 14px;
  font-weight: normal;
  line-height: 24px;
  text-align: center;
  text-shadow: 0 1px 0 #fff;
  background-color: #eee;
  border: 1px solid #ccc;
}

.input-append .add-on,
.input-prepend .add-on {
  color: blue;
  background-color: pink;
}

#woocommerce_product_search-3 #searchform div label {
  display: inline-block;
  width: auto;
  height: 24px;
  min-width: 16px;
  padding: 4px 5px;
  font-size: 14px;
  font-weight: normal;
  line-height: 24px;
  text-align: center;
  text-shadow: 0 1px 0 #fff;
  background-color: #eee;
  border: 1px solid #ccc;
  vertical-align: top;
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
  margin-right: -1px;
  color: blue;
  background-color: pink;     // NOW IT'S MENTIONED LAST!
  margin-bottom: 0;
}
user2312234
  • 265
  • 1
  • 2
  • 13
  • Check in your markup that the `add-on` class is a child of either `input-append` or `input-prepend`. If it isn't the styles will not be applied. – Colin Bacon Jul 11 '14 at 15:59
  • @ColinBacon, I don't know exactly in which file you want me to check this. I don't use the classes in my HTML, I apply them also with LESS. I described it now better in my question and even added a image with HTML and CSS visible. Can you please review it and get back to me again? Thanks. – user2312234 Jul 12 '14 at 17:26
  • You need to make sure that that you have your ordering correct. First bootstrap import and then below this your styles for `add-on`. Looking at the image, your pink styles are getting compiled before the bootstrap ones. – Colin Bacon Jul 14 '14 at 08:44
  • @ColinBacon, yes, I agree that it seems to resolve (looking at the image) before the bootstrap ones. The question however remains how that is possible. I start the file using the import for the bootstrap files, and after that I do my own css/less, I described that also in my question. Hope to hear from you. – user2312234 Jul 15 '14 at 09:43

1 Answers1

0

Twitter Bootstrap is ment to easily build on top of it.

That's only true for mixins, see also: Bootstrap 3 with LESS: how to handle bootstrap's nested rules?

In stead on mixins (which only work for simple classes) you could try to use the :extend() psuedo class, see: http://lesscss.org/features/#extend-feature-combining-styles-a-more-advanced-mixin

To style your HTML:

<form id="searchform">
<label>zoeken:</label>
<input type="text">
<input type="submit">
</form>

Try the following Less code:

@import "bootstrap.less";

#searchform {
&:extend(.input-prepend, input-append all);
margin-top:5px;
label {
&:extend(.input-prepend .add-on all);
color: blue;    
background-color: pink;
}
input[type="text"] {
&:extend(.input-prepend input all, .input-append input all);
border-radius: 0;
}
input[type="submit"] {
&:extend(button all,.btn all,.input-append .add-on all);
height:30px;
}    
}
Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
  • I have read LESS docs now about mixins, learned some good things. But to keep it as simple as possible in relations to bootstrap v 2.3.2, do I understand you correctly that I actually only should re-use the styles found in mixins.less in my own LESS file (because that is what mixins are suppose to do). When I want to use all the other styles provided by Bootstrap, Boostrap wants us to mention this in our HTML? – user2312234 Jul 17 '14 at 16:14
  • Yes i think your conclusion is right. Where possible you should bootstrap structures, which means the HTML and CSS as defined in the docs. – Bass Jobsen Jul 17 '14 at 21:44
  • Hi, I was afraid of that ;-). But what makes a mixin a 'mixin'. In my own LESS file I am re-using a lot of classes that are found in the other LESS files. For example, working with a CMS means that I don't always can add TB to the HTML, and thats when I started reusing all kinds of classes of TB in my own LESS. For example I use a lot: .btn, .text-center, .ul.inline and also the alerts classes. Concerning our previous conclusion this would be wrong, but is it? It seems to me that actually everything written in LESS is a mixin.... – user2312234 Jul 18 '14 at 08:52
  • mixins end with (), but indeed you can mixin every simple class. The problem here is that Bootstrap uses relations (child and sibling) between classes. inside the forms.less file you will find selectors such as .form-inline {.form-control {} } which will compile into the following CSS code: .form-inline .form-control {}. You can't mixin the preceding selector, but you can extend it. – Bass Jobsen Jul 18 '14 at 11:07
  • I see that not all mixins end with (), and if it does it will be not outputted in the CSS, so we can not use it in the HTML, but we can still mix it in our LESS file into other classes/id's. But back to the question and your example > in my question you see in forms.less: .input-prepend {.add-on{} }, following the explanations of official LESS docs this is called namespaces, see http://lesscss.org/features/#mixins-feature-namespaces. You see that I have included it in my own less to the div and the label, actually following the rules of LESS explenation. See other comment.... – user2312234 Jul 19 '14 at 18:36
  • ... but why, when I specify a new background color for .input-prepend {.add-on{} } AFTER all the bootstrap docs are included, still the background color of forms.less is being used? In the image you see that my pink background color is loaded before the background-color of forms.less, but again those bootstrap files are included at the top of my own less file. I should be able to overwrite these values right? – user2312234 Jul 19 '14 at 18:39
  • The `.input-prepend {.add-on{} }` should work for `
    $
    ` for the forms label in your example, you can use: `#searchform { .input-append; .input-prepend; label { .add-on; } }` also notice that this label also has a `screen-reader-text` class in your code
    – Bass Jobsen Jul 19 '14 at 20:24
  • Hi, the screen-reader-text class is added by the CMS, but I disabled all there CSS and besides that I want to show the label for visual users as well. To debug further I have looked at the output (and the order of it) from my LESS file, looking at the final CSS file. I have added this to my question at the bottom. I have added my questions in it, can you have a look at it? – user2312234 Jul 19 '14 at 21:48
  • Hi, I have also made a CodePen where you see that it SHOULD BE pink... don't understand why it's working there and not on my site. Link: http://codepen.io/willemsiebe/pen/kvmic. – user2312234 Jul 19 '14 at 22:03
  • Well the codepen works and when i compile your code. Maybe your Less compiler use a different version of Less? Maybe you will also find the following question useful http://stackoverflow.com/questions/18828924/less-mixing-duplicate-properties – Bass Jobsen Jul 20 '14 at 18:33
  • Thanks I will read that. But I made a discovery, which again I don't understand. I placed the exact code I used in my own style.less to overwrite the values of TB forms.less now in a DIFFERENT less file, and included this with @import in my style.less file on the exact same place. Now it does work... I have added this to the question. I really don't understand why it works with import, and why it doesn't work when overwriting it directly in my own style.less. – user2312234 Jul 20 '14 at 18:50
  • Bass, I'm using the WP LESS plugin, I found your topic here: http://wordpress.org/support/topic/twitters-bootstrap-300 so you have knowledge about this plugin. Is this plugin maybe causing the problem? – user2312234 Jul 20 '14 at 19:14
  • yes, your plugin makes the difference. I think you use https://wordpress.org/plugins/wp-less/ which relies in http://leafo.net/lessphp/, please also see the demo on that page, to see how your less code will be compiled. Also notice that compiling bootstrap2 with the latest version on Less got some issues, see http://stackoverflow.com/questions/19553367/less-unrecognized-input-error-when-using-bootstrap/19553368. – Bass Jobsen Jul 20 '14 at 21:35
  • For my JBST project (https://github.com/bassjobsen/jbst) i use https://github.com/oyejorge/less.phphttps://github.com/oyejorge/less.php which supports a newer version of Less – Bass Jobsen Jul 20 '14 at 21:44
  • Hi Bass, thanks for all your comments and helping me out. I'm going to play with the demo page and see what happens there. The issue you refer to about compiling problem is about v. 2.2.2 of TB, the don't use ```~``` anymore in version 2.3.2. However, I do use version 1.6 of the WP LESS plugin now, and on their github page you can read this: ```Warning: this release has some breaking changes. If you the old selector expression syntax (eg. Bootstrap 2.3) then it’s recommended to use 0.3.9.```. But I have never encountered any issues actually... Link: https://github.com/oncletom/wp-less. – user2312234 Jul 21 '14 at 06:59