What's hard about CSS?

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.

What else is hard about CSS?

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.

Should I just give up then?

You might be the sort of person who relishes this sort of challenge - if so, great! If not, help is at hand

Twitter Bootstrap

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:

  1. Link to the Twitter Bootstrap stylesheet in the head of your html page.
  2. Attach the relevant Twitter Bootstrap class to you html element.

An example: making a stripy table

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

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.

Task:

The aim of the rest of this session will be to create the website for "Sam's Sarnies" using Twitter Bootstrap.

  1. Clone down the repository for the bootstrap exercise: https://github.com/code61/bootstrap_exercise.git
  2. Open bootstrap_exercise/index.html in your browser.
  3. Go to the Bootstrap website (it's hosted at github, like your first_site) and click the Download Bootstrap button (not the Download Source).
  4. Unzip and copy the dist folder into the bootstrap_exercise folder.
  5. Open index.html in Sublime Text and Chrome.
  6. Add a link to the twitter bootstrap stylesheet into index.html
    <link href='dist/css/bootstrap.css' rel='stylesheet'>
    
    Note that you're using a document-relative link to a file two subfolders down.
  7. Refresh the page in Chrome. Notice how the fonts have changed.
  8. Add the following line to the head section:
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    as suggested in the CSS / Overview section of the Bootstrap docs.

Bootstrap layout

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:

Page margins

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>

Columns

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.

Task:
  1. Add a container div class=&#39;container&#39; around the page content.
  2. Create a row at the top of the page, with two columns, with the left twice as wide as the right. Put the h1 in the left column and the img src=&#39;images/sandwich.png&#39; on the right.
  3. Create a row with three equal colums to hold each of "The Buzz" divs.

More Bootstrap

Typography

Skim through the typography section of the bootstrap docs.

Task:
  1. Change the quotes in 'The Buzz' to use blockquotes. (Don't worry about the vertical grey lines - we'll remove those later.)
  2. Change the paragraphs in 'Our mission' to be lead body copy.

Badges and Buttons

Check out the buttons section (CSS > Buttons).

Task:
  1. Change the 'Send' button to a success button:
    <button class='btn btn-success' >Send</button>
    
    You might also need btn-small in the Recent Activity section.
  2. 2. Make the social links at the bottom in to large buttons (we'll colour them later):
    <button class='btn btn-lg' >Send</button>
    

Images

Have a look at the image section of the Bootstrap docs (CSS > Images).

Task:
  1. Make the images in 'The Buzz' round, by adding the img-circle class.
  2. You can center the image by adding the helper class center-block.
  3. Change the main sandwich image into a img-responsive, as described in the responsive images section. Try resizing your browser and see how it changes size.

Modifying Bootstrap

Warning: Bootstrap has been designed and heavily tested for good cross-browser compatibility. Unless you know what you're doing or have a lot of time, it's probably best to stick with their layout and tweak small things e.g. fonts and colours. Just because it looks great in your browser doesn't mean it will look great in everyone's!

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.

Task:
  1. Create a file called 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;
    }
    
  2. Link this file into the head of index.html underneath your link to bootstrap.
  3. What happens? Notice how in the first rule we've selected only those buttons that exist inside an element with id=social-buttons.

Changing the background

Task:
  1. Change the background of the 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;
    }
    
  2. This doesn't look quite right. The problem is that the jumbotron is it is inside the div class=&#39;container&#39;. You can change this by moving it inside:
    <div class="jumbotron">
      <div class="container">
    
      </div>
    </div>
    
    Similarly you will now need to create new containers inside the div id=&#39;buzz&#39; and div id=&#39;mission&#39;.
  3. Change the background color of the mission section to rgba(32, 35, 41, 0.9) and the font colour to #ddd

Browse through the navbar section of the Bootstrap docs.

Task:
  1. Look at the html for the basic starter template.

  2. Use it to add a navbar to your site.

  3. Make it a navbar-fixed-top. You will need to add

     body { padding-top: 70px; }

    to main.css.

  4. Add a search box to the navbar. Use the pull-right class to put it on the right hand side.

Other things

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.

Task:
  1. Make any other changes necessary to make your site look like the example
  2. If you get stuck check by looking at the gh-pages branch on the github repo.

Homework

Finishing off

Task:

Finish off the bootstrap_exercise from class

Personal site

Task:

Continue work on your personal site. You might want to think about using Twitter Bootstrap!

Get a Google Account

Task:

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!

Further reading on CSS

Task:
  1. (Optional) Read this article which explains about CSS Specificity (and more).
  2. (Optional) Read this article on CSS selectors.