Embedding Bootstrap class properties into another class - css

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.

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>

Related

Components next to each other

I have a doubt about using Angular. I need to place the sentences next to each other. These are two different components that I created. I want recipe-list works and recipes-details works and these two sentences should show next to each other
<div class="row">
<div class="col-md-5 ">
<app-recipe-list></app-recipe-list>
</div>
<div class="col-md-7">
<app-recipes-details></app-recipes-details>
</div>
</div>
<p>recipe-list works!</p> <p>recipes-details works!</p>
The problem is, that component here take full width of window. So first, apply class to component directly:
<div class="row">
<app-recipe-list class="col-5"></app-recipe-list>
<app-recipes-details class="col-7"></app-recipes-details>
</div>
But also give them style display: inline-block;. The best way would be to apply :host pseudoclass in component's css (recipe-list.component.css and recipes-details.component.css):
:host {
display: inline-block;
}

overide zurb foundation float right property for last element

I am using Zurb foundation and I am trying to create a css property that will be called and override the property float:right that they give the the last column. I know that they provide the end class to make you able to float a div on the left, but I have a case where I really have to make sure that my css property is the one to be called at the end.
code:
<div class="row">
<div class="my-class small-6 columns">floated on the right by foundation</div>
</div>
.my-class{
float:left;
}
the above code does not work, but it works when I add the important property which is something I want to avoid as well.
I found this question when dealing with dynamic content. In this case, I didn't know how many items I would ultimately have and didn't want to override foundations default behavior. The solution in this case was Foundation's block grid:
http://foundation.zurb.com/docs/components/block_grid.html
As an example, the way I initially tried to implement this was by wrapping each repeating element in a div with that amount of columns it should use like this:
<div class="row">
<div class="columns small-3">content</div>
<div class="columns small-3">content</div>
<div class="columns small-3">content</div>
<div class="columns small-3">content</div>
<div class="columns small-3">content</div> <!-- Floated right. -->
</div>
Instead, the better way to do this is to use Foundation's block grid class, which specifies how many items should be in each row, like this:
<div class="row">
<ul class="small-block-grid-4">
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li> <!-- Right where I expected it! -->
</ul>
</div>
Hopefully that help some fellow Googlers. :-)
In HTML/CSS, you need to make sure that your custom class is more specific than the class you're overriding and that your CSS is loaded after Zurb's CSS.
Zurb has the following class;
[class*="column"] + [class*="column"]:last-child {
float: right; }
Which means attribute class contains "column" and is last in parent. To be able to override this, try adding this to the end of your CSS file that is loaded after Zurb's and change the div to <div class="small-6 columns my-class">
[class*="my-class"]:last-child {
float: left;
}

Responsive CSS improvement issue

I started to write a mobile version in css and I am asking you which is the better way:
In the html to write a div which has a class desktop and another div which has a class mobile. And build everthing from zero. For instance:
<div class="container-fluid bg-1 desktop1">
<div class="container">
<div class="col-xs-5 despre-noi-text first-section">
<h2>asdfsdfdf</h2>
<p class="despre-paragraph">
adsdadd
</p>
</div>
<div class="col-xs-7 despre-noi-img second-section"></div>
</div>
</div>
<div class="container-fluid mobile1">
<div class="container">
<div class ="row mobile-row">
<div class="col-xs-12 mobile1-img"></div>
</div>
<div class ="row mobile-row">
<div class="col-xs-12 mobile1-text">
<h2>asdfsdffdfsfsdf</h2>
<p class="mobile1-despre-paragraph">
asdfdfdfd
</p>
<p class="mobile1-despre-paragraph2">saddfsdfsdfsdfd</p>
</div>
</div>
</div>
</div>
And after that I will check with media queries on the mobile resolution which classes will be hidden and which will be not hidden.
#media only screen and (max-width : 480px) {
.desktop1 { display:none; }
.mobile1 { display:block; }
}
OR to overwrite all of the classes in media queries ?
Definitely use media queries to override the default properties. It's generally a good principle to keep your code DRY and easily maintainable. Having two versions of a block of HTML means unnecessary duplication (meaning more code needs to be loaded to the browser) and more work in future maintenance.
It's not a performance issue to use the queries to override the styles -- CSS is built to cascade. Don't feel bad about taking advantage of it (unless you're cascading with little reason and adding unnecessary specification).
Short answer: Save yourself the stress. Don't duplicate the HTML.

Bootstrap 3 & LESS Semantic Column Ordering

In Bootstrap 3 I am able to do the follow (for semantic markup):
.div1 {
.make-xs-column(12);
}
.div2 {
.make-xs-column(12);
}
Example HTML:
<div class="div1">
<!-- Stuff Goes Here -->
</div>
<div class="div2">
<!-- More Stuff Goes Here -->
</div>
Within inline class definitions I can change the order of div1 and div2 by doing:
<div class="div1 col-xs-push-12">
</div>
<div class="div2 col-xs-pull-12">
</div>
Is it possible to change the column ordering with LESS mixins? Or is this feature not currently available? Would it be good practice to do something like this?:
.div1 {
.make-xs-column(12);
.col-sm-push-12;
}
.div2 {
.make-xs-column(12);
.col-sm-pull-12;
}
Edited: I meant to ask about xs columns rather than sm. Should I do pull and push on sm+ devices and reverse the order of my markup?
It is the same story as with .make-*-column(), i.e. there're .make-*-column-offset .make-*-column-push and .make-*-column-pull mixins.

Showing Div on Hover over img (Bootstrap 3)

Using CSS I am running into trouble getting a div later on the page to show up using the hover command over an img tag. I'm writing the page using Bootstrap 3 - Any idea why this may be a problem? The words in "hovershow" appear at the right spot on the page when they are not originally hiden using CSS which makes me think there's a problem with the command itself.
HTML
<div class="col-md-4 col-sm-4">
<img id="Email_Logo" class="featurette-image img-responsive" src="img/Email_Icon_Send1.jpg" data-src="holder.js/500x500/auto" alt="Generic placeholder image">
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="hovershow"><p>This should show on hover</p></div>
</div>
</div>
CSS
.hovershow{
display:none;
}
#Email_Logo:hover .hovershow{
display: block;
}
That's definitely not how CSS works.
The following CSS implies there is an element .hovershow somewhere within #Email_Logo:
.#Email_Logo:hover .hovershow{
display: block;
}
And well... that's not the case. What you want can either be achieved by some easy Javascripting or a change in your HTML 'tree' and CSS.

Resources