4

I'm using Bootstrap to set up my site layout and have something like:

  <div class="row-fluid">
    <div class="span3">    
    </div>
    <div class="span9">
    </div>
  </div>

That works fine. However, I'm slightly bothered by the fact that this is defining the presentation in the markup and to make it easier to make future changes, I'd like to add another layer of indirection. I'd like to add my own class that defines the semantics and then include the Bootstrap class that defines the layout presentation. For example:

  <div class="main-block">
    <div class="side-bar">    
    </div>
    <div class="content-area">
    </div>
  </div>

and my corresponding less file...

@import "twitter/bootstrap";
.main-block { .row-fluid }
.side-bar { .span3 }
.content-area { .span9 }

The less documentation states that you can "embed all the properties of a class into another class by simply including the class name as one of its properties" so it looks like it should work, but I am getting an error:

.row-fluid is undefined

Is there something that I am missing? Or is there a better way to go about this? This is in a rails 3.2 project using the less-rails-bootstrap gem if that makes any difference.

g .
  • 8,110
  • 5
  • 38
  • 48
  • I just found this question (http://stackoverflow.com/questions/9090238/tweaking-bootstrap-2-0-for-semantic-markup) which is similar, but focussed on the grid system. Are only some classes able to be embedded in other classes? – g . Mar 30 '12 at 15:19

1 Answers1

2

It's a little bit more complicated. What you're referring to is essentially what "mixins" are all about. First, let's resolve the error. From the little I see my bet is that you are trying to compile a "custom".less file and that you did not @import the variables.less and mixins.less files at the top of the page. Is that correct? If so, see if that gets the code to compile as expected.

However, once you get the code to compile you'll see that you have a new problem. In this particular case, by attempting to use a name other than .span you will lose any styling that is applied by the attribute selectors in the grid mixin, namely [class*="span"]. Compiled, it looks like this:

[class*="span"] { float: left; margin-left: 20px; }
.row-fluid [class*="span"] {}
.row-fluid [class*="span"]:first-child { margin-left: 0; }

So in this case the attribute selectors apply their styles to any class that starts with "span".

Here are a couple of options that might be better for you:

1) Adding the word "span" before your custom class names should work

<div class="row main-block">
  <div class="span-side-bar">
  </div>
  <div class="span-content-area">
  </div>
</div>

2) And using multiple classes will work, but only if you don't apply any styling to the custom classes that would negate any styles in the native grid classes:

<div class="row main-block">
  <div class="span3 side-bar">
  </div>
  <div class="span9 content-area">
  </div>
</div>

3) My recommendation is to live with the little bit of extra markup required to maintain the default Bootstrap grid system. Renaming sounds great now, but if you have any desire to merge in future updates, the one mixin I'd leave alone is the grid.

  <div class="row">
    <div class="span3">
      <div class="side-bar">
      </div>      
    </div>
    <div class="span9">
      <div class="content-area">
      </div>      
    </div>
  </div>
jonschlinkert
  • 10,872
  • 4
  • 43
  • 50