Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you remain with wanting to theme it within one style sheet (and not multiple sheets as cimmanon noted in the comments), and assuming you are using LESS 1.3.2+, then the following code works to reduce the amount of duplication by setting up a loop through the classes that need theme changes. </p> <p>Note that this does not work on Codepen (it is throwing an error <code>uncaught throw #</code>, perhaps because they are running an earlier version of LESS), but you can see it compiling correctly by putting the code into <a href="http://less2css.org/" rel="noreferrer">LESS's compiler</a>.</p> <p><strong>LESS (based off your Codepen code with an added theme for demo)</strong></p> <pre><code>////////////////////////////////////////////////////// // CONSTANTS @lightColour: #fff; @darkColour: #000; @lightBg: #fff; @darkBg: #000; @numberOfThemes: 3; //controls theme loop ////////////////////////////////////////////////////// // MIXINS //Theme Definitions by parametric mixin numbers (1), (2), etc. .themeDefs(1) { @lightColour: #f00; @darkColour: #fff; @lightBg: #f00; @darkBg: #fff; } .themeDefs(2) { //inverse of 1 @lightColour: #fff; @darkColour: #f00; @lightBg: #fff; @darkBg: #f00; } .themeDefs(3) { @lightColour: #cfc; @darkColour: #363; @lightBg: #cfc; @darkBg: #363; } .curvy { -moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; } ////////////////////////////////////////////////////// // GENERAL STYLING * {padding: 0;margin: 0;} html {text-align: center;} h2 {padding: 20px 0;} .box { .curvy; color: @lightColour; background: @darkBg; display:inline-block; width:10%; padding:20px 5%; margin:0 1% 20px 1%; } ////////////////////////////////////////////////////// // THEME BUILDING .buildThemes(@index) when (@index &lt; @numberOfThemes + 1) { .theme-@{index} { .themeDefs(@index); color: @lightColour; background: @darkBg; .box { color: @darkColour; background: @lightBg; } } .buildThemes(@index + 1); } //stop loop .buildThemes(@index) {} //start theme building loop .buildThemes(1); </code></pre> <p><strong>CSS Output (only showing the looped theme css for brevity)</strong></p> <pre><code>.theme-1 { color: #ff0000; background: #ffffff; } .theme-1 .box { color: #ffffff; background: #ff0000; } .theme-2 { color: #ffffff; background: #ff0000; } .theme-2 .box { color: #ff0000; background: #ffffff; } .theme-3 { color: #ccffcc; background: #336633; } .theme-3 .box { color: #336633; background: #ccffcc; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload