DEV Community

Cover image for How to host static sites on Github pages
Daniel Omehe
Daniel Omehe

Posted on

How to host static sites on Github pages

Finally finished your first project, you're so excited you want to show it to everyone but then you remember you don't know how to host a site online? Well this tutorial covers how to get you started with hosting static pages on GitHub pages. Let get into it.

Prerequisites:

  1. HTML
  2. CSS
  3. And, maybe some JavaScript

Note: This tutorial assumes you already know a bit of HTML and CSS. So i won't be addressing those in this tutorial.

To get started you'll need to have git installed, if you don't already have git installed on your computer you can get it here: https://git-scm.com/.

Next, create a folder and add an HTML and CSS files. I'll be creating a simple application with just a loading spinner animation. Copy and paste the following code in your local file:

Index.html

<!Doctype html>
   <html lang="en">
     <head>
        <title>Git And Sass practice</title>
        <link rel="stylesheet" href="./css/styles.css" />
    </head>
    <body>
        <div class="loading"></div>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Styles.css

body {
  background-color: red;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

h1 {
  color: white;
  font-size: 5rem;
  text-align: center;
}

.loading {
  width: 100px;
  height: 100px;
  border: 5px solid #fff;
  animation: spin 2s linear infinite;
  position: relative;
}

.loading::before {
  content: "";
  width: 100%;
  height: 100%;
  border: 5px solid #fff;
  position: absolute;
  left: -0.3rem;
  top: -0.3rem;
  animation: spinbefore 2s linear infinite;
}
.loading::after {
  content: "";
  width: 100%;
  height: 100%;
  border: 5px solid #fff;
  position: absolute;
  left: -0.3rem;
  top: -0.3rem;
  animation: spinafter 2s linear infinite;
}

@keyframes spin {
  50% {
    transform: rotate(360deg);
  }
}

@keyframes spinbefore {
  50% {
    transform: rotate(30deg);
  }
}

@keyframes spinafter {
  50% {
    transform: rotate(60deg);
  }
}
Enter fullscreen mode Exit fullscreen mode

With that done, open your browser and visit the GitHub official website, follow the steps to create an account if you do not have one already, i won't be covering that in this tutorial.

If you already have an account login, once that is done it takes you to your this page.

Image description

Click on new on the left sidebar to create a new repository.

Image description

On the next page enter your repo name, click on add Readme and then click on create repository to finish creating your repo.

Image description

Voila! you just completed the first step to hosting you site. Here is what your repo looks like for a start but as you make changes and push it is bound to reflect all the files from your project.

Image description

Next, we need to connect the remote repo to your local repo, open git bash/cmd on windows and terminal on mac. I prefer to use terminal from my vs code editor that way i don't have to leave my editor.

Open terminal and run git init to initialize an empty git repo in the folder you already have open. Then open GitHub and on the new repo click on the green code button and copy the repo url.

Image description

Now to connect the local repo to the remote repo you can either run

git clone to create a local close of your repo

or

git remote add origin [the-repo-url-you-copied]

and then run to pull and merge all files in the remote repo with those in the local one.

git pull origin main

Now, run git add . to add all files in the to the staging area

and then run git commit -m "your-commit-message"

and then run git push origin main to push all changes to your remote repo.

Then, open GitHub and go to the settings tab, click on the pages tab on the left sidebar

Image description

In the pages tab select deploy from a branch, a little further down select the branch to deploy from and click save

Image description

And like that your site is live after a few minutes reload page and click visit site.

Image description

Thank you for reading.

Live version of my local example: https://danielomehe.github.io/sass-compile/

Top comments (1)

Collapse
 
adewoleademola profile image
Ademola Adewole

This is amazing impressive it really help me to figure out mine problem in github and it seem easy peasy