Websites, HTML, CSS

Understanding the basics of websites and HTML

Matthew DeHaven

March 31, 2024

Course Home Page

Lecture Summary

  • Static vs. Dynamic
  • HTML
  • CSS
  • Bootstrap

Website Primer

Websites

Websites are simply servers that deliver files upon request.

They will always return HTML files

  • HyperText Markup Language

The benefits of HTML

  • Supported by every browser
  • Very flexible formatting
  • Light-weight (meaning websites load quickly)

Static vs. Dynamic Websites

HTML files are static.

This means that when you request a specific page from a server, you always get the same html file.

In contrast, some websites are dynamic.

  • like amazon.com, netflix.com, nytimes.com etc.

When you visit Amazon, they dynamically build a webpage specific to you.

  • Query their database for what products you like, what your recent purchases were, etc.

Client-side vs. Server-side Interactivity

A dynamic website will rebuild html files for each user using data on their servers.

You can, however, have some interactivity for static websites using JavaScript.

Javascript is another programming language that can run client-side (on your machine).

  • Very good at editing html files
  • allows for sites where you “click this button” to “show a figure”
  • All of the interactive data visualizations you see from the NYTimes are written in JavaScript

HTML Files

We will focus on basic HTML files.

  • static
  • non-interactive

Really, an HTML file is just a messy document.

  • similar to LaTeX, or markdown for making documents

HTML

Example HTML file

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Hello world!</p>
  </body>
</html>

Hello world!

HTML Document Structure

<!DOCTYPE html> tells the computer this is an html document

<html>

</html>

These are opening and closing commands that wrap the entire html document.

Every HTML document has two key components:

  • “head” for metadata and settings
  • “body” for content

HTML Elements

Every HTML document is made up of a nested list of elements.

A basic element is <p></p> for paragraphs of text.

  • <p> defines the start of a text paragraph
  • </p> defines the end

There are header elements as well:

<h1>Heading level 1</h1>
<h2>Heading level 2</h2>
<h3>Heading level 3</h3>
<h4>Heading level 4</h4>
<h5>Heading level 5</h5>
<h6>Heading level 6</h6>

Example HTML with Headers

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <h1>A Big Header</h1>
    <p>Hello world!</p>
    <h2>Another smaller header</h2>
    <p>Some more text written in a paragraph block</p>
  </body>
</html>

A Big Header

Hello world!

Another smaller header

Some more text written in a paragraph block.

Bold and Italics

We can set bold and italics text using the default <b> and <em> tags.

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Hello <b>world</b>!</p>
    <h2>Another smaller header</h2>
    <p>Some more <em>text</em> written in a paragraph block</p>
  </body>
</html>

Hello world!

Another smaller header

Some more text written in a paragraph block

Divs

<div> elements are html tags for divisions or sections.

  • commonly used to group portions of a html document together.
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Hello <b>world</b>!</p>
    <div>
    <h2>Another smaller header</h2>
    <p>Some more <em>text</em> written in a paragraph block</p>
    </div>
  </body>
</html>

Hello world!

Another smaller header

Some more text written in a paragraph block

Setting Div Styles in HTML

We can use the <div> to set a style attribute for a group of html elements.

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Hello <b>world</b>!</p>
    <div style="background-color:#FFF4A3;">
    <h2>Another smaller header</h2>
    <p>Some more <em>text</em> written in a paragraph block</p>
    </div>
  </body>
</html>

Hello world!

Another smaller header

Some more text written in a paragraph block

Setting Styles in HTML

In fact, every HTML element has a style= attribute.

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Hello <b>world</b>!</p>
    <div style="background-color:#FFF4A3;">
    <h2>Another smaller header</h2>
    <p>Some more <em style="background-color:#FFC0C7;">text</em> written in a paragraph block</p>
    </div>
  </body>
</html>

Hello world!

Another smaller header

Some more text written in a paragraph block

Inheritance

Notice that our “text” emphasis ends up red even though it is within the <div> tag.

Hello world!

Another smaller header

Some more text written in a paragraph block

This is because HTML elements inherit attributes and styles from parent elements

  • but the lowest level element has priority

Inheritance Example

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Hello <b>world</b>!</p>
    <div style="background-color:#FFF4A3;">
    <h2>Another smaller header</h2>
    <p>Some more <em style="background-color:#FFC0C7;">text</em> written in a paragraph block</p>
    </div>
  </body>
</html>

Our “text” inherits from:

  • <html>
    • <body>
      • <div>
        • <p>
          • <em>

Setting Styles in the Header

Instead of setting styles for each tag, set them for all copies of an element.

<!DOCTYPE html>
<html>
  <head>
  <style>
    p {background-color: #FFF4A3;}
    em {color: blue;}
  </style>
  </head>
  <body>
    <p>Hello <b>world</b>!</p>
    <div>
    <h2>Another smaller header</h2>
    <p>Some more <em>text</em>
    written in a paragraph block</p>
    </div>
  </body>
</html>

CSS

CSS

CSS stands for “Cascading Style Sheets”.

  • Think of the “inheritance” chain for “cascading”

CSS controls the style of HTML elements.

It can be set in 3 ways:

  1. Inline with the style= argument for a specific HTML element
  2. Internal with the <style></style> element in the <head> section
  3. External with a <link> to an external CSS file

External CSS File

styles.css
p {background-color: #FFF4A3;}
em {color: blue;}
index.html
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <p>Hello <b>world</b>!</p>
    <div>
    <h2>Another smaller header</h2>
    <p>Some more <em>text</em>
    written in a paragraph block</p>
    </div>
  </body>
</html>

Setting Classes

Now that we have seen CSS, it is useful to talk about one more HTML element attribute:

  • classes

Any HTML element can be given a class.

You can specify styles for a specific class in your CSS.

Class Example

Class styles are set with .<classname>{}. Classes are set as attributes.

styles.css
.myclass {
  color: blue;
  font-size:30px;
  }
index.html
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <p>Hello <b>world</b>!</p>
    <div class = "myclass">
    <h2>Another smaller header</h2>
    <p>Some more <em>text</em>
    written in a paragraph block</p>
    </div>
  </body>
</html>

Class Example Continued

Different elements can reference the same class.

styles.css
.myclass {
  color: blue;
  font-size:30px;
  }
index.html
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <p>Hello <b class="myclass">world</b>!</p>
    <div class = "myclass">
    <h2>Another smaller header</h2>
    <p>Some more <em>text</em>
    written in a paragraph block</p>
    </div>
  </body>
</html>

Classes Summary

Classes are a powerful way to group different style formatting and apply them across elements.

You can define your own classes and the specific formating for those classes.

They can also be used by JavaScript for some interactive elements, but we won’t cover that.

Learning More HTML and CSS

I recommend checking out W3Schools’ HTML Tutorials for more information on HTML and CSS.

We have covered the very basics:

  • HTML files are documents
  • Consist of a <head> and a <body>
  • The body has nested elements which have attributes
  • Styles are set inline or in CSS files
  • Elements inherit styles and attributes

Hosting Static Websites

Hosting Static Websites

There are many services for hosting static websites.

The one we will use is GitHub Pages.

GitHub Pages

Can be tuned on for any repository.

You will need to tell it which “branch” has your HTML files

  • probably “main”

And whether they are in the “root” folder or “docs/” subfolder

  • default is “root” which is just the repository folder
  • “docs/” is useful if you want to keep your website files separate

GitHub User Page

Each GitHub user can setup a home page for their GitHub account.

It is always called:

  • “www.<yourusername>.github.io”

The URL for my account would be “https://www.matdehaven.github.io”

GitHub Project Pages

Every repository can have its own page.

By default, theses will all live at

  • “www.<yourusername>.github.io/<yourprojectname>”

Website Home Pages

When you open a website location, your browser will by default open the file called

  • “index.html”

So the most basic website you can create has only an “index.html” file, and maybe a “styles.css” file.

Bootstrap

Bootstrap 5

Bootstrap is a “framework” for HTML websites

  • Includes HTML and CSS templates and pre-made elements
  • And some JavaScript functions for interactivity

The goals of Bootstrap are

  • fast and easy
  • mobile-friendly
  • responsive designs

Bootstrap Benefits

Bootstrap has…

  • Nice default stylings
    • And multiple “themes” to choose from
  • dynamic resizing
    • ex. on mobile, your website collapses menu buttons together
  • fluid “containers” for your content

Bootstrap CDN

Bootstrap can be installed, or you can use the Content Delivery Network (CDN) to load in the <head> of any HTML file.

<!DOCTYPE html>
<html>
  <head>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    
  </body>
</html>

Bootstrap Features

  • Buttons
  • Navbars (menus at the top of the screen)
  • Badges
  • Image Carousels
  • much more

What About Google Sites?

There are various services for building websites that are “point-and-click”:

  • Google Sites (free!), Squarespace, Wix, Wordpress

You lose some freedom but don’t need to know any programming.

When hosting files on Google Drive (used with Google Sites) or Dropbox, some people won’t be able to open them.

But if you find yourself after this course intimidated by maintaining your own website, Google Sites is by far the easiest option.

Live Coding Example

Live Coding Example

  • Simplest HTML file website on GitHub Pages

  • Adding another page

  • Adding a CSS style file

  • Adding Bootstrap