How can I use Bootstrap styles in a LESS file?

Ian Campbell


I am trying to import bootstrap.css into a .less file, so that I can conditionally apply Bootstrap styles with media-queries.

Here is my LESS code:

// Visual Studio is saying "Couldn't locate" the file for both, yet they are there:
@import url("bootstrap.min.css");
@import url("bootstrap-theme.min.css");

// ...can't find the source of the "@screen" at-rules:
@extra-small: ~"screen and (max-width: @screen-xs-max)";
@small:       ~"screen and (min-width: @screen-sm-min) and (max-width: @screen-sm-max)";
@medium:      ~"screen and (min-width: @screen-md-min) and (max-width: @screen-md-max)";
@large:       ~"screen and (min-width: @screen-lg-min)";

footer {
    text-align: center;

    // ...doesn't like the comma "or" syntax here, so "or" is used instead:
    @media @extra-small or @small {
        #linkedin-flair {
            .hide // compile error
        }
        #stackoverflow-flair {
            .hide // compile error
        }
    }

    @media @medium {
        #linkedin-flair {
            .col-sm-3 // compile error
        }
        #copyright-text {
            .col-sm-6 // compile error
        }
        #stackoverflow-flair {
            .col-sm-3 // compile error
        }
    }

    @media @large {
        #linkedin-flair {
            .col-sm-4 // compile error
        }
        #copyright-text {
            .col-sm-4 // compile error
        }
        #stackoverflow-flair {
            .col-sm-4 // compile error
        }
    }
}


...the LESS is having trouble with the @import and @screen at-rules, and with referencing any Bootstrap classes such as .hide.

I am using Visual Studio 2013, Web Essentials 2013, and Bootstrap 3.

Bass Jobsen

1) You should prefer to download Bootstrap's LESS files and import them with @import "bootstrap.less". If you can't follow @seven-phases-max 's links and use @import (less) bootstrap.min.css

2) "// ...can't find the source of the "@screen" at-rules:" these rules are not defined in Bootstrap's LESS files. The code from the docs illustrate the boundaries of the grid defined by Bootstrap. It will show you what happens if you change the variables mentioned.

For example take a look to grid.less which defines:

@media (min-width: @screen-sm) { width: @container-sm; }

Also read: http://blog.scur.pl/2012/06/variable-media-queries-less-css/ and LESS variables in @media queries i don't think you solution won't work cause:

@screen-lg: 992px;
@large: ~"screen and (min-width: @screen-lg)";
@media @large{ color: red; }

compiles to:

@media screen and (min-width: @screen-lg) {
  color: red;
}

NB @screen-lg not set.

3) " .hide // compile error" there is no mixins called hide You could use mixins from responsive-utilities.less (see also: http://getbootstrap.com/css/#responsive-utilities)

In your case you will get something like:

//example DO NOT compile to useful CSS
@import (reference) "bootstrap.less";
#linkedin-flair
{
&:extend(.hidden-xs);
&:extend(.hidden-sm);
}

4) " .col-sm-4 // compile error" .col-sm-4 is dynamical generate (grid.less) you can't use it as a mixin.

use:

#stackoverflow-flair {
 .make-sm-column(4);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I combine my LESS file with the default bootstrap less file

From Dev

How can I avoid breaking Bootstrap columns into rows when I use less than 12 cols?

From Dev

how to use less with bootstrap?

From Dev

How can I use man without less?

From Dev

How can I use LESS in jsfiddle?

From Dev

gulp-cache: How can I use a file cache for LESS builds

From Dev

Can I use css-classes as a mixin in a less-file?

From Dev

How can I use a declaration from an existing rule set in LESS?

From Dev

How can I use a declaration from an existing rule set in LESS?

From Dev

How can I ALIAS --> "less" the latest file in a directory?

From Dev

How can I get Webstorm to recognize my LESS file?

From Dev

How can I ALIAS --> "less" the latest file in a directory?

From Dev

How can I save the current contents of less to a file?

From Dev

How can I use bootstrap with wordpress & woocommerce?

From Dev

How to update Bootstrap (and use LESS) in a Meteor app

From Dev

How i can change background of file input in bootstrap css file?

From Dev

Can I import a LESS file into SASS?

From Java

How can I use a local file on container?

From Dev

How can I use File::ChangeNotify on Windows?

From Dev

How can I use header in css file

From Dev

How can I use header in css file

From Dev

How can I change Bootstrap table-striped in a less specific way?

From Dev

How can I change Bootstrap table-striped in a less specific way?

From Dev

How can i assign two styles to a placemark

From Dev

How can i assign two styles to a placemark

From Dev

How can I replace inline styles in css

From Dev

JavaScript: cssText property, how can I get the absolute path of styles which use external resources in FireFox?

From Dev

How can I use Roboto font via XML and styles tag only in android

From Dev

how to use less mixins file in my code?

Related Related

  1. 1

    How can I combine my LESS file with the default bootstrap less file

  2. 2

    How can I avoid breaking Bootstrap columns into rows when I use less than 12 cols?

  3. 3

    how to use less with bootstrap?

  4. 4

    How can I use man without less?

  5. 5

    How can I use LESS in jsfiddle?

  6. 6

    gulp-cache: How can I use a file cache for LESS builds

  7. 7

    Can I use css-classes as a mixin in a less-file?

  8. 8

    How can I use a declaration from an existing rule set in LESS?

  9. 9

    How can I use a declaration from an existing rule set in LESS?

  10. 10

    How can I ALIAS --> "less" the latest file in a directory?

  11. 11

    How can I get Webstorm to recognize my LESS file?

  12. 12

    How can I ALIAS --> "less" the latest file in a directory?

  13. 13

    How can I save the current contents of less to a file?

  14. 14

    How can I use bootstrap with wordpress & woocommerce?

  15. 15

    How to update Bootstrap (and use LESS) in a Meteor app

  16. 16

    How i can change background of file input in bootstrap css file?

  17. 17

    Can I import a LESS file into SASS?

  18. 18

    How can I use a local file on container?

  19. 19

    How can I use File::ChangeNotify on Windows?

  20. 20

    How can I use header in css file

  21. 21

    How can I use header in css file

  22. 22

    How can I change Bootstrap table-striped in a less specific way?

  23. 23

    How can I change Bootstrap table-striped in a less specific way?

  24. 24

    How can i assign two styles to a placemark

  25. 25

    How can i assign two styles to a placemark

  26. 26

    How can I replace inline styles in css

  27. 27

    JavaScript: cssText property, how can I get the absolute path of styles which use external resources in FireFox?

  28. 28

    How can I use Roboto font via XML and styles tag only in android

  29. 29

    how to use less mixins file in my code?

HotTag

Archive