Getting started with Bootstrap
An overview of Dice style guidelines. This includes information on Bootstrap, documentation on how to download and use it, basic templates, examples and more.
An overview of Dice style guidelines. This includes information on Bootstrap, documentation on how to download and use it, basic templates, examples and more.
Bootstrap has a few easy ways to quickly get started, each one appealing to a different skill level and use case. Read through to see what suits your particular needs.
The fastest way to get Bootstrap is to download the precompiled and minified versions of their CSS, JavaScript, and fonts. No documentation or original source code files are included.
Download precompiled Bootstrap
Within the download you'll find the following directories and files, logically grouping common resources and providing both compiled and minified variations.
Once downloaded, unzip the compressed folder to see the structure of (the compiled) Bootstrap. You'll see something like this:
bootstrap/
├── css/
│ ├── bootstrap.css
│ ├── bootstrap.min.css
│ ├── bootstrap-theme.css
│ └── bootstrap-theme.min.css
├── js/
│ ├── bootstrap.js
│ └── bootstrap.min.js
└── fonts/
├── glyphicons-halflings-regular.eot
├── glyphicons-halflings-regular.svg
├── glyphicons-halflings-regular.ttf
└── glyphicons-halflings-regular.woff
This is the most basic form of Bootstrap: precompiled files for quick drop-in usage in nearly any web project. Bootstrap provides compiled CSS and JS (bootstrap.*
), as well as compiled and minified CSS and JS (bootstrap.min.*
). Fonts from Glyphicons are included, as is the optional Bootstrap theme.
Please note that all JavaScript plugins require jQuery to be included, as shown in the starter template. Consult the Bootstrap bower.json
to see which versions of jQuery are supported.
Start with this basic HTML template, or modify these examples. Feel free to customize the templates and examples, adapting them to suit your needs.
Copy the HTML below to begin working with a minimal Bootstrap document.
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="http://assets.dice.com/assets/stylesheets/DiceV5.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>Hello, world!</h1>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Build on the basic template above with Bootstrap's many components.
Bootstrap is built to work best in the latest desktop and mobile browsers, meaning older browsers might display differently styled, though fully functional, renderings of certain components.
Specifically, Bootstrap supports the latest versions of the following:
Unofficially, Bootstrap should look and behave well enough in Chromium and Chrome for Linux, Firefox for Linux, and Internet Explorer 7, though they are not officially supported.
Internet Explorer 8 and 9 are also supported, however, please be aware that some CSS3 properties and HTML5 elements are not fully supported by these browsers. In addition, Internet Explorer 8 requires the use of Respond.js to enable media query support.
Feature | Internet Explorer 8 | Internet Explorer 9 |
---|---|---|
border-radius |
Not supported | Supported |
box-shadow |
Not supported | Supported |
transform |
Not supported | Supported, with -ms prefix |
transition |
Not supported | |
placeholder |
Not supported |
Visit Can I use... for details on browser support of CSS3 and HTML5 features.
Beware of the following caveats when using Respond.js in your development and production environments for Internet Explorer 8.
Using Respond.js with CSS hosted on a different (sub)domain (for example, on a CDN) requires some additional setup. See the Respond.js docs for details.
file://
Due to browser security rules, Respond.js doesn't work with pages viewed via the file://
protocol (like when opening a local HTML file). To test responsive features in IE8, view your pages over HTTP(S). See the Respond.js docs for details.
@import
Respond.js doesn't work with CSS that's referenced via @import
. In particular, some Drupal configurations are known to use @import
. See the Respond.js docs for details.
IE8 does not fully support box-sizing: border-box;
when combined with min-width
, max-width
, min-height
, or max-height
. For that reason, as of v3.0.1, Bootstrap no longer uses max-width
on .container
s.
Bootstrap is not supported in the old Internet Explorer compatibility modes. To be sure you're using the latest rendering mode for IE, consider including the appropriate <meta>
tag in your pages:
<meta http-equiv="X-UA-Compatible" content="IE=edge">
This tag is included in all docs pages and examples to ensure the best rendering possible in each supported version of Internet Explorer.
See this StackOverflow question for more information.
Internet Explorer 10 doesn't differentiate device width from viewport width, and thus doesn't properly apply the media queries in Bootstrap's CSS. Normally you'd just add a quick snippet of CSS to fix this:
@-ms-viewport { width: device-width; }
However, this doesn't work as it causes Windows Phone 8 devices to show a mostly desktop view instead of narrow "phone" view. To address this, you'll need to include the following CSS and JavaScript to work around the bug until Microsoft issues a fix.
@-webkit-viewport { width: device-width; }
@-moz-viewport { width: device-width; }
@-ms-viewport { width: device-width; }
@-o-viewport { width: device-width; }
@viewport { width: device-width; }
if (navigator.userAgent.match(/IEMobile\/10\.0/)) {
var msViewportStyle = document.createElement("style")
msViewportStyle.appendChild(
document.createTextNode(
"@-ms-viewport{width:auto!important}"
)
)
document.getElementsByTagName("head")[0].appendChild(msViewportStyle)
}
For more information and usage guidelines, read Windows Phone 8 and Device-Width.
As a heads up, Bootstrap includes this in the Bootstrap docs as an example.
As of Safari v6.1 for OS X and Safari for iOS v7.0.1, Safari's rendering engine has some trouble with the number of decimal places used in Bootstrap .col-*-1
grid classes. So if you have 12 individual grid columns, you'll notice that they come up short compared to other rows of columns. Bootstrap can't do much here (see #9282) but you do have some options:
.pull-right
to your last grid column to get the hard-right alignmentBootstrap developers keep an eye on this though and will update their code if they have an easy solution.
Support for overflow: hidden
on the <body>
element is quite limited in iOS and Android. To that end, when you scroll past the top or bottom of a modal in either of those devices' browsers, the <body>
content will begin to scroll.
Also, note that if you're using inputs in your modal – iOS has a rendering bug that doesn't update the position of fixed elements when the virtual keyboard is triggered. A few workarounds for this include transforming your elements to position: absolute
or invoking a timer on focus to try to correct the positioning manually. This is not handled by Bootstrap, so it is up to you to decide which solution is best for your application.
Page zooming inevitably presents rendering artifacts in some components, both in Bootstrap and the rest of the web. Depending on the issue, Bootstrap developers may be able to fix it (search first and then open an issue if need be). However, Bootstrap developers tend to ignore these as they often have no direct solution other than hacky workarounds.
While Bootstrap doesn't officially support any third party plugins or add-ons, they do offer some useful advice to help avoid potential issues in your projects.
Some third party software, including Google Maps and Google Custom Search Engine, conflict with Bootstrap due to * { box-sizing: border-box; }
, a rule which makes it so padding
does not affect the final computed width of an element. Learn more about box model and sizing at CSS Tricks.
Depending on the context, you may override as-needed (Option 1) or reset the box-sizing for entire regions (Option 2).
/* Box-sizing resets
*
* Reset individual elements or override regions to avoid conflicts due to
* global box model settings of Bootstrap. Two options, individual overrides and
* region resets, are available as plain CSS and uncompiled LESS formats.
*/
/* Option 1A: Override a single element's box model via CSS */
.element {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
}
/* Option 1B: Override a single element's box model by using a Bootstrap LESS mixin */
.element {
.box-sizing(content-box);
}
/* Option 2A: Reset an entire region via CSS */
.reset-box-sizing,
.reset-box-sizing *,
.reset-box-sizing *:before,
.reset-box-sizing *:after {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
}
/* Option 2B: Reset an entire region with a custom LESS mixin */
.reset-box-sizing {
&,
*,
*:before,
*:after {
.box-sizing(content-box);
}
}
.element {
.reset-box-sizing();
}
Bootstrap follows common web standards, and with minimal extra effort, can be used to create sites that are accessible to those using AT.
If your navigation contains many links and comes before the main content in the DOM, add a Skip to main content
link immediately after your opening <body>
tag. (read why)
<body>
<a href="#content" class="sr-only">Skip to main content</a>
<div class="container" id="content">
The main page content.
</div>
</body>
When nesting headings (<h1>
- <h6>
), your primary document header should be an <h1>
. Subsequent headings should make logical use of <h2>
- <h6>
such that screen readers can construct a table of contents for your pages.
Learn more at HTML CodeSniffer and Penn State's AccessAbility.
Bootstrap is released under the Apache 2 license and is copyright 2013 Twitter. Boiled down to smaller chunks, it can be described with the following conditions.
The full Bootstrap license is located in the project repository for more information.
Bootstrap is best maintained when you treat it as a separate and independently-versioned dependency in your development environment. Doing this makes upgrading Bootstrap easier in the future.
Once you've downloaded and included Bootstrap's styles and scripts, you can customize its components. Just create a new stylesheet (LESS, if you like, or just plain CSS) to house your customizations.
Unless you plan on reading the CSS, go with minified stylesheets. It's the same code, just compacted. Minified styles use less bandwidth, which is good, especially in production environments.
From there, include whatever Bootstrap components and HTML content you need to create templates for your site's pages.
You can customize components to varying degrees, but most fall into two camps: light customizations and overhauls. Plenty examples of both are available from third parties.
Bootstrap defines light customizations as superficial changes, for example, color and font changes to existing Bootstrap components. A light customization example is the Twitter Translation Center (coded by @mdo). Let's look at how to implement the custom button Bootstrap developers wrote for this site, .btn-ttc
.
The stock Bootstrap buttons require just one class, .btn
, to start. Here you extend the .btn
style with a new modifier class, .btn-ttc
. This gives it a distinct custom look with minimal effort.
The customized button will be coded like this:
<button type="button" class="btn btn-ttc">Save changes</button>
Note how .btn-ttc
is added to the standard .btn
class.
To implement this, in the custom stylesheet, add the following CSS:
/* Custom button
-------------------------------------------------- */
/* Override base .btn styles */
/* Apply text and background changes to three key states: default, hover, and active (click). */
.btn-ttc,
.btn-ttc:hover,
.btn-ttc:active {
color: white;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
background-color: #007da7;
}
/* Apply the custom-colored gradients */
/* Note: you'll need to include all the appropriate gradients for various browsers and standards. */
.btn-ttc {
background-repeat: repeat-x;
background-image: linear-gradient(top, #009ED2 0%, #007DA7 100%);
...
}
/* Set the hover state */
/* An easy hover state is just to move the gradient up a small amount. Add other embellishments as you see fit. */
.btn-ttc:hover {
background-position: 0 -15px;
}
In short: Look to the style source and duplicate the selectors you need for your modifications.
In summary, here's the basic workflow:
.navbar
style specification.!important
here. Keep it simple.Once you are comfortable performing light customizations, visual overhauls are just as straightforward. For a site like Karma, which uses Bootstrap as a CSS reset with heavy modifications, more extensive work is involved. But the same principle applies: include Bootstrap's default stylesheet first, then apply your custom stylesheet.
While not recommended for folks new to Bootstrap, you may use one of two alternate methods for customization. The first is modifying the source .less
files (making upgrades super difficult), and the second is mapping source LESS code to your own classes via mixins. For the time being, neither of those options are documented here.
Not all sites and applications need to make use of everything Bootstrap has to offer, especially in production environments where optimizing bandwidth is an issue. Bootstrap encourages you to remove whatever is unused with their Customizer.
Using the Customizer, simply uncheck any component, feature, or asset you don't need. Hit download and swap out the default Bootstrap files with these newly customized ones. You'll get vanilla Bootstrap, but without the features *you* deem unnecessary. All custom builds include compiled and minified versions, so use whichever works for you.