How to overwrite styling in Twitter Bootstrap
All these tips will work, but a simpler way might be to include your stylesheet after the Bootstrap styles.
If you include your css (site-specific.css
) after Bootstrap's (bootstrap.css
), you can override rules by redefining them.
For example, if this is how you include CSS in your <head>
<link rel="stylesheet" href="css/bootstrap.css" />
<link rel="stylesheet" href="css/site-specific.css" />
You can simply move the sidebar to the right by writing (in your site-specific.css
file):
.sidebar {
float: right;
}
Forgive the lack of HAML and SASS, I do not know them well enough to write tutorials in them.
The answer to this is CSS Specificity. You need to be more "specific" in your CSS so that it can override bootstrap css properties.
For example you have a sample code for a bootstrap menu here:
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div id="home-menu-container" class="collapse navbar-collapse">
<ul id="home-menu" class="nav navbar-nav">
<li><a class="navbar-brand" href="#"><img src="images/xd_logo.png" /></a></li>
<li><a href="#intro">Home</a></li>
<li><a href="#about">About Us</a></li>
<li><a href="#services">What We Do</a></li>
<li><a href="#process">Our Process</a><br /></li>
<li><a href="#portfolio">Portfolio</a></li>
<li><a href="#contact">Contact Us</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
Here, you need to remember the hierarchy of the specificity. It goes like this:
- Give an element with an id mentioned 100 points
- Give an element with a class mentioned 10 points
- Give a simple element a single 1 point
So, for the above if your css has something like this:
.navbar ul li a { color: red; } /* 10(.navbar) + 1(ul) + 1(li) + 1(a) = 13 points */
.navbar a { color: green; } /* 10(.navbar) + 1(a) = 11 points */
So, even if you have defined the .navbar a
after .navbar ul li a
it is still going to override with a red colour, instead of a green since the specificity is more (13 points).
So, basically all you need to do is calculate the points for the element you are wanting to change the css for, via inspect element on your browser. Here, bootstrap has specified its css for the element as
.navbar-inverse .navbar-nav>li>a { /* Total = 22 points */
color: #999;
}
So, even if your css is loading is being loaded after bootstrap.css which has the following line:
.navbar-nav li a {
color: red;
}
it's still going to be rendered as #999. In order to solve this, bootstrap has 22 points (calculate it yourself). So all we need is something more than that. Thus, I have added custom IDs to the elements i.e. home-menu-container and home-menu. Now the following css will work:
#home-menu-container #home-menu li a { color: red; } /* 100 + 100 + 1 + 1 = 202 points :) */
Done.
You can refer to this MDN link.