You’ve seen quite a bit of CSS now; it all seems quite straightforward - you write some css, tweak it ‘til it looks good and you’re done! In theory this is exactly how CSS works and is why CSS is brilliant.
Unfortunately, the realities are not quite so straightforward. Different browsers will render CSS with subtle differences. Take a look at the cat picture above. The styling is relatively simple - all we’ve done is add a border and a shadow. The following CSS will probably do this in your browser:
.img-polaroid {
padding: 4px;
border: 1px solid rgba(0, 0, 0, 0.2); /* transparent black border */
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
However, to get this effect in all browsers you need the following:
.img-polaroid {
padding: 4px;
background-color: #fff;
border: 1px solid #ccc; /* grey border for browsers that can't do transparency */
border: 1px solid rgba(0, 0, 0, 0.2); /* put the transparency after for browsers that do */
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); /* some browers only understand webkit box shadow */
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); /* others need this */
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
Just because your site looks good in Chrome, doesn’t mean it will look good in Internet Explorer. Making your site look good (or even presentable) in multiple browsers takes time, effort and experience.
About 5 years ago, ‘all’ you would have had to worry about is the cross-browser display issues. Since then, the mobile web has exploded and you have another (far more important) concern: how will your site look when viewed on a mobile?
Making webpages that look good when viewed at multiple different sizes is a whole new level of complexity.
You might be the sort of person who relishes this sort of challenge - if so, great! If not, help is at hand
Twitter Bootstrap is set of CSS (& Javascript) files, released by the makers of Twitter.
Twitter Bootstrap is a set of ready-made CSS files that provide solutions to common presentation requirements in a cross-browser and responsive way. To make use of Twitter Bootstrap, you need to do two things:
head
of your html page.Suppose you want a Zebra-esque table like this fine specimen:
I am a | Zebra table! |
Look at my | stripes. |
Do you find them | beautiful? |
Do you find them | mesmerising? |
Wooaahh .. | .. such stripiness! |
You have a look at the Twitter Bootstrap table documentation and discover that you need to add the table table-striped
class to the <table>
tag:
<table class="table table-striped">
...
</table>
This will apply the relevant CSS rules from the bootstrap CSS file. If you’re interested, you can go into Developer Tools and view the rules that apply e.g.
/* from line 1950 of bootstrap.css */
.table-striped tbody tr:nth-child(odd) td,
.table-striped tbody tr:nth-child(odd) th {
background-color: #f9f9f9;
}
Responsive design means designing your sites so that they look good on all screen sizes.
Twitter Bootstrap promotes a ‘mobile first’ philosophy, encouraging you to design your site so that it looks good at all sizes from the very beginning. It provides a lot of useful CSS that helps you to do this.
We’re not really going to cover responsive design in depth in class, but the Bootstrap docs do a good job of explaining what’s possible. Take a look at the grid system as an example.
The aim of the rest of this session will be to create the website for "Sam's Sarnies" using Twitter Bootstrap.
bootstrap_exercise/index.html
in your browser.
first_site
) and click the Download Bootstrap
button (not the Download Source).
dist
folder into the bootstrap_exercise
folder.
index.html
in Sublime Text and Chrome.
index.html
<link href='dist/css/bootstrap.css' rel='stylesheet'>
head
section:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
CSS / Overview
section of the Bootstrap docs.
Bootstrap gives you several options on how to layout your page. To find out more take a look at the Grid system docs on the Bootstrap site. Some of these options will work out-of-the-box with responsive design - so that you can create a single html page which will look good whether viewed on a laptop, tablet or phone.
We’ll just look at a few of the most important layout options here:
Bootstrap makes it easy to center content on your page and provide sensible page margins. To do this make use of the container
class:
<body>
<div class="container">
<!-- page content goes here -->
</div>
</body>
Bootstrap works on a grid layout, with 12 columns (by default). You can create a column layout with the row
and span
classes:
<div class='row'>
<div class='col-sm-4'>
<!-- First column content -->
</div>
<div class='col-sm-4'>
<!-- Second column content -->
</div>
<div class='col-sm-4'>
<!-- Third column content -->
</div>
</div>
The number after the col-sm-
determines how many of the 12 grid columns that page column takes up. The sm
bit determines the width at which the columns will collapse on top of each other (which is useful when viewing your site on a phone). For more information see the Grid system docs.
div class='container'
around the page content.h1
in the left column and the img src='images/sandwich.png'
on the right.div
s.Skim through the typography section of the bootstrap docs.
Check out the buttons section (CSS > Buttons).
<button class='btn btn-success' >Send</button>
btn-small
in the Recent Activity section.
<button class='btn btn-lg' >Send</button>
Have a look at the image section of the Bootstrap docs (CSS > Images).
img-circle
class.center-block
.img-responsive
, as described in the responsive images section. Try resizing your browser and see how it changes size.If you’re going to modify Bootstrap don’t touch the Bootstrap files. Instead create a new css file of your own to overwrite anything you don’t want. This means when a new version of Bootstrap comes out you can upgrade by dragging the new version over the top of the old, without losing any modifications.
main.css
and write the following CSS:
#social-buttons button {
color: white;
}
.btn-twitter {
background-color: #00acee;
border-color: #009ad5;
}
.btn-facebook {
background-color: #4868ac;
border-color: #314776;
}
.btn-pinterest {
background-color: #b62f26;
border-color: #b62f26;
}
head
of index.html
underneath your link to bootstrap.
id=social-buttons
.
jumbotron
to be the image fruit-and-veg.png
by adding the css
.jumbotron {
min-height: 600px;
background-image: url('images/fruit-and-veg.jpg');
background-size: cover;
background-attachment: fixed;
}
div class='container'
. You can change this by moving it inside:
<div class="jumbotron">
<div class="container">
</div>
</div>
container
s inside the div id='buzz'
and div id='mission'
.
mission
section to rgba(32, 35, 41, 0.9)
and the font colour to #ddd
Browse through the navbar section of the Bootstrap docs.
Look at the html for the basic starter template.
Use it to add a navbar to your site.
Make it a navbar-fixed-top
. You will need to add
body { padding-top: 70px; }
to main.css
.
Add a search box to the navbar. Use the pull-right
class to put it on the right hand side.
There are various other changes you will need to make your site look like the example. Try and figure out what these are by examining the html in the Developer tools.
Finish off the bootstrap_exercise from class
Continue work on your personal site. You might want to think about using Twitter Bootstrap!
If you don't already have a google account (e.g. via a gmail address), you need to sign up for one: https://accounts.google.com/SignUp. You'll need it in class next week!