Mark As Completed Discussion

In this lesson, we will learn about working of web pages, with a focus on following key points,

  1. What are the technologies used in the creation of web pages?
  2. How to build and style websites using HTML and CSS.

While studying, working, or even doing daily tasks, we use the internet every day and browse through various web pages. But did you ever think about how these web pages are made? Or how to build one of your own? In this lesson, we will learn about the two popular programming languages behind every web page, HTML and CSS, and how we can use these languages to create our web pages.

Working of Web Pages

All web pages are HTML documents. To improve the style, appearance, and interactivity of a website, external files such as style sheets (CSS files) and scripts (JavaScript files) are linked with the original HTML document. These files can also be embedded within the HTML document (which we will discuss in upcoming sections!).

What are HTML and CSS?

We've mentioned that HTML and CSS are core components behind web pages. But what are they, and what corresponding functions do they have behind the scenes?

HTML, is short for HyperText Markup Language. It is the standard language for creating web pages. It consists of various elements which define the content and provide a proper structure to the various objects of the page. Headings, paragraphs, navigation bars, etc. are all different elements implemented in HTML.

CSS, or Cascading Style Sheets, are responsible for styling the appearance of the HTML elements on the web page. CSS defines different rules to define how HTML elements appear on the page. Fonts, colors, alignment, and placement of elements on the web page are all defined using CSS.

Basics of HTML

As mentioned earlier, HTML consists of various elements that define the structure and content of the page. Each of these elements has a specific purpose. For example, all the content of the web page must be enclosed within the <body> element. All element content is also enclosed within tags, which are angle brackets <> surrounding the element name.

Tags and Attributes

Tags mostly occur in pairs, with an opening and a closing tag. Opening tag consists of the element name enclosed in angle brackets and indicates the beginning of the element. For example, <p>, for paragraph element. Closing tag consists of the element name and a forward slash enclosed in angle brackets. For example, </p>, which closes the paragraph element.

Basics of HTML

Attributes are properties of HTML elements that provide additional information. They are defined after the element name in the opening tag. The attribute name is followed by an equals sign, to which the attribute value is assigned in quotation marks. For example, the id attribute allows assigning a unique identifier to an HTML element as follows.

Basics of HTML

Setting up an HTML Web page

Now let's start setting up a basic web page. We initialize the HTML document using some specific tags, which consist of <!DOCTYPE html>, <html>, <head> and <body>.

SNIPPET
1<!DOCTYPE html>
2<html>
3<head>
4<title> My First Webpage </title>
5</head>
6<body>
7<h1> Hello World! </h1>
8</body>
9</html>

This creates a document like this.

Setting up an HTML Web page

Let's analyze the purpose of each of these elements.

  1. <!DOCTYPE html> element: This element defines the document type declaration, and informs the browser about which language we are using to create the website. Since we're using HTML, html is specified. Without this specification, the browser will not be able to display the web page.
  2. <html> element: This element defines the entire HTML document, and encloses all other elements.
  3. <head> element: This element defines the metadata about the document. For example, links to style sheets, scripts, or the title of the page (the <title> element as you can see in the above example too) are all defined within this element.
  4. <body> element: All the visible content of the webpage is displayed within this element. Various other elements such as paragraphs, headings, etc are defined within this element.

Common HTML Tags

With the above example, we can only set up a basic webpage with not much content. In reality, an HTML document is composed of several different elements, each of them having a unique purpose. Let's see some of the common ones in the table below.

HTML ElementTagUsage
Headings<h1> to <h6>Defines headings with <h1> being the most important to <h6> being the least important
Paragraphs<p>Creates new paragraphs
Hyperlinks<a>Creates a hyperlink by specifying the URL in the href attribute with the opening tag
Images<img>Adds images to the document by specifying the image path in the src attribute of the opening tag. This element does not have a closing tag.

Try this exercise. Click the correct answer from the options.

The correct sequence of HTML tags for starting a web page is

Click the option that best answers the question.

  • head, title, html, body
  • html, head, title, body
  • html, body, title, head
  • head, html, title, body

Basics of CSS

Web pages would be extremely boring if they only had black and white text as we created above. In most web pages that you've seen, there are colors, images, and several other interactive elements that make it look appealing. CSS helps us achieve this purpose.

Since we've understood how a basic webpage can be created, let's see how it can be styled. Style sheets are created and linked with the HTML document using the <link> tag within the head element.

SNIPPET
1<!DOCTYPE html>
2<html>
3<head>
4<title> My First Webpage </title>
5<link rel="stylesheet" href="style.css">
6</head>
7<body>
8<h1> Hello World! </h1>
9</body>
10</html>

Style sheets can also be defined within the HTML document using the <style> tag within the head element. However, the recommended approach is to use external style sheets.

CSS Rules

CSS styles can be applied to elements in an HTML document. More specifically, it defines the rules that need to be applied to an element or group of elements in HTML.

In CSS, rules begin with a selector. The selector is the HTML element that we want to style. This is followed by curly braces, inside which is the declaration. The declaration specifies the properties that need to be styled along with a corresponding value. The property must be followed by a colon and the declaration by a semi-colon.

Basics of CSS

Note that if we specify the selector as an HTML element, then all the HTML elements in the document will be styled in the same way. This saves a lot of time as we do not have to style elements individually!

Build your intuition. Is this statement true or false?

HTML document has "elements" which are styled using "rules".

Press true if you believe the statement is correct, or false otherwise.

Adding CSS to an HTML Web page

Let's work through an example of adding a style sheet to a basic HTML document. Consider a simple HTML document below,

SNIPPET
1<!DOCTYPE html>
2<html>
3<head>
4    <title> My First Webpage </title>
5    <link rel="stylesheet" href="style.css">
6</head>
7<body>
8    <h1> Hello World! </h1>
9    <p> This is my first webpage. </p>
10</body>
11</html>

Let's start by changing the color of the h1 heading in style.css.

SNIPPET
1h1{
2    color: blue;
3}
Adding CSS to an HTML Web page

Now let's specify a change in multiple properties of the paragraph element. CSS allows us to specify multiple properties within a single rule, so we can change the background color and font size of a paragraph element as,

SNIPPET
1p{
2    background-color: gray;
3    font-size: 25px;
4}
Adding CSS to an HTML Web page

We can see that now our simple webpage looks a little more colorful than before.

Common CSS Properties

CSS provides an abundant amount of styling properties to work with. Some common CSS properties are discussed in the table below.

CSS PropertyValueUsage
font-stylenormal / italic/ obliqueAllows for styling the font
font-weightnormal / boldAdds weight to the font
font-sizen pxSpecify an integer value of n to adjust font size
background-colorColor name or Hex code of colorAdds color to the background of element.
paddingn pxCreates a space around the HTML element.

Summary

This lesson only lists down the basics of HTML and CSS and does not go into depth about many other HTML tags and CSS properties. There are other options available for content creation and styling in both HTML and CSS. Try searching and experimenting with other common HTML elements and CSS properties to create beautiful websites!

One Pager Cheat Sheet

  • We will discover how to create websites using the programming languages, HTML and CSS, and learn about the technologies behind today's web pages.
  • The HTML document is linked to external style sheet (CSS) and script (JavaScript) files to enhance the style, appearance, and interactivity of a web page.
  • HTML elements and their appearance on web pages is managed by HyperText Markup Language (HTML) and Cascading Style Sheets (CSS).
  • HTML consists of variouselementsenclosed withintagsand sometimes further defined byattributesto provide additional information.
  • The basic HTML document is created using the <!DOCTYPE html>, <html>, <head> and <body> tags which define the document type, enclose all elements, set the metadata and display the visible content respectively.
  • Boldly structuring webpages with Headings, Paragraphs, Hyperlinks, and Images tags, HTML allows us to display content in a meaningful way.
  • The correct sequence of HTML tags to start a web page is <html>, <head>, <title>, and <body>.
  • CSS helps us to add color and life to a basic webpage by creating style sheets and applying rules to HTML elements to control their styling.
  • CSS is used to change the appearance of HTML elements by defining selectors and declarations within rules.
  • By adding a CSS stylesheet to an HTML document, multple properties of different elements can be easily changed to modify the appearance of the webpage.
  • CSS allows for the styling of font (e.g. font-style, font-weight and font-size), background color and padding of HTML elements, such as through specifying font-style, font-weight, font-size, background-color and padding with values in px.
  • This lesson introduces the basics of HTML and CSS, but there are many other HTML tags and CSS properties available that can be experimented with to create beautiful websites!