Git is a version control system. It allows you to keep the entire history of your code, and makes it easy to share and collaborate with others.
Git works on a folder level. To set up a folder to work with git you need to open up the Sourcetree app and select File > New / Clone
. You then need to select the Create Repository tab from the top menu. In the Desitnation Path browse to the folder that you want to control with Git.
There are some other hidden files (e.g. those used by your operating system) that you don’t want to be version controlled. An example of these is the .DS_Store
file on macs. We’ll talk more about this later - for now we’ll just quickly exclude them from our repository.
To do this you need to change the settings on your repository.
This won’t work with Sourcetree. If you try and do this email Nick.
.DS_Store
on the first line of the ‘Ignored files’ section, and press ‘Save Changes’.If you’re on Windows you don’t need to worry about this.
Suppose we’ve now created a file called index.html
in the folder. It’s in the folder but currently isn’t being tracked by git.
Adding files to a git repository is actually a two-stage process: you have to add them and then commit them. This is useful when you get more advanced, but we’ll usually do those things together for the time being.
File > New/Clone ...
and select your folder in the destination path.Github is a site which will host a git repo online for you, making it easy to collaborate with others. We’ll now see how to put some code online.
Normally when we push code up to github, it just sits there for others to see and contribute to. The code we’re about to push up happens to be the code for a website. Instead of the files just sitting there, we want github to serve them as a website. The way github does this is through its github pages.
There are several ways of getting GitHub to publish your site as a GitHub page. We’re going to use a simple trick: in the branches panel of the GitHub app, change the name of the current branch from master
to gh-pages
.
To push code up to GitHub, in the GitHub app select the ‘Push to Github’ button (on the top right). You’ll be prompted to choose a name (which might as well be first_site
).
master
to gh-pages
.first_site
code.index.html
in Sublime Text. Commit the change to the repository and then push it to GitHub. Make sure you can see the change in the code on GitHub and also in the published page.Last time we looked at HTML, and saw how this was used to mark up the information in a webpage. Right now, your HTML pages don’t look very good, as you haven’t given any styling information. The way to do this is using CSS (Cascading Style Sheets).
CSS is a way of separating the way your page looks from the content that it displays. As an extreme example of this check out CSS Zen Garden - by clicking on the links you completely change how the site looks, but the html remains unchanged.
There are a few ways to include CSS in an HTML file:
<style>..</style>
section in the <head>
.css
file in the <head>
The first way is sometimes useful, but defeats the point of using CSS to separate presentation and information. The second way is a bit better and is what we’ll do now - it’s nice to have everything in a single file when you’re just starting. The third way is the best. We’ll look at how to do this next time.
<head>
If you’re putting your CSS in the <head>
of your html file (option 2) it should look something like this:
<head>
<title>Some title</title>
<style>
h1 { color: red; }
</style>
</head>
The bit inside the <style>
tags is CSS. The h1
bit specifies the tag that will be styled. The bit inside the { ... }
specifies the styles that will be applied. Here we change the colour of the h1
text red.
If you save the changes to the html file, then open (or refresh) the page in your browser you should see the changes. By opening the developer tools, and hovering over the h1
you should be able to see your css rule at the side.
In the example above we changed the color of the h1
element. Here are a few examples of other simple properties you can try out while you’re getting the hang of css:
p { font-family: 'Arial'; }
h2 { font-size: 20px; }
h3 {
background-color: green;
font-size: 2px;
}
Note that you can specify multiple properties on one element. When you do this it’s nice to lay them out on multiple lines as done above.
exercise1.html
in Sublime Text and in Chrome.head
to make the h1
turn red.exercise1.html
looks like exercise1_solution.png
.So far you have used html tags to specify CSS rules. For example,
h2 {
font-size: 40px;
color: pink;
}
will turn all your <h2>
massive and pink.
It is often useful to be able to make CSS rules more specific, so you can apply different styles to different parts of the page. One common way to do this is to target specific html attributes - in particular id
and class
.
id
and class
attributesHTML tags can have attributes which provide additional information about the element. You’ve aready seen some examples of this:
<a href="http://www.facebook.com">
<img src="cat_pic.png">
Both href
and src
are examples of html attributes. They are written in pairs with their values: attribute="value"
.
There are some attributes that can be added to any tag. Two examples of these are id
and class
:
<h2 id="products_title">Our scrumptious puddings</h2>
<ul id="products_list">
<li class="product_item">Black forrest gateau</li>
<li class="product_item">Rasberry lemon swirl cheesecake</li>
<li class="product_item">Sticky toffee pudding</li>
<li class="product_item">Death-by-chocolate cake</li>
</uk>
Both id
and class
are used to add some information to the HTML tags. The key difference is that id
should specify a unique element on the page, whereas multiple elements can share the same class
.
CSS lets you target the id
and class
attributes in special ways:
/* make the h2 with id="products_title" purple */
h2#products_title { color: purple; }
/* remove the bullets from all li with class="product_item" */
li.product_item { list-style-type: none; }
The id
is targeted by adding #id_value
to the tag and the class
is targeted by adding .class_value
to the tag.
It is also possible to target any items with a given class
or id
by leaving out the HTML tag:
/* make any element with id="products_title" purple */
#products_title { color: purple; }
/* make any element with class="product_item blue" */
.product_item { color: blue; }
There are two important HTML tags, that we didn’t use last week: <div>
and <span>
. Both are really useful when it comes to using HTML attributes to target CSS classes.
<div>
stands for division and is used to break the page up into different parts. It is a ‘block-level’ element, which means that it will start a new line before and after it.
<span>
can be used to apply classes and ids to certain bits of text. It is an ‘inline’ element, which won’t start a new paragrah before or after.
<div id='info_section'>
<p>This is a paragraph in the info section. We can use a span to target <span class='important'>certain bits of important text</span>.<p>
</div>
html2/exercise2.html
in Sublime Text and Chrome.html2/exercise2_solution.png
So far you have written your CSS rules directly in the head
section of your html page:
<!DOCTYPE html>
<html>
<head>
<title>My page</title>
<style type="text/css">
h1 { color: red; }
</style>
</head>
<!-- body goes here -->
</html>
We did this to make your first experiments with CSS quick an easy. In a live site it is considered bad practice to put your CSS inside the head
section: Typically a site will have one set of styles that apply to all the pages. By separating these CSS rules into their own file you (a) reduce repetition in your code and (b) reduce the amount of information that has to be sent to the browser for each page - if the CSS file applies to the whole site, it only needs to be sent to the visitor once.
To link to an external CSS file you can do the following:
<!DOCTYPE html>
<html>
<head>
<title>My page</title>
<link rel='stylesheet' type='text/css' href='path/to/my_css_file.css'>
</head>
<!-- body goes here -->
</html>
Linking to other files (stylesheets, javascript files, images) can be done in several ways, just like linking to another page. Say you have the following directory structure:
first_site
|
---- index.html
|
---- images
| |
| ---- background.jgp
|
---- stylesheets
|
---- main.css
and you’re going to deploy your site to “http://www.my_first_site.com”. Suppose you want to link to main.css
from index.html
and to background.jpg
from main.css
. There are three different styles of links you can use:
Absolute external links include the complete url to the resource you’re linking to. Absolute links start with either http:// or https://.
<!-- in index.html -->
<link rel="stylesheet" type="text/css" href="http://www.my_first_site.com/stylesheets/main.css">
/* in main.css */
body {
background-image: url("http://www.my_first_site.com/images/background.jpg");
}
Absolute external links can be used to link to resources held on different sites, but wouldn’t usually be used for links within your own site. They’re a bit fragile - if you change your domain name all the links will break. They also won’t work when you’re developing locally.
Root-relative links contain the path to the resource relative to the site’s root. The site’s root is (roughly) the folder that contains the site - in this case, first_site
. Root-relative links begin with a /
:
<!-- in index.html -->
<link rel="stylesheet" type="text/css" href="/stylesheets/main.css">
/* in main.css */
body {
background-image: url("/images/background.jpg");
}
Root-relative links are a bit more flexible than absolute external links: e.g. if you change your domain name everything will still be fine. They’re sometimes useful for your own static sites, but probably won’t work when developing locally (because the root will be taken to be the root of your file system and not the folder containing the site!).
Document-relative links contain the path to the resource relative to the file where the link is written. Document-relative links don’t begin with /
:
<!-- in index.html -->
<link rel="stylesheet" type="text/css" href="stylesheets/main.css">
/* in main.css */
body {
background-image: url("../images/background.jpg");
}
To link to the stylesheet from index.html
we use stylesheets/main.css
which says “look in the same folder I’m in (first_site
) and find a folder called styleheets
and a file in it called main.css
”.
To link to the image from the stylesheet is slightly more complicated: we use ../images/background.jpg
. This means “go to the folder above the one I’m in (I’m in stylesheets
, so that’s first_site
) and find a folder called images
and a file in it called background.jpg
”.
.
means in the folder that I'm in..
means in the folder above the one that I'm inRelative links are the most flexible - they will work on your local file system. The only think you have to be careful about is moving your files into different folders, which can cause links to break.
You should be using relative local links in these exercises.
For a recap of all this, read this article.
exercise1.html
and separate the CSS out into a separate file (called exercise1.css
).exercise1.css
and check that you can see that in the browser. (You might need to refresh the page a few times).Finish off both CSS exercises from class. Check your solutions online:
branch
dropdown (just above the list of files) select the solution
branch.first_site
based on what you've learnt. Make sure you add them to git, push them to github and check you can see them online.The point of this exercise is to set up your domain name to point towards your new GitHub pages site.
As GitHub hosts many different pages at their IP address, it isn’t quite as simple as pointing your domain address towards that IP address. There are two things you need to do to get your domain name working with your GitHub pages site:
Github explains this here.
For the first bit you need to log in to your domain registrar and change the DNS settings. You want an A-record pointing to 204.232.175.78
(which is github.com). Note that it can take up to a couple of days for DNS changes to propagate.
If you’re using 123-reg, your should log in, select your domain from the list, and click “Manage”. You should then go to “Manage DNS”.
(The @ dns entry stands for the root or bare domain.)
Log in to your domain registrar and set an A-record to point towards GitHub.
Your changes won’t take effect immediately.
Open Sublime Text and create a new file.
Write your domain name on the first line of the new file e.g:
mydomain.com
Save that file as CNAME
(uppercase, with no extension) in your first_site
folder
Commit your change and then push to github.