HTML and CSS#

Markup Languages#

Essentially, a markup language is a type of language that employs annotations or tags to dictate how a computer should process the text within those tags. The tags provide the computer with instructions on how to display or interpret the enclosed text. Many markup languages are designed to be easily readable by humans and are intended to be composed using simple text editors, not complex word processors such as MS Word. The present book employs a markup language known as Markdown. Markdown prioritizes human readability and simplicity.

HTML (Hyper-Text Markup Language)#

HTML, similar to other markup languages, aims to structure a document and provide content. Styling and aesthetics come later, with the introduction of CSS.

In HTML, tags are used to markup the document. They follow the format for opening and for closing, with the forward slash differentiating the closing tag from the opening tag.

An HTML document is essentially a sequence of these paired tags. Let’s take a look at a rudimentary HTML document:

<html>
    <head>
        <title>Sample Page</title>
    </head>
    <body>
        <h1>My First Heading</h1>
        <p>A straightforward paragraph of text.</p>
        <p>Another paragraph of text.</p>
    </body>
</html>

In this example, if you were to adjust the text within one of the paragraphs, or perhaps change the h1 tag to h2 (remember to adjust both opening and closing tags), you’ll see the changes reflected when viewing in a browser.

Indentation is used for clarity and does not affect how the browser interprets the HTML document. You could remove all indentation or place all code on one line, and the rendered page would remain unchanged.

As we delve into specific HTML tags, it’s beneficial to contemplate the overall structure of the webpage being built. One way to visualize this is through a “tree view” of the HTML page. In such a view, the html tag is the root of the tree, branching out to head and body tags as its children, which further subdivide into other tags. The tree view illustrates the hierarchical nature of an HTML document, a structure we’ll frequently revisit as we begin working with CSS and JavaScript.

HTML Document Structure#

Imagine an HTML document as a neatly organized office file. It has a clear structure, with different sections serving different purposes. Here’s how it looks:

    html
    Copy code
    <!DOCTYPE html>
    <html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
        <h1>My First Heading</h1>
        <p>My first paragraph.</p>
    </body>
    </html>

The declaration is like the label on the file - it tells the browser that this is an HTML5 document. The tag is the file itself, containing all the other elements. The is like the file’s index, containing meta-information about the document, while the is the main content of the file.

Basic HTML Tags#

Now, let’s explore the basic elements or ‘tags’ that make up the content of a web page.

  • Headings: Headings in HTML are like the chapter titles in a book, guiding the reader through the content. They range from <h1> for main headings, down to <h6> for the smallest subheadings.

  • Paragraphs: The <p> tag is used to define a paragraph. It’s like the sentences in a book, providing the main content.

  • Images: Images bring a web page to life! The <img> tag is used to embed an image in a web page. Remember to always include the alt attribute to provide a description of the image for screen readers.

  • Links: Links are the essence of the web, connecting one page to another. The <a> tag defines a hyperlink.

  • Text Formatting: HTML provides several tags for formatting text, including <b> for bold text, <i> for italic text, and <u> for underlined text.

Advanced HTML Tags#

As we delve deeper into HTML, we encounter more complex tags that allow us to create a variety of content structures.

  • Unordered lists, created with the <ul> tag, are like the bullet points in a presentation, allowing us to present information in a clear, concise format.

  • Ordered lists, created with the <ol> tag, are like the numbered steps in a recipe, providing a clear sequence of information.

  • Description lists, created with the <dl>, <dt> (description term), and

    (description data) tags, are like a glossary, providing a list of terms along with their descriptions.

  • Nested Lists: Nested lists are lists within lists, allowing us to create complex hierarchical structures.

HTML Attributes#

HTML attributes are like adjectives - they provide additional information about an element. They come in pairs and are included in the opening tag of an element. For example, the src attribute in an <img> tag provides the source URL of the image.

HTML5 Semantic Elements#

Semantic elements are like the signposts in a city, providing meaningful information about the content they contain. They help both the browser and the developer understand the structure of the content. Examples include <header>, <nav>, <main>, <article>, <section>, <aside>, and <footer>.

HTML Validation#

HTML validation is like a spell check for your code. It ensures that your HTML is written according to the standards set by the World Wide Web Consortium (W3C). This helps avoid potential issues with how different browsers interpret your code. Tools like the W3C HTML validator can be used for this purpose.

Best Practices#

Just like any language, HTML has its own set of best practices. Proper indentation and formatting make your code easier to read and understand. Comments can be used to explain your code, making it easier for others (and future you) to understand. It’s also important to ensure that your web content is accessible to all users, including those with disabilities.

Cascading Style Sheets (CSS) Basics#

If HTML is the skeleton of a web page, CSS is the skin and clothing that makes it visually appealing. CSS is a stylesheet language used to describe the look and formatting of a document written in HTML.

CSS was first proposed by Håkon Wium Lie on October 10, 1994. Since then, it has evolved into a powerful tool that allows developers to create beautiful, responsive, and user-friendly websites. It’s one of the three cornerstone technologies of the World Wide Web, alongside HTML and JavaScript.

CSS Syntax#

CSS is made up of “rules”, each consisting of a selector and a declaration block. The selector points to the HTML element you want to style, and the declaration block contains one or more declarations separated by semicolons. Each declaration includes a CSS property name and a value, separated by a colon.

Here’s an example of CSS syntax:

p {
    color: red;
    text-align: center;
}

In this example, p is the selector, and color: red; and text-align: center; are declarations. color and text-align are properties, and red and center are the values.

You can also add comments in CSS by wrapping the comment text with /* and */.

How to Include CSS#

There are three ways to include CSS in your HTML documents:

  1. Inline CSS: CSS is applied directly to the HTML elements using the style attribute. This method is not recommended as it mixes HTML with CSS, making the code harder to maintain.

  2. Internal CSS: CSS rules are included in the <head> section of the HTML document using the <style> tag. This method is useful for single-page websites.

  3. External CSS: CSS rules are written in a separate file with a .css extension, which is linked to the HTML document using the <link> tag. This method is the most common and recommended way to include CSS, especially for larger websites.

CSS Selectors#

CSS selectors are used to select the HTML elements you want to style. Here are some commonly used CSS selectors:

  • Element Selector: Selects elements based on the element type. For example, p selects all paragraph elements.

  • Class Selector: Selects elements based on the class attribute. For example, .myClass selects all elements with class="myClass".

  • ID Selector: Selects a specific element based on the id attribute. For example, #myID selects the element with id="myID".

Practical Example: Using External CSS#

Let’s put what we’ve learned into practice with a simple example. We’ll create an HTML file and a separate CSS file to style it.

First, let’s create an HTML file named index.html:

<!DOCTYPE html>
<html>
<head>
    <title>My First Styled Page</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <h1 class="centered">Hello, World!</h1>
    <p class="centered red">Welcome to my first styled web page.</p>
</body>
</html>

In the <head> section, we’ve included a link to our CSS file, styles.css, using the <link> tag.

Now, let’s create a CSS file named styles.css:

.centered {
    text-align: center;
}

.red {
    color: red;
}

In our CSS file, we’ve defined two classes: .centered and .red. The .centered class aligns the text to the center, and the .red class changes the text color to red.

When you open index.html in a web browser, you should see a centered heading saying “Hello, World!” and a centered paragraph in red saying “Welcome to my first styled web page.”

Intermediate Example: Styling a Blog Post#

Now, let’s take it a step further and style a simple blog post. We’ll use more complex selectors and properties in our CSS.

First, let’s create an HTML file named blog.html:

<!DOCTYPE html>
<html>
<head>
    <title>My Blog Post</title>
    <link rel="stylesheet" type="text/css" href="blog-styles.css">
</head>
<body>
    <header>
        <h1 class="centered">My Blog Post</h1>
    </header>
    <main>
        <article>
            <h2>Introduction</h2>
            <p>Welcome to my blog post. This is the introduction.</p>
            <h2>Content</h2>
            <p>This is the main content of my blog post.</p>
            <h2>Conclusion</h2>
            <p>This is the conclusion of my blog post.</p>
        </article>
    </main>
    <footer>
        <p class="centered">Thank you for reading!</p>
    </footer>
</body>
</html>

Now, let’s create a CSS file named blog-styles.css:

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f0f0f0;
}

header {
    background-color: #333;
    color: white;
    padding: 10px 0;
}

.centered {
    text-align: center;
}

main {
    margin: 15px;
}

article h2 {
    color: #333;
}

footer {
    background-color: #333;
    color: white;
    text-align: center;
    padding: 10px 0;
    position: fixed;
    bottom: 0;
    width: 100%;
}

In our CSS file, we’ve styled the body, header, main, article, h2, and footer elements, and used the .centered class to center text. We’ve also used the position: fixed; property to position the footer at the bottom of the page.

When you open blog.html in a web browser, you should see a styled blog post with a fixed footer.