Skip to content
Home » META » Meta Front-End Developer Professional Certificate » Introduction to Front-End Development » Week 2: Introduction to HTML and CSS

Week 2: Introduction to HTML and CSS

In this module you will learn how to construct HTML documents and add basic styling and layout using CSS.

Learning Objectives

  • Use HTML to create a simple web page
  • Use CSS to define style of a simple web page

Getting started with HTML


Video: What is Hyper Text Markup Language?

HTML documents are the basic building blocks of the web. They are text files that contain a specific structure that consists of elements and tags. HTML elements are defined by opening and closing tags, which are enclosed in angle brackets (< >). Most elements have content inside them, such as text or other elements. Some elements are self-closing, meaning they do not have a closing tag.

HTML standards are defined by the World Wide Web Consortium (W3C). The current version of HTML is HTML 5.

HTML elements build up an HTML document, which is then displayed by a web browser. CSS can be used to tell the browser how to display the web page.

Here are some key takeaways from the video:

  • HTML documents are the basic building blocks of the web.
  • HTML elements are defined by opening and closing tags, which are enclosed in angle brackets (< >).
  • Most elements have content inside them, such as text or other elements.
  • Some elements are self-closing, meaning they do not have a closing tag.
  • HTML standards are defined by the World Wide Web Consortium (W3C).
  • The current version of HTML is HTML 5.
  • HTML elements build up an HTML document, which is then displayed by a web browser.
  • CSS can be used to tell the browser how to display the web page.

What is Hyper Text Markup Language (HTML)?

HTML stands for HyperText Markup Language. It is the standard markup language for creating web pages. HTML uses a set of tags and attributes to define the structure and content of a web page. When a web browser encounters an HTML document, it interprets the tags and displays the document accordingly.

How does HTML work?

HTML documents are made up of elements. An element is a piece of content that is surrounded by opening and closing tags. For example, the following HTML code defines a paragraph element:

HTML

<p>This is a paragraph.</p>

The opening tag is <p>, and the closing tag is </p>. The text between the opening and closing tags is the content of the element.

In addition to elements, HTML also uses attributes. An attribute is a piece of information that provides additional information about an element. For example, the following HTML code defines an image element that has an attribute called src that specifies the URL of the image:

HTML

<img src="https://example.com/image.png" alt="This is an image." />

The src attribute tells the web browser where to find the image file. The alt attribute provides a text alternative for the image, which is displayed if the image cannot be loaded.

Why is HTML important?

HTML is the foundation of the web. It is used to create all of the web pages that you visit. HTML is also used to create email messages, e-books, and other electronic documents.

How to learn HTML

There are many resources available to help you learn HTML. You can find books, tutorials, and online courses that cover all aspects of HTML.

Here are some tips for learning HTML:

  • Start by learning the basics of HTML, such as the different types of elements and attributes.
  • Once you have a basic understanding of HTML, start practicing by creating simple web pages.
  • Don’t be afraid to experiment with different HTML tags and attributes.
  • Use a web browser’s developer tools to inspect the HTML code of existing web pages.
  • There are many online communities where you can ask questions and get help from other HTML learners.

Conclusion

HTML is a powerful language that can be used to create a wide variety of web pages and other electronic documents. By learning HTML, you can gain the skills necessary to create your own website or to work as a web developer.

You want to insert a paragraph on your website. Choose the correct format for the opening and closing paragraph tags:

Opening tag: <p> Closing tag: </p>

That’s correct! This is the format required for opening and closing paragraph tags.

When a team of builders begin
constructing a building, one of their key tasks is
to assemble the frame. It doesn’t matter
what building it is. It always starts with a
frame of beams and rafters. Once in place, the frame guides the rest of
the construction. If you compare a building
with a web page, an HTML document is a
lot like the frame, is a basic structure
assembled with different components such
as tags and elements. In this video, you
will learn about HTML documents and
the difference between HTML tags and elements. To begin, let me tell you a short story about
where it all started. Sir Tim Berners-Lee,
a physicist, often thought about how
he and his colleagues from around the world
would share information. He was so used to
browsing information online that it might seem a
silly thing to ponder about. Actually, that question
was very relevant at the time and started the
development of HTML, which you’d like to explore
the very first web page. I know just where to find it. The first web page
was created in 1999 by Sir Tim Berners-Lee. He worked at the European
Organization for Nuclear Research or CERN, and originally proposed HTML. The first version of
HTML was released in 1991 along with the first
web browser and web server. HTML stands for Hypertext
Markup Language. But what does that mean exactly? Hypertext is text which
contains links to other text. Markup refers to tags and elements used within a document. For now, let’s just focus on
some of the basics of HTML. HTML is simply a text file with a specific structure that
consists of elements and tags. Also take note that HTML files usually have a dot HTML suffix. For instance, when
you visit a website, the first page that is
returned to the browser is often called index.html. Now, let’s explore what
HTML tags and elements are. Each HTML element consists of an opening tag enclosed
in angle brackets. For example to
create a paragraph, you type a left angle bracket, the letter p for paragraph, and then a right angle bracket. Most elements are paired
with a closing tag, which has a forward slash off
to the left angle bracket. For example you close the paragraph element with
a left angle bracket, a forward slash the letter p
and a right angle bracket. HTML elements usually have
some content inside them. For example between the opening and closing tags of a paragraph, you add the text of the
paragraph you want to write. HTML elements can also
contain other elements, for example you can
add an italics element inside a paragraph element to make texts appear in italics. Elements can also be
empty or self-closing, meaning they do not have
a closing HTML tag. One example of a
self-closing element is the line break tag. You can add a line break
tag in a paragraph tag to move content to
the following line by typing left angle bracket, the letters br, then right angle bracket. At the end of a
self-closing tag, you simply add a
right angle bracket. You can also close the right angle bracket by typing a forward slash
right before it. Now that you know what
elements and tags are, you will explore what
HTML standards are. The rules and structure
for elements and tags are known as the
HTML specification. The HTML specification is maintained by the World
Wide Web Consortium, or W3C, as it is commonly known. Whenever the HTML
specification changes, a new version of HTML
is standardized, the current version is HTML 5. To summarize, HTML elements with their opening
and closing tags, and angle brackets build
up an HTML document. These elements form the
structure of a web page and describe to the web
browser what to display. For example the browser
reads an HTML page that has an image tags display an
image file called icon.png. Next, it reads a p tag to display a
paragraph under that image. But the browser reads
the HTML document and displays the web page to the
user in a very basic format. Now, if you want to tell the browser how to
display the web page, you should use CSS. You will learn more about
CSS in this course. By now, you have learned
how HTML tags are used to create elements that build the structure
of a web page. You’ll have an
opportunity to explore HTML in action in this course.

Video: HTML documents

This video teaches you how to create a simple webpage using HTML. The video starts by explaining the structure of a HTML document, which consists of the DOCTYPE declaration, the HTML tag, the head tag, and the body tag. The video then shows you how to add different elements to the body of the webpage, such as headings, paragraphs, and images. Finally, the video shows you how to save and view the webpage in a web browser.

Here are the key takeaways from the video:

  • HTML documents are made up of elements and tags.
  • The DOCTYPE declaration tells the web browser that the document is an HTML document.
  • The HTML tag is the root element of an HTML document.
  • The head tag contains information about the HTML document, such as the title and meta tags.
  • The body tag contains the content of the webpage, such as headings, paragraphs, and images.
  • You can use the H1 tag to create a main heading on the webpage.
  • You can use the H2 tag to create subheadings on the webpage.
  • You can use the paragraph tag to add text to the webpage.
  • You can use the image tag to add images to the webpage.

To save and view the webpage in a web browser:

  1. Save the HTML file.
  2. Open the HTML file in a web browser.

You can also right-click on the HTML file and select “Reveal in File Explorer” to open the file in the file browser. Then, double-click on the HTML file to open it in the web browser.

HTML Documents Tutorial

HTML, or HyperText Markup Language, is the standard markup language for creating web pages. HTML documents are made up of elements, which are defined using tags. Tags are enclosed in angle brackets (< >). For example, the following HTML code defines a paragraph element:

HTML

<p>This is a paragraph.</p>

The paragraph element will be displayed as a paragraph of text in the web browser.

There are many different types of HTML elements, each of which has a specific purpose. For example, the following HTML code defines a heading element:

HTML

<h1>This is a heading.</h1>

The heading element will be displayed as a heading in the web browser.

Other common HTML elements include:

  • img: Image element
  • a: Link element
  • ul: Unordered list element
  • ol: Ordered list element
  • table: Table element

You can use HTML elements to create a variety of different web pages, from simple websites to complex web applications.

Creating an HTML Document

To create an HTML document, you can use any text editor, such as Notepad or Sublime Text. However, it is recommended to use a code editor, such as Visual Studio Code, which is designed for writing code.

To create a new HTML document in Visual Studio Code, click on the “File” menu and select “New File”. In the “New File” dialog box, select “HTML” and click on the “Create” button.

Once you have created a new HTML document, you can start adding elements to it. To add an element, simply type the opening tag for the element, followed by the content of the element, and then the closing tag for the element.

For example, to add a paragraph element to your HTML document, you would type the following code:

HTML

<p>This is a paragraph.</p>

Saving and Viewing an HTML Document

To save your HTML document, click on the “File” menu and select “Save As”. In the “Save As” dialog box, select a location to save your file and click on the “Save” button.

To view your HTML document in a web browser, simply open the HTML file in the web browser.

Here is an example of a simple HTML document:

HTML

<!DOCTYPE <strong>html</strong>>
<html>
<head>
  <title>My First Web Page</title>
</head>
<body>
  <h1>This is my first web page!</h1>
  <p>This is a paragraph.</p>
</body>
</html>

This HTML document will create a web page with the title “My First Web Page” and the paragraph “This is a paragraph.”

Learning More About HTML

There are many resources available to help you learn more about HTML. You can find books, tutorials, and online courses that cover all aspects of HTML.

Here are some tips for learning HTML:

  • Start by learning the basics of HTML, such as the different types of elements and attributes.
  • Once you have a basic understanding of HTML, start practicing by creating simple web pages.
  • Don’t be afraid to experiment with different HTML tags and attributes.
  • Use a web browser’s developer tools to inspect the HTML code of existing web pages.
  • There are many online communities where you can ask questions and get help from other HTML learners.

Conclusion

HTML is a powerful language that can be used to create a wide variety of web pages and other electronic documents. By learning HTML, you can gain the skills necessary to create your own website or to work as a web developer.

The structure of an HTML document starts with:

<!DOCTYPE html>

That’s right! The first element in an HTML document is the DOCTYPE declaration which notifies the web browser that it is an HTML document. 

In this video, I
will guide you to the blueprints of
a HTML document. I will create a
very simple webpage for a restaurant
called Little Lemon. The great thing
about HTML documents is that you don’t need
the web to view them. Yes, they don’t have
to be hosted on a web server for you to view
them in your web browser. You can save HTML files on your computer and viewed them locally with your web browser, almost like eating
takeaways at home. By the end of this
video, you’ll be able to identify the structure of a HTML document and
create a basic webpage,. To create a HTML file
and Visual Studio Code. Right-click in the Explorer
panel and select New File. I’ll name it index.html. By now you know
that HTML documents are made up of
elements and tags. But before I can add
elements or tags, I first need to create a standard HTML structure which starts with the
DOCTYPE declaration. This notifies the web browser
that is a HTML documents. Next, I create the HTML tag, which is also known as
the HTML root element. Inside the HTML tag, I add two main elements. The head and the body elements. Is important to note
that nothing inside the head element is displayed on the webpage in
the web browser. In the head element,
you add information or metadata about the
HTML documents. It is not part of the
content of the webpage. For example, inside
the head tag, you always create
the title elements. This is the title
that is displayed in the web browser tab. I can also link to CSS files and define Meta tags in
the head section. Meta tags can, for instance, specify the description
of the web page, keywords for search engines and the order of the web page. Next up, I add the content of the webpage inside the body tag. The body can contain
elements such as headings, paragraphs, images, and videos. Let me start by adding
a main heading on the webpage by using the H1 tag. Inside the H1 tag, I typed the heading, Our Menu. That’s good. Now I need to
start creating my menu. I want to add two items. To do that, I add
two subheadings. For this, I use the H2 tag. The menu items in
the subheadings are falafel and pasta salad. In order to make sure
everything is correct, I open my index.html file in the browser
to check my webpage. But first I need to save
the file by pressing the keys Control and S
or if you’re on a Mac, Command and S. Now, I right-click on my
index.html file in the explorer panel and select
Reveal in File Explorer. If you’re not using Windows, this may be called revealing finder or reveal
in file browser. Now the file browser
opens and lists the file. I double-click on index.html and it opens in the web browser. Great, it’s all there. The title that displays
in the browser tab matches the title that I
added to the head section and the content is displayed in the browser window but
it is still a bit empty. I go back to Visual Studio Code and add a short
description for each dish. I do this by adding a paragraph tag below
each subheading H2. Let’s imagine a nice falafel. I type chickpea,
herbs, and spices. What goes into the
pasta salad again? Lettuce, vegetables,
and mozzarella. Yummy. I save the file again
to update the changes. Again, I open it
in my web browser. There you go. Now
the ingredients are also included in the menu. Some of the best websites
are websites about food. Whenever you need
some inspiration, search for food
website ideas online, you will be fascinators. Now that you know
some basic HTML tags, try to identify where they
were using those websites. There are many HTML tags that you can use
in your webpages. But now you’re familiar with the few elementary ones and I encourage you to
start practicing with them. Best of luck.

Reading: Simple HTML tags

Reading

Video: Linking documents

  • To link different HTML pages together, you use the anchor tag.
  • The anchor tag is enclosed in angle brackets and has an href attribute, which specifies the URL of the page you want to link to.
  • You can also add text to the anchor tag, which will be displayed as the link text on the web page.
  • To test if a link works, save the HTML file and open it in a web browser.
  • If the link is valid, the link text will be displayed in blue. If you click on the link, the browser will open the linked page.

Here is an example of an anchor tag:

HTML

<a href="location.html">Our Location</a>

This code will create a link with the text “Our Location” that links to the HTML file “location.html”.

You can link different HTML pages together to create your website by adding anchor tags to your HTML files. For example, you could add a link to the “location.html” file from the “index.html” file.

When you save and open your HTML files in a web browser, you will be able to click on the links to navigate between your web pages.

Linking documents in Front-end development

What is linking documents?

Linking documents is the process of connecting two or more web pages together. This is done using the anchor tag, which creates a hyperlink or link. When a user clicks on a link, they are taken to the linked page.

Why link documents?

There are many reasons why you might want to link documents in your front-end development projects. For example:

  • To create a website navigation system.
  • To link to related content on your website.
  • To link to external websites.
  • To create a more interactive and engaging experience for your users.

How to link documents

To link documents in your front-end development projects, you will need to use the anchor tag. The anchor tag is enclosed in angle brackets and has an href attribute, which specifies the URL of the page you want to link to. You can also add text to the anchor tag, which will be displayed as the link text on the web page.

For example, the following code will create a link to the page “location.html”:

HTML

<a href="location.html">Our Location</a>

You can add anchor tags to any HTML element, but they are most commonly added to paragraphs and headings.

Types of links

There are two main types of links:

  • Internal links: Internal links are links that point to other pages on your website.
  • External links: External links are links that point to pages on other websites.

Best practices for linking documents

Here are some best practices for linking documents in your front-end development projects:

  • Use descriptive link text. This will help users to understand where the link goes before they click on it.
  • Avoid using generic link text, such as “Click here” or “Learn more”.
  • Use contextually relevant link text. This means that the link text should be related to the content that it is linking to.
  • Avoid using too many links on a single page. This can be overwhelming for users and can also make your page look cluttered.
  • Make sure that all of your links are working properly. Test your links regularly to ensure that they are taking users to the correct pages.

Conclusion

Linking documents is an important part of front-end development. By following the best practices outlined above, you can create links that are informative, helpful, and easy to use.

You are creating a website and you want to add a link to the about.html page on your home.html page. What is the correct notation to create this link?

<a href = “about.html”>About</a>

Yes, that’s correct. The href attribute is inside the anchor tag.

By now you should know how
to build a basic webpage but how do you build a website? Remember, websites consists of
multiple webpages linked together. To link pages together. You use the anchor tag anchor
tag create hyperlinks or links as they are commonly known. In this video, you will learn how to link different web
pages together to create your own website. In my project folder. I now create a second webpage for Little Lemon that will contain
the address of the restaurants. I right click in the explorer
panel to create a new file and name it location.html. I use the same file structure
created in my index to HTML email but add new content. I add a new H one tag for
the heading and type our location. Next I add a paragraph tag and
insert the address 123 Rome Road,
Main Districts capital city. I press control and S to save the file or
command and S on Mac. Now that my new file with
the restaurant location is created. Let’s navigate back to index.html to
add a link from there to location.html. Below the last paragraph
tag I add the anchor or a tag followed by the href tribute or
hypertext reference. I typed the file name, location.html so
that my index.html file will link to it. Within a tag I can also add
a descriptive text that is displayed as a link name on my web page. So I type our location. The text our location will now display
on the web page in the browser as a clickable link. Let’s test see if it works. I saved the file to update it with the
changes and open it in my web browser by right clicking on the index.html file and
selecting reveal in file Explorer. If you’re not on Windows,
this may be called reveal in finder or revealing file browser. I then double click on the index file. The words are location are displayed
in blue by the web browser, success. If I click on the blue link,
the browser opens the location.html file. You can now link different HTML files
together to create your website. Best of luck.

Video: Adding images to a webpage with HTML

  • To add images to HTML documents, you use the <img> tag.
  • The <img> tag has two required attributes: src and alt.
  • The src attribute specifies the URL of the image file you want to add.
  • The alt attribute specifies an alternative text for the image, which will be displayed if the image cannot be loaded or for people using assistive technologies.
  • You can also use the width and height attributes to specify the dimensions of the image.

Here is an example of how to add an image to an HTML document:

HTML

<img src="falafel.jpeg" alt="A falafel">

This code will add an image named “falafel.jpeg” to the web page, with the alternative text “A falafel”.

Best practices for adding images to HTML documents:

  • Use descriptive alt text for all images. This will help users to understand what the image is about, even if they cannot see it.
  • Use the width and height attributes to specify the dimensions of the image. This will help to ensure that the image is displayed correctly on all devices.
  • Optimize your images for web performance. This means compressing the images to reduce their file size without sacrificing too much quality.
  • Use a consistent image style throughout your website. This will help to create a more polished and professional look.

Adding images to a webpage with HTML

To add images to a webpage with HTML, you use the <img> tag. The <img> tag is an empty element, meaning that it does not have any closing tag. It has two required attributes:

  • src: The URL of the image file.
  • alt: The alternative text for the image. This is displayed if the image cannot be loaded or for people using assistive technologies.

You can also add the following optional attributes to the <img> tag:

  • width: The width of the image in pixels.
  • height: The height of the image in pixels.
  • class: A CSS class that can be used to style the image.

To add an image to your webpage, simply place the <img> tag in the HTML code where you want the image to appear. For example, the following code would add an image named my-image.jpg to the webpage, with the alternative text “My image”:

HTML

<img src="my-image.jpg" alt="My image">

You can also use the <img> tag to create a link to another webpage. To do this, simply wrap the <img> tag in an <a> tag with the href attribute set to the URL of the webpage you want to link to. For example, the following code would create a link to the webpage https://example.com/ when the user clicks on the image:

HTML

<a href="https://example.com/"><img src="my-image.jpg" alt="My image"></a>

Here are some tips for adding images to your webpages:

  • Use descriptive alt text for all images. This will help users to understand the content of your webpages, even if they cannot see the images.
  • Optimize your images for web. This means compressing the images to reduce their file size without sacrificing too much quality.
  • Use a consistent image style throughout your website. This will help to create a more polished and professional look.

Example:

The following HTML code would add two images to a webpage:

HTML

<img src="image1.jpg" alt="Image 1" width="200" height="150">
<img src="image2.jpg" alt="Image 2" class="my-image">

The first image would be 200 pixels wide and 150 pixels high. The second image would use the my-image CSS class. You could then use CSS to style the my-image class however you want.

Conclusion

Adding images to your webpages is a great way to make them more visually appealing and engaging. By following the tips above, you can ensure that your images are displayed correctly and that your webpages are accessible to all users.

You want to add an image to your webpage. What should you add within the image tag? Please select all that apply.

The alt description

That’s correct! Adding alt descriptions is not required to add an image on your website but it is certainly best practice. This is helpful when the image becomes unavailable, for instance, if the image file is accidentally deleted. But, most importantly, it improves the accessibility of your website to those who are differently abled. Screen readers and other accessibility tools use the Alt attribute to provide information to their users. 

The link to the source file

That’s correct! Just like when linking to a file, you need to provide the link to the image. The code will be <img src= “example.jpg” width=”100″ height=”100″ />

The width and height specifications

That’s correct! The image will display at its actual size if you do not specify the dimensions in the img tag. The code will be <img src= “example.jpg” width=”100″ height=”100″ />

A picture is worth 1000 words as they say. I think you will agree that a website
without pictures wouldn’t provide a very good user experience. Especially websites about food. My example website about Little Lemon
could certainly use a few images. In this video you will learn how to add
images to HTML documents using the image or IMG tag as it is commonly known. I have to image files
in my project folder, one name falafel.jpeg and
another named salad.jpeg. Similar to what I did with document links, I have to specify the link to
the image file using the image tag. Images are not inserted into web pages. Instead we use image tags in
HTML to link to image files. The image tag then creates a placeholder
for the image on the web page. To add an image tag,
I type img and inside it I add the src attribute or
source attribute as it is known. I type falafel.jpeg As a source for one image, And salad.jpeg, As a source for the other image. Let’s save the file and
open it in the web browser. The images are displayed but
they are way too big. Luckily there are several ways
to set the size of an image but in this video I will just
demonstrate how to do this in HTML. Right after the sourcing in the image tag, you can specify
the dimensions of an image. I first add the width attribute,
And set it to 240 pixels. Then I add the height attribute and set it to 135 pixels. I’ll set the other image
to the same dimensions. Let’s save and
view it in the browser again. Much better, the images are smaller now. But there is one important attribute that
you should always include with image tags. It is good practice to add
a short description for images. It helps improve accessibility for
people using assistive technologies and it can also improve
search engine rankings. You can add image descriptions with the
alternative text attribute also known as the altar tribute. Still within the image tag after
the dimensions attributes you can add the altar tribute. I will type a short description,
A falafel and for the other image I will type,
A pasta salad. I will save the Html file again and
open it in the web browser. The all text is not displayed
anywhere on the site but will be read by assistive
technologies such as screen readers. Screen readers and other accessibility
tools use the altar tribute to provide information to their users. Now you know how to add images
to websites with the image tag. Little Lemon website is now ready for
everyone who uses the web. Have fun while you make your
sites colorful by adding images

Video: Use HTML to work with data in tables

  • To add a table to an HTML file, you use the <table> tag.
  • Inside the <table> tag, you add <tr> tags to create rows and <td> tags to create data cells.
  • You can add headers to the columns of a table by using the <th> tag.
  • You can use CSS to style tables.

Example:

The following HTML code would create a simple table:

HTML

<table>
  <tr>
    <th>Dish</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Falafel</td>
    <td>$10</td>
  </tr>
  <tr>
    <td>Pasta Salad</td>
    <td>$12</td>
  </tr>
</table>

The following CSS code would add a 1px border to the table:

CSS

table {
  border: 1px solid black;
}

Conclusion:

HTML tables are a great way to organize data on your webpages. By following the tips above, you can create tables that are both informative and visually appealing.

Use HTML to work with data in tables in Front-end development

HTML tables are a powerful tool for organizing and displaying data on web pages. They can be used to create everything from simple price lists to complex data visualizations.

To work with data in tables in Front-end development, you need to understand the basic structure of an HTML table and how to use the different HTML elements to represent your data.

Basic structure of an HTML table

An HTML table is made up of the following elements:

  • <table>: This element marks the beginning and end of the table.
  • <tr>: This element marks the beginning and end of a row in the table.
  • <td>: This element defines a data cell in the table.
  • <th>: This element defines a header cell in the table.

To create a table, you simply need to wrap your data in the appropriate HTML elements. For example, the following code would create a simple table with two rows and two columns:

HTML

<table>
  <tr>
    <th>Dish</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Falafel</td>
    <td>$10</td>
  </tr>
  <tr>
    <td>Pasta Salad</td>
    <td>$12</td>
  </tr>
</table>

Working with data in tables

Once you have created the basic structure of your table, you can start adding your data. To add data to a table cell, simply place the data between the opening and closing <td> tags. To add a header to a table column, place the header text between the opening and closing <th> tags.

You can also use HTML attributes to define additional properties for your table cells. For example, you can use the colspan and rowspan attributes to span a cell across multiple columns or rows, respectively.

Example:

The following code would create a table with two rows and three columns, where the first column spans both rows:

HTML

<table>
  <tr>
    <th colspan="2">Dishes</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Falafel</td>
    <td>Pasta Salad</td>
    <td>$10</td>
  </tr>
</table>

Styling tables with CSS

Once you have added your data to your table, you can use CSS to style it. For example, you can use CSS to change the font, color, border, and padding of your table cells.

Example:

The following CSS code would add a 1px border to all of the table cells in the previous example:

CSS

table {
  border-collapse: collapse;
  border: 1px solid black;
}

Conclusion

HTML tables are a powerful tool for working with data in Front-end development. By understanding the basic structure of an HTML table and how to use the different HTML elements to represent your data, you can create tables that are both informative and visually appealing.

Additional tips

  • Use descriptive table headings. This will help users to understand the data in your table.
  • Use consistent formatting for your table cells. This will make your table easier to read and scan.
  • Use CSS to style your tables. This will give you more control over the appearance of your tables.
  • Use accessible HTML markup. This will ensure that your tables are accessible to all users, including those with disabilities.
You are developing a website for a shoe shop. In order to add a table to your website, which tags can you use? Select all that apply.

The table data tags: <td> </td>

Correct. You need to add table data tags for each cell in each row.

The table row tag: <tr> </tr>

Correct. You need to add table row tags for each line of information in your table.

The table tag: <table> </table>

Correct. The table tag is the first tag that you need to insert in order to add a table to a webpage.

The table header tags: <th> </th>

Correct. You use the table header tags to add headings to the table which makes it easier to understand the data.

My website for the lemon
is coming on well, but some key information is
still missing. The prices. The best way to add
information like a price list is by
using an HTML table. You might be familiar
with columns and rows in spreadsheets software. HTML tables look similar
and allow you to neatly organize content
in rows and columns. Websites almost always have
information that needs to be organized in a way that makes it quick and easy to read. For example, the
schedule of an event, available sizes of
clothing items, or the specs of a camera. In this video, I will
demonstrate how to create a symbol table for the
prices of little lemon. I want to add a table
with the prices to the bottom of the
index.html page. So I already have it open. Still in the body elements
below the anchor elements, I add the table tag. Inside of it I add to table
row tags or tea or tags, as they’re commonly referred to. This will create a row
for each of the dishes. Now I need to add
table data tags or td tags so that I can
include the prices. Table data tags define the
contents of table cells. So I need to add two of
those inside each row, one for the dish and
one for its price. In the first row, I type Falafel inside the
first table data tag. In the second table data tag, I add the price, let’s say $10. I do the same with
the second row. I type Pasta Salad inside
the first table data tag, and a price of $12 inside
the second table data tag. Now I will save the file and
open it in my web browser. Great. The prices display at
the bottom of the webpage. However, the table is not displayed like a regular
table at the moment, but I will style it soon. To make it more
clear for my users, I’m going to add headers
to the columns now. To do this, I add a new row to the top of the table
with the table row tag. Then use the table header tag or th tag for the heading cells. I need to add two
table header tags. I type dish in the
first column heading, and price in the second heading. I save the HTML and refresh it again to check the
update in the browser. The information
that’s presented much clearer now that the
columns have headings, but it is not displayed
like a table, yet. I will add a bit of CSS to improve the styling
of the table. Don’t worry about the
details just yet. But I would like to add
a little bit of magic now to get you excited
about styling. I will add a style tag. I will add a one pixel
border to the table. Let’s save the file again
and open it in the browser. Now the table has a border
around all the cells. This is just a quick
method of styling a table. Later you will learn
how to do this using best practices with CSS. Congratulations.
You now know how to add tables to HTML files. I use tables extensively
and I am sure you will too.

Video: What are forms?

HTML forms are an essential part of the web. They allow users to interact with websites in a variety of ways, such as logging in, submitting contact information, and making purchases.

To create an HTML form, you use the <form> tag. The <form> tag has a number of attributes that you can use to specify how the form should behave, such as the HTTP method to use and the URL to submit the form data to.

Inside the <form> tag, you can add various input elements. The type of input element determines what kind of data the user can enter. For example, the text input type allows users to enter a single line of text, while the textarea input type allows users to enter multiple lines of text.

Other common input types include:

  • password: Allows users to enter a password that will be masked.
  • checkbox: Allows users to select one or more options.
  • radio: Allows users to select one option from a group of options.
  • select: Displays a drop-down list of options that users can select.
  • file: Allows users to select a file to upload.

Once you have added the input elements that you want to your form, you need to add a submit button. The submit button allows users to submit the form data to the server.

Here is an example of a simple HTML form:

HTML

<form action="/submit.php" method="post">
  <label for="username">Username:</label>
  <input type="text" name="username" id="username">

  <label for="password">Password:</label>
  <input type="password" name="password" id="password">

  <input type="submit" value="Submit">
</form>

This form will submit the user’s username and password to the /submit.php script on the server using the HTTP POST method.

Input types

There are a variety of input types that you can use in your HTML forms. Here is a brief overview of some of the most common input types:

  • Text: Allows users to enter a single line of text.
  • Password: Allows users to enter a password that will be masked.
  • Checkbox: Allows users to select one or more options.
  • Radio: Allows users to select one option from a group of options.
  • Select: Displays a drop-down list of options that users can select.
  • File: Allows users to select a file to upload.
  • Number: Allows users to enter a number.
  • Email: Allows users to enter an email address.
  • Date: Allows users to select a date.
  • Time: Allows users to select a time.
  • URL: Allows users to enter a URL.

Creating user-friendly forms

When designing your HTML forms, there are a few things you can do to make them more user-friendly:

  • Use clear and concise labels for your input fields.
  • Use the appropriate input type for each field.
  • Provide validation for your form data.
  • Make sure your submit button is clearly visible and easy to click.

By following these tips, you can create HTML forms that are both easy to use and effective.

What are forms in Front-end development?

Forms are a key component of front-end development. They allow users to interact with websites and web applications in a variety of ways, such as logging in, submitting contact information, and making purchases.

Forms are created using HTML and CSS. HTML is used to define the structure of the form, while CSS is used to style the form.

The basic structure of a form is as follows:

HTML

<form>
  <input type="text" name="username">
  <input type="password" name="password">
  <input type="submit" value="Submit">
</form>

This form will create two input fields: one for the user’s username and one for the user’s password. There is also a submit button that the user can click to submit the form.

Different types of form inputs

There are a variety of different types of form inputs that you can use, such as:

  • Text: Allows the user to enter a single line of text.
  • Password: Allows the user to enter a password that will be masked.
  • Checkbox: Allows the user to select one or more options.
  • Radio: Allows the user to select one option from a group of options.
  • Select: Displays a drop-down list of options that the user can select.
  • File: Allows the user to select a file to upload.
  • Number: Allows the user to enter a number.
  • Email: Allows the user to enter an email address.
  • Date: Allows the user to select a date.
  • Time: Allows the user to select a time.
  • URL: Allows the user to enter a URL.

Form validation

Form validation is the process of checking to make sure that the data that the user has entered into a form is valid. This can be done using JavaScript or a server-side language such as PHP or Python.

For example, you could use JavaScript to validate that the user has entered a valid email address into the email address field.

Submitting form data

When the user submits a form, the form data is sent to the server. The server can then process the form data and take appropriate action, such as logging the user in or sending them a confirmation email.

Best practices for creating forms

Here are a few best practices for creating forms:

  • Use clear and concise labels for your input fields.
  • Use the appropriate input type for each field.
  • Provide validation for your form data.
  • Make sure your submit button is clearly visible and easy to click.
  • Make your forms responsive so that they look good and function well on all devices.

Conclusion

Forms are a key component of front-end development. They allow users to interact with websites and web applications in a variety of ways. By following the best practices outlined above, you can create forms that are both user-friendly and effective.

You are building an e-commerce site for a client. What kind of input types could you use? Check all that apply.

Text, Email, Checkbox, Password, Radio, Number, Textarea

Text, password, checkbox, radio, textarea, email and number are all types of input.

Today you can order almost
anything directly to your home. But did you know that one of the
functionalities that makes online shopping possible is HTML forms. Without them, you would not be able to enter your
credit card details during checkout. But forms are not limited to e commerce. You can use forms practically
every day on the internet. When you log into your favorite website,
you do it using a form. In this video, I’ll take you through
the steps to create a form and introduce you to a few
different input types. But first,
let’s briefly explore how forms work. To put it simply when a user enters
data on a website an HTML form submits an HTTP request containing
the data to the server. But how do you create
a form in the first place? You define forms with the html form tags. Forms also have an optional
form attribute called action. Actions specifies the URL or path that
the form should submit the request to. When the action attribute
is not specified, it submits the request to the same
path as the current web page. There is also the FORM method, with the FORM method you can specify the
HTTP method to use for the HTTP request. There are two HTTP methods to
submit the form data, GET and POST. Remember the GET HTTP method retrieves
information from the server. The POST HTTP method
sends data to the server. When a user submits a form,
one of these HTTP methods is used. We’ll explore how this is
done on a later course. Now let’s explore how to add fields
that the user can enter data into. First, let’s add a simple text field. For example, a user name field. The fields in a form
are specified by input tags. Note that the input tag does
not need a closing tag. But if I want to, I could close the tag by adding a forward
slash before the end of the input tag. When I use the text input type,
it displays a text field on the web page. But a text box on its own
isn’t very user friendly, so I will now add a label above it. By adding the label tag, the form will now display the word
user name above the input field. Next, let’s add a password field. In this case I won’t use the text input
type since I don’t want the password to be visible. HTML has an input type specific for passwords that will mask
the user’s input data. The user name will be visible and
the password will be masked. Finally, I need to add a button so
the user can submit the form. So I used the input type, submit. When the user clicks the submit button, the HTTP request containing the form
content will be sent to the web server. In summary, you use the form tag at the
start of a form where after you indicate the method that should be used. You can create labels for
input types with the label tag, you use the text input type for
a text input like a user name. For a password,
you use the password input type. To add a submit button to a form,
you use the submit input type. There are many different
input types available. For example,
you can add checkboxes to a form. To do this,
you use the checkbooks input type. Each box can be checked or unchecked. You use the name and value attributes
to configure how the data is sent to the server, you will explore this
in more depth in a later course. Radio buttons on the other hand
are like checkboxes except only one button can be
checked in on the group. Checking one radio button will
uncheck all the other radio buttons. There are also other types of inputs
such as the number, email and file upload types. However, some input fields
do not use the input tags. For instance the multi line text field. The text input type that I
showed you earlier is only for single line text content. To allow users to enter
multiple lines of text, I need to use the text area tag
instead of text input type. Another type of input that does not use
the input tag is a drop down list which contains items that the user can select. This time, you use the select tag and for
the items on the list you use option tags. You now know how HTML forms work and
the different input types, developers use. You covered form tags such as text,
password, checkbox, radio, text area and select. I can’t imagine my life
without online shopping. Next time you order something online,
notice all the types of input the form includes and how you,
as a user interacts with it. You might notice that the user experience
of some forms is better than others. I’m sure that you are now able to
choose appropriate input types for the forms that you create.

Video: Introduction to the DOM

The Document Object Model (DOM) is a tree structure that represents the HTML elements in a web page. It is used by JavaScript to interact with the page and make it dynamic.

How the DOM is created

When a web browser loads an HTML page, it constructs a DOM to represent it. The DOM is a tree structure, with the HTML element at the root and all other elements nested below it.

How JavaScript interacts with the DOM

JavaScript can access and modify the DOM to change the content and structure of a web page. For example, JavaScript can be used to:

  • Update the text of a heading
  • Add or remove elements from the page
  • Change the style of an element
  • Animate elements on the page

Common DOM and JavaScript uses

  • Accessing and modifying HTML attributes and content
  • Responding to user actions such as clicking, mouse over, and scrolling
  • Adding and removing DOM objects
  • Animating HTML elements

Example

The following JavaScript code updates the text of a heading on a web page:

JavaScript

// Get the heading element
const heading = document.querySelector('h1');

// Change the text of the heading
heading.textContent = 'New heading text';

This code will find the first h1 element on the page and change its text to New heading text.

Conclusion

The DOM is a powerful tool that allows JavaScript to interact with web pages and make them dynamic. By understanding the DOM, you can create more complex and interactive web applications.

Introduction to the DOM

The Document Object Model (DOM) is a tree-structured representation of an HTML document. It allows JavaScript to access and modify the document content, structure, and style.

How the DOM is created

When a web browser loads an HTML page, it constructs a DOM to represent it. The DOM is a tree structure, with the HTML element at the root and all other elements nested below it.

Each node in the DOM represents a single HTML element. The node’s type indicates the type of element it represents, and its attributes represent the element’s attributes.

For example, the following HTML code:

HTML

<h1>This is a heading</h1>

would be represented by the following DOM tree:

<html>
  <head>
    <title>My Web Page</title>
  </head>
  <body>
    <h1>This is a heading</h1>
  </body>
</html>

How JavaScript interacts with the DOM

JavaScript can access and modify the DOM using the document object. The document object is the root node of the DOM tree.

To access a node in the DOM, JavaScript can use the querySelector() or querySelectorAll() methods. These methods return a reference to the first or all nodes that match the specified selector.

Once JavaScript has a reference to a node, it can modify its content, attributes, and style. For example, the following JavaScript code would change the text of the heading in the previous example:

JavaScript

const heading = document.querySelector('h1');
heading.textContent = 'New heading text';

Common DOM and JavaScript uses

JavaScript can use the DOM to perform a variety of tasks, including:

  • Changing the content of a web page
  • Adding and removing elements from a web page
  • Changing the style of elements on a web page
  • Responding to user events, such as clicks and key presses
  • Creating animations

Example

The following JavaScript code adds a new paragraph element to the end of the body of an HTML document:

JavaScript

const body = document.querySelector('body');
const paragraph = document.createElement('p');
paragraph.textContent = 'This is a new paragraph.';
body.appendChild(paragraph);

This code will create a new paragraph element with the text This is a new paragraph. and append it to the end of the body of the document.

Conclusion

The DOM is a powerful tool that allows JavaScript to interact with web pages and make them dynamic. By understanding the DOM, you can create more complex and interactive web applications.

Additional resources

  • The Mozilla Developer Network (MDN) has a comprehensive documentation on the DOM: https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model
  • W3Schools also has a good introduction to the DOM: https://www.w3schools.com/js/js_htmldom.asp
True or false. The Document Object Model allows you to update all HTML elements on a web page.

True

That’s correct. The DOM allows you to update and delete existing elements on the web page.

Imagine your favorite social media site. If it was just an HTML document,
users would be able to scroll the page, look at pictures and read text but they
wouldn’t be able to log in like posts or get notifications for new messages. In order to allow users to do these
things, they need to be able to interact with objects on the page such as
the reaction icons or the comment button. Let me put it differently,
an HTML document must be represented in a certain way, so that JavaScript
code can query and update it, to do this we use
the document object model. In this video, I will unpack what
the DOM is, how it is created and how JavaScript interacts with
the DOM to make sites dynamic. When your web browser receives an HTML
page, it constructs a DOM to represent it. DOM stands for Document Object Model and
it is simply a tree, structure or model of the objects in your HTML file. To understand the DOM, I want you to think
of a simple HTML page, remember that an HTML document has opening and closing
tags for the document and the head. And inside the head tags you have
title tags, then you have the body and inside the body there might
be elements such as div tags. Okay, now, let me guide you through
how a DOM would be generated, the DOM has a series of objects each
representing a single HTML element. At the root of the DOM is the html object
and it contains the head and body object. From there, the head object
houses the title object and the title object contains its text object. The body object contains the two
div objects, the first div houses, the h1 object which again
houses its text object. The second div object contains the
paragraph object which contains its text object. In summary, all the elements in the HTML
file are represented as objects in the document object model. I just took you through
a very simple webpage, webpages typically have
hundreds of elements. Can you imagine how complex the DOM of
a highly interactive modern web page is? You as a developer can use
JavaScript to access and modify the DOM to make
your webpages dynamic. In fact, in a later course you
will learn how to access and modify the structure of the DOM and
its elements in different ways. Now that you know what the DOM is and
how it is referenced, let me briefly explain common DOOM and
JavaScript uses. For each element, you can access the HTML
attributes and content, this means you can update the existing content that
is displayed in the web browser. For instance, you could write some
JavaScript to update the seconds, minutes and
hours of a digital clock on a website. Or if you develop a movie streaming
website, you can write code that responds to user actions such as clicking
mouse over scrolling and so on. In this way you can allow a preview
of the movie to play when the user hovers the mouse
over the movie’s poster. Or you can think of a log in page,
when users click the Login button, you can disable the button so
that it cannot be clicked again. The web application would bring
them to a new web page or would re-enable the button,
if a log in error occurred. You can even add DOM objects to
dynamically add new HTML content to a live web page. For instance, you can add an error message
to a form if the user inserts invalid data, DOM objects can also
be deleted which will remove the corresponding HTML
displayed in the browser. Think of a to-do list on a website, you can remove an item from the list
when the user clicks on it. Another major use of JavaScript and
the DOM is to animate the HTML elements. This can be quite complex
depending on the animation but there are many libraries available
that aim to simplify this. For example, when a page first loads,
you can fade in the page contents or when a user receives an instant message, a notification can pop
up in the web browser. Many JavaScript libraries and
frameworks rely on the DOM, one of these libraries
is the react library. By now, you know what the DOM is, how it
is constructed and the wonderful things web developers can do with it to
create modern interactive websites. Next time you use your favorite social
media site think about how the DOM is working behind the scenes to change
the color of that like button.

Video: Web accessibility

Web accessibility is the practice of making websites usable for all people, including those with disabilities. It is important for web developers to consider accessibility when building websites to ensure that everyone can access and enjoy the internet.

The Web Accessibility Initiative (WAI) was launched in 1997 to develop standards and resources for web accessibility. The WAI’s Web Content Accessibility Guidelines (WCAG) are the international standards for web accessibility.

Web accessibility is becoming a requirement in many industries. For example, the European Union has approved the Web Accessibility Directive, which requires websites and mobile applications of public sector bodies to be accessible to those with disabilities.

Examples of how accessibility is provided to those with different disabilities:

  • Screen reader software: Screen readers read the content of websites and everything that is happening on the device. Screen readers are used by blind and vision impaired users, as well as by those with reading or learning difficulties.
  • Speech recognition software:Speech recognition software can turn spoken words into computer commands or dictate inputting text. This is useful for people who may not be able to use a mouse or keyboard due to physical or neurological disabilities.
  • Subtitles and video scripts: Subtitles and video scripts provide assistance to those with audio and visual disabilities.

Steps web developers can take to improve accessibility:

  • Think about accessibility from the beginning of a project: It is much easier to make a website accessible from the beginning than to retrofit it later on.
  • Use the correct HTML structure and appropriate elements: This will provide a good foundation for accessibility. For example, having text that is not contained within proper tags like paragraph or heading tags makes it harder for assistive technologies to interact with the content.
  • Use the WAI-ARIA specification: The WAI-ARIA specification outlines different techniques to improve accessibility for complex web apps.

Web accessibility: A tutorial

Web accessibility is the practice of making websites usable for all people, including those with disabilities. It is important for web developers to consider accessibility when building websites to ensure that everyone can access and enjoy the internet.

Here is a tutorial on web accessibility:

  1. Understand the different types of disabilities. There are many different types of disabilities that can affect how people interact with the web. Some common disabilities include:
    • Visual disabilities (e.g., blindness, low vision)
    • Hearing disabilities (e.g., deafness, hearing loss)
    • Cognitive disabilities (e.g., dyslexia, autism)
    • Physical and motor disabilities (e.g., cerebral palsy, quadriplegia)
    • Speech disabilities (e.g., aphasia, dysarthria)
  2. Learn about assistive technologies. Assistive technologies are devices and software that help people with disabilities use the web. Some common assistive technologies include:
    • Screen readers: Screen readers read the content of websites aloud to users who are blind or have low vision.
    • Magnifiers: Magnifiers enlarge the text and images on a web page to make them easier to see.
    • Speech recognition software: Speech recognition software allows users to control their computer using voice commands.
  3. Follow web accessibility guidelines. The Web Accessibility Initiative (WAI) has developed a set of guidelines called the Web Content Accessibility Guidelines (WCAG). The WCAG provide guidance on how to make web content accessible to people with disabilities.

Here are some specific tips for improving web accessibility:

  • Use the correct HTML structure and elements. This will help assistive technologies understand the content of your website. For example, use heading tags to indicate the structure of your text and list tags to indicate lists.
  • Provide alternative text for images. Alternative text is a text description of an image that is read by screen readers.
  • Use captions and transcripts for videos. Captions are text that appears at the bottom of a video and describe what is happening in the video. Transcripts are text versions of the audio content of a video.
  • Make sure your website is navigable using a keyboard. This is important for users who cannot use a mouse.
  • Use color contrast. Make sure that the text on your website is easy to read against the background color.
  • Avoid using blinking or flashing content. This can be disorienting for users with certain disabilities.

Testing for accessibility

Once you have made your website accessible, it is important to test it to make sure that it is usable for people with disabilities. There are a number of different ways to test for accessibility, including:

  • Using an assistive technology: Use a screen reader or other assistive technology to test your website.
  • Using an accessibility checker: There are a number of different accessibility checkers available online.
  • Hiring an accessibility expert: If you are unsure whether your website is accessible, you can hire an accessibility expert to test it for you.

Which of the following technologies are examples of assistive technologies? Select all that apply.

Screen reader software

Well done. Other assistive technologies include subtitles in videos and speech recognition software.

Subtitles in videos

Well done. Other assistive technologies include screen reader software and speech recognition software.

Speech recognition software

Well done. Other assistive technologies include subtitles in videos and screen reader software.

as a web developer you should try to
ensure that everyone can access and enjoy the internet almost like
an architect that needs to design a building with ramps and rails so
that it is accessible by all. You need to build a website in
a certain way to improve accessibility. When the Web accessibility Initiative or
W. A.
I. was launched in 1997. The creator of the World Wide Web. Sir, tim Berners lee said the power of
the Web is in its universality access by everyone regardless of
disability is an essential aspect. Web accessibility aims
to allow people with disabilities to understand navigate and
interact with websites. A common misunderstanding is that it
refers to visual disabilities but in fact it includes all disabilities
that affect interaction with websites such as audio and
visual disabilities, cognitive and neurological disabilities and
physical and speech disabilities. The W three C. Web accessibility initiative
developed specifications and supporting resources for accessibility. These are considered international
standards for web accessibility. In fact web accessibility is becoming
a requirement in many industries In 2016. The European Union approved the web
accessibility directive which requires websites and mobile applications of public
section bodies to be accessible to those with disabilities. Let’s explore some examples of how
accessibility is provided to those with different disabilities. People living with disabilities often use
assistive technologies to aid them in browsing the web screen reader software
can read the content of websites and everything that is
happening on the device. Screen readers are used not only by
blind and vision impaired users but also by those with reading or
learning difficulties. Speech recognition software can turn
spoken words into computer commands or dictate inputting text. This is useful for people who may
not be able to use a mouse or keyboard due to physical or neurological
disabilities, subtitles and video scripts and videos provide assistance to
those with audio and visual disabilities. There are many steps that you can
take to improve accessibility. It is good practice to think of
accessibility from the beginning of a project, it is a lot harder to rework
the project to be accessible later on. If you use the correct html structure and
appropriate elements as a foundation, it will greatly improve accessibility in this
course you will learn to structure your html properly and follow best practices
to support assistive technologies for example having text that is not contained
within proper tags like paragraph or heading tags makes it harder for assistive
technologies to interact with the content. Similarly using multiple line
breaks to break up text and add space also presents
barriers to accessibility. These are just simple examples
to improve web accessibility. However, in the future you’ll probably
develop very complex user interfaces and to make them accessible may
not be as straightforward as just having correct html. Fortunately one of the tasks of the W AI
is to define the accessible rich internet application or aria. Specification. The aria specification outlines different
techniques to improve accessibility for complex web apps. Now, you know about different assistive
technologies and that you should use the correct html structure and appropriate
elements to improve accessibility.

Practice Quiz: Knowledge Check – Getting started with HTML

An HTML document begins with ______________.

To display a link to another HTML document, the ______________ tag is used.

To add an image to a webpage, the ______________ tag is used.

To represent the HTML document in JavaScript, the browser builds _____________.

Which of the following answer choices improves web accessibility for people with disabilities? Select all that apply.

CSS Basics


Video: Selecting and styling

CSS is the language that tells web browsers how to display HTML elements on screen. It is made up of selectors, which indicate which HTML elements to style, and declaration blocks, which define the properties of those elements. Properties are specific attributes of an element, such as color or font size, and values are the specific settings for those properties. A CSS rule consists of a selector and a declaration block.

Here is an example of a CSS rule that changes the color of all h1 elements to gray:

CSS

h1 {
  color: gray;
}

Here is an example of a CSS rule that changes the color of an h1 element with the ID “header-one” to green:

CSS

#header-one {
  color: green;
}

CSS precedents and specificity determine which CSS rule applies to an element when there are multiple rules that could apply. The most specific rule will always be used.

Introduction to Selecting and Styling in CSS

CSS (Cascading Style Sheets) is a language that describes the presentation of web pages, including colors, layout, and fonts. It works in conjunction with HTML, the language that defines the structure of web pages. CSS rules are used to select HTML elements and apply styles to them.

Selectors

A selector is a pattern that identifies one or more HTML elements. There are several types of selectors, including:

  • Element selectors: Select all elements of a specific tag type, such as h1, p, or div.
  • ID selectors: Select an element with a specific ID attribute, such as #header or #footer.
  • Class selectors: Select elements with a specific class attribute, such as .button or .nav-item.
  • Descendant selectors: Select elements that are descendants of other elements, such as p a or ul li.
  • Child selectors: Select elements that are direct children of other elements, such as div > p or ul > li.

Declaration Blocks

A declaration block is a set of one or more declarations that define the styles of an HTML element. Each declaration consists of a property and a value, separated by a colon. For example, the following declaration sets the color of an element to red:

CSS

color: red;

There are many different CSS properties that can be used to style HTML elements. Some common properties include:

  • color: Sets the color of text.
  • font-family: Sets the font family of text.
  • font-size: Sets the font size of text.
  • background-color: Sets the background color of an element.
  • padding: Sets the padding around an element.
  • margin: Sets the margin around an element.

Combining Selectors and Declaration Blocks

CSS rules are combined to style different parts of a web page. For example, the following rule selects all h1 elements and sets their color to red, while the following rule selects all p elements and sets their color to blue:

CSS

h1 {
  color: red;
}

p {
  color: blue;
}

Specificity

When multiple CSS rules apply to an element, the browser will use the rule with the highest specificity. Specificity is a measure of how precise a selector is. The more specific a selector is, the higher its specificity.

Here is the order of precedence for CSS selectors:

  1. ID selectors
  2. Class selectors
  3. Attribute selectors
  4. Descendant selectors
  5. Child selectors
  6. Element selectors

Examples

Here are some examples of CSS rules:

CSS

/* Select all h1 elements and set their color to red. */
h1 {
  color: red;
}

/* Select all p elements with the class "paragraph" and set their font size to 16px. */
.paragraph {
  font-size: 16px;
}

/* Select all div elements that are descendants of div elements with the class "container" and set their background color to gray. */
div div.container {
  background-color: gray;
}

Practice

I encourage you to practice selecting and styling HTML elements with CSS. There are many resources available online, such as tutorials and CSS playgrounds, that can help you get started.

I’d like you to think of a physical
building and its structure, interior, exterior and
decorations in a similar fashion. When it comes to websites and web apps you
can think of html as being the frame and structure. CSS, however, is the paint,
wallpaper, fixtures, artwork and overall style or look and feel. In other words, CSS tells the web browser
how to display html elements on screen. When using CSS, there are five
elements you need to understand. A declaration block starts
with a left curly bracket and ends with the matching
right curly bracket. In between these curly brackets
are the style declarations. Let’s cover these elements one by one. The first element of the CSS
rule is the selector. This indicates which html element or
elements we want to style. For example, you can create a rule that
uses the h1 selector to change the color of all heading one tags
in a web page to gray. In this case, hi is the selector. Color is property and
gray is the value of the property. Now let’s focus on the structure
of the declaration block. Each declaration is made up of
two parts a property and a value. For example, in this case the properties
that you want to change are the color and the background color of the header or
selector. Once you have decided which
properties you want to define, then you must assign
each property of value. The color property gets
a value of blue and the background color property
gets a value of gray. This will result in all heading one tags
displaying blue text on a gray background in the browser. By now, you have learned that CSS
tells the web browser how to display html elements on screen. Now, let’s work through
an example to change a set of heading elements using
different CSS rules. I will share a pro tip
demonstrating how to install and use the VS code extension live
preview to speed up your workflow. Okay, so
I’m back in visual studio code here. Did you know that you can add additional
functionality by using extensions. If you click on the extensions tab, you can install the live
preview extension by Microsoft. If you are running VS code locally,
you can install and use this extension. Once installed, right click on your
html file in the explorer panel and select live preview, show preview. When you update an html file, you immediately notice the changes
in visual studio code. In this video, I will demonstrate
how to create a CSS file and style some html elements. Before I start styling elements, let’s create a simple html
document named index.html. To demonstrate how CSS works,
I will use an example created previously. Next I create a file named style.CSS. For my web page to use the CSS file, I need to link it to the head
element of the html file. To link to a style sheet, you use the link tag which must be
assigned to attributes rel and h ref. The rel attribute is
assigned to style sheet and the href attribute is assigned to
the name of the style sheet file. In this example, it’s style.CSS. I am now ready to apply CSS to
the index.html file using style rules that I will write
in the style.CSS file. The selector I’m creating applies
the CSS rule to all h1 elements. This type of selector is
called an html type selector. The properties in the declaration
block allow me to change how the selected elements are displayed. For example, I’m setting the color property of all the
h1 elements to display text and purple. I save the file using Ctrl nd S, notice
that this change occurs immediately, and there is no need to
refresh the webpage. So now what if I want to only
style a single h1 element? I can add an ID attribute to
the tag h1 that I want to style. And in my CSS file,
I create a rule for that ID. Let’s do that now. First, I create an ID rule using the hash
symbol and then the name header one. Then I define the CSS properties of that
rule to change the color value to green. I apply the rule and
save the file using Ctrl and S. Notice that the text for the heading,
chapter one on the web page turns green. You may be wondering, why does the first CSS rule not
apply to the h1 element with the ID? This is because of something called
CSS precedents and specificity. This is a complex statement, but basically the browser will use the most
precise selector for an html element. CSS has a set of hierarchy rules
which dictate which rule will apply to an element. In this example, the ID rule takes precedence over
the html type selector rule. You can learn more about
CSS selector rules and precedents from the additional
reading at the end of this lesson. Congratulations, you have now learned
about the selection and styling in CSS. Let’s quickly recap. After choosing a selector for your styles, you create a declaration block using
opening and closing curly brackets. Inside of the code block,
you write a declaration which consists of a property value pair
ending in a semi colon. And all these parts together. The selector code block and
declaration make up a CSS rule. I encourage you to add some
CSS rules to your html documents to practice selecting and
styling.

When you create a CSS rule, the part inside the curly brackets is called the:

Declaration block

That’s right. Everything between the curly brackets is called the declaration  block.

Reading: Different types of selectors

Reading

Reading: Text and color in CSS

Reading

Video: Box model introduction

The box model is a CSS concept that defines how an HTML element is displayed on a web page. It consists of four parts: content, padding, border, and margin.

  • Content: The actual content of the element, such as text or an image.
  • Padding: A transparent area around the content.
  • Border: A line around the padding.
  • Margin: A transparent area around the border.

The size of an element is determined by the size of its content, padding, border, and margin. The box model can be used to create appealing layouts and allow users’ eyes to easily flow between content.

Here are some key takeaways from the text:

  • The width and height of the content are determined by the content itself.
  • The padding extends the content size.
  • The border goes around the padding and content.
  • The margin extends the border area to separate the element from its neighboring elements.
Demystifying the Box Model: A Comprehensive Guide to CSS Layout

In the realm of web development, the box model stands as a fundamental concept, governing the layout and presentation of HTML elements. It serves as a conceptual framework that defines how elements are displayed on a web page, encompassing four distinct components: content, padding, border, and margin.

Understanding the Box Model Components

Content: The heart of an element, the content represents the actual information it contains, such as text, images, or other media elements. Its dimensions, known as content width and content height, determine the core size of the element.

Padding: Padding introduces a transparent area surrounding the content, creating a visual separation between the element’s boundaries and its inner content. Padding properties, such as padding-top, padding-right, padding-bottom, and padding-left, control the thickness of this transparent space.

Border: Encasing the padding and content, the border forms a visual outline around the element. Its size, defined by border-width properties, establishes the thickness of the border line. Border styles, such as solid, dashed, or dotted, determine the appearance of the border.

Margin: Extending beyond the border, the margin creates a transparent area separating the element from its neighboring elements. Margin properties, including margin-top, margin-right, margin-bottom, and margin-left, control the width of this transparent space.

The Box Model in Action: Calculating Element Dimensions

The box model plays a crucial role in determining the overall dimensions of an element. By understanding the interplay between its components, developers can precisely control the layout of their web pages.

Content Box: The content box encompasses the content area, encompassing the content itself and any padding applied to it. Its width and height are determined by the content width and content height properties.

Border Box: The border box expands upon the content box, incorporating the border as well. Its dimensions are calculated by adding the border thickness to the content box dimensions.

Margin Box: Enclosing the border box, the margin box represents the element’s outer boundaries, including the surrounding margin. Its width and height are determined by adding the margin width to the border box dimensions.

Practical Applications of the Box Model

The box model serves as a versatile tool for achieving various layout goals in web design. Here are a few practical examples of its applications:

Spacing Elements: By adjusting padding and margin properties, developers can control the spacing between elements, creating balanced and visually appealing layouts.

Creating Call-to-Action Buttons: The box model can be used to design visually distinct call-to-action buttons that stand out from the surrounding content.

Form Design: Padding and margin properties can be employed to ensure adequate spacing between form elements, enhancing user experience and readability.

Mastering the Box Model: Essential Tips

Embrace Shorthand Properties: CSS offers shorthand properties for padding, border, and margin, allowing developers to define multiple values in a single rule.

Understand Box Model Calculations: Familiarity with the calculations for content box, border box, and margin box dimensions is essential for precise layout control.

Practice with Real-World Projects: Practical experience gained from working on actual web projects will solidify understanding and proficiency with the box model.

Conclusion: The Box Model as a Fundamental Tool

The box model stands as a cornerstone of CSS layout, providing a structured framework for controlling the presentation and positioning of HTML elements. By understanding its components and practical applications, developers can create visually appealing and user-friendly web pages. Embrace the box model and elevate your web development skills to new heights!

You are busy designing a web page for a small company. They supplied you with an image that needs to be in the center on the landing page. The image has a content width of 100px, 10px padding on both left and right sides, a 10px border on both left and right sides and a 10px margin on both left and right sides. What is the border box width?

140px

Well done. Border box width = content width (100) + padding width (2×10) + border width (2×10)

Just like a busy day
at a restaurant, a website can easily
look squashed and busy, with waiters having to
maneuver their way around and customers chairs up against
each other, it can get messy. Good restaurants usually have carefully planned
layouts to make customers feel comfortable and allow waiters to move
around with ease. Sometimes creative
web development requires thinking
inside the box. In this video, I
will demonstrate how to use the box model in CSS to create an appealing
layout and allow users eyes to easily
flow between content. When an HTML document and CSS
style sheet are downloaded, the web browser needs to know how to display the
elements on the screen. To do this, it allocates a rectangle or box
to each element. CSS rules are applied to
the boxes of the elements. This is known as the box model. Every box consists
of four parts. The content, the padding, the border, and
finally, the margin. The content is the actual
content of the element, like the text or the image. Its size is known as the content width
and content height. Browsers are clever
and by default, they will calculate
the width and height based on the
content itself. Fortunately, developers can control the size to
manipulate the layout. You can do this by applying CSS rules to the
content, such as width, minimum width, maximum width, height, minimum height,
and maximum height. Let’s move on to the padding. The padding extends
the content size. Its size is known as the padding box width and
the padding box height. The thickness of the padding is determined by the padding top, padding bottom, padding
left and padding right. The padding box width
can be calculated as content width plus padding left side plus
padding right side. You can calculate the
padding box height by adding the content height, the padding top side and
padding bottom side together. Next, let me show you
how you set the border. The border goes around
the padding and content. Its size is known as the border box width
and border-box height. You can set different
types of borders, such as solid border
or a dash border. To understand how this
plays out practically, I will demonstrate the HTML, the CSS style, and
the physical border. To set the border, you can
write border width thin, border width medium,
and border width thick. Now, let me share with you a pro developer tip to help you calculate the size of your HTML elements when
working with the box model. An HTML element is equal to
the size of the border box. To get the border box width, you add the padding
box width plus the border left side plus
the border right side. You can calculate the
border-box height by adding the padding box height
and the border top side, as well as the
border bottom side. Lastly, let me show you
how to set the margin. Following on from what
you have just learned, do you have an idea of what
the CSS rule might be? The margin extends
the border area to separate the element from
its neighboring elements. Its size is known as
the margin box width, a margin box height. To specify the margin sides, you write, margin top, followed by the value,
the margin bottom, margin-left, and
margin-right follow the exact same notation. We can also set the padding, border and margin with
shorthand properties, but more on this later. Lastly, let’s calculate the
size of the margin box. Can you guess how?
The margin box width can be calculated by adding
the border box width, the margin left side and the
margin right-side together. To get the margin box height
at the border box height, margin top side and margin
bottom side together. To easily remember this, think of the content
as yourself. The padding is like the
thickness of your clothes. The border is like the
silhouette or outline and the margin is
the personal space between you and another person. In this video, I’ve opened
the box model for you. Inside it is the content and
around it flows the padding, the border fits
around the padding. Lastly, the margin is the empty space keeping
elements apart. You will work with the box model in every website you develop, so I encourage you to start practicing with it
in your designs.

Video: Document flow – block vs. inline

The web browser uses a system called document flow to position HTML elements on the screen. There are two main types of HTML elements: block-level elements and inline elements.

Block-level elements occupy the full width of their parent element and start on a new line. Examples of block-level elements include paragraphs, headings, and lists.

Inline elements only occupy the width of their content and do not start on a new line. Examples of inline elements include images, links, and spans.

The display CSS property can be used to change an element from block-level to inline or vice versa.

Understanding Document Flow and Block vs. Inline Elements in CSS

CSS, or Cascading Style Sheets, is a powerful tool for styling and presenting HTML elements on a web page. One of the fundamental concepts in CSS layout is understanding the concept of document flow and how it affects the positioning of elements on the page.

Document Flow: The Foundation of Element Positioning

Document flow refers to the natural flow of HTML elements within the browser window. It determines how elements are stacked and aligned on the page. There are two main types of elements in document flow: block-level elements and inline elements.

Block-level elements are like building blocks, occupying the full width of their parent element and starting on a new line. They create a vertical hierarchy on the page. Examples of block-level elements include:

  • Paragraphs (<p>)
  • Headings (<h1>, <h2>, etc.)
  • Lists (<ul>, <ol>, etc.)
  • Images (<img>)

Inline elements, on the other hand, fit within the flow of surrounding content and do not start on a new line. They are like words in a sentence, flowing horizontally within a block-level element. Examples of inline elements include:

  • Links (<a>)
  • Spans (<span>)
  • Images (<img>) with the display: inline property applied

Visualizing Block and Inline Elements

To visualize the difference between block and inline elements, imagine a simple web page with a heading, a paragraph, and a link.

In this example, the heading and paragraph are block-level elements, so they will appear on separate lines. The link is an inline element, so it will appear within the paragraph text.

Changing the Default Display Behavior

While block and inline elements have their default display behavior, you can use CSS to override this behavior. The display property allows you to change an element’s display type from block-level to inline or vice versa.

For example, you can make a paragraph appear inline by setting its display property to inline:

CSS

p {
  display: inline;
}

This will cause paragraphs to flow within the surrounding content, rather than starting on a new line.

Practical Applications of Document Flow

Understanding document flow is essential for creating well-structured and visually appealing web pages. Here are some examples of how document flow is used in web design:

  • Creating sections and hierarchies: Block-level elements are used to divide content into sections and create a clear hierarchy on the page.
  • Laying out text and images: Inline elements allow you to position text and images within a paragraph or other block-level element.
  • Aligning elements: CSS properties like margin, padding, and float can be used to fine-tune the alignment of block and inline elements within the document flow.

Mastering Document Flow: Key Takeaways

  • Block-level elements occupy the full width of their parent element and start on a new line.
  • Inline elements fit within the flow of surrounding content and do not start on a new line.
  • The display property can be used to change an element’s display type from block-level to inline or vice versa.
  • Understanding document flow is crucial for creating well-structured and visually appealing web pages.

By mastering document flow and the differences between block and inline elements, you can control the layout and positioning of HTML elements on your web pages, creating a more organized and visually appealing user experience.

By now you’ve learned how CSS
styles individual html elements, but how does the web browser nowhere
to place the elements on the screen? The web browsers normal way of calculating the position of html elements on
the screen is called document flow. By default, nearly all html elements
are organized into one of two categories namely in block and
in line elements. A block level element will occupy the full
horizontal width of its parent element and the vertical height of its content. Each block level element will
have a new line before and after. Therefore, multiple block level elements
will stack on top of each other like a stack of boxes. In line elements only occupy the width and
height of their content. They don’t appear on a new line,
hence the name in line. Therefore, multiple in line elements
can form a row of elements. When coding in html, you need to be able
to recognize and use block elements. Some examples of block level elements
include the tags, div form and heading. You also need to be familiar
with common in line elements. These include the tags anchor,
image, input label, bold, italics, emphasis and span. Let me demonstrate
the difference between block and in line now by using an example of an html
file containing div and span elements. Okay, so,
I’ve opened visual studio code here, I will now demonstrate an example where
you will learn about document flow. First, I want you to notice that I
have a div element containing three sentences of Lauren ipsum. A div or divided element is used
to divide content into sections. If you don’t speak Latin, don’t worry. Lauren Ipsum is placeholder text
that has been used since the 1500s. Each sentence contains a span element
that you may recall are in line elements. Notice that all text is displayed in an
unbroken flow of content in the browser. In the code I have div and span elements. I am now going to change the middle
sentence from a span to a div tag. Because the div tag is a block level
element, notice that the sentence has been moved to a new line and the span
element after it is also a new line. It’s possible to change elements from
block level to in line and vice versa. This can be done using
the display CSS property. Let me add a CSS file
called style dot CSS. In my html file, let me give
the middle sentence div element an Id. I now add a rule in the CSS file to
change the element to an in line element. To create the CSS rule, I type hash
middle and open the curly brackets. On the next line, I type display
colon space in line semi colon. And on the next line I
close the curly brackets, notice that the div has now
changed to an in line element. If I want to change it back,
I can just remove the property. I have deleted the rule in the code to
change the element to an in line element. Another way to achieve this outcome is
to set the display property to block. I have now changed the value for
display to block. In another video, I will demonstrate
to you that there are other values you can set for the display property to
change the layout behavior of an element. Congratulations, you have now learned
about block and in line elements in html. It’s important for developers to be
comfortable working with each and to know the differences. It’s important to remember that block
elements begin on a new line and take up the full width of the page. And in line elements work within the flow
of surrounding content rather than breaking onto their own line.

True or false. Inline elements appear on a new line. 

False

That’s right. Inline elements only occupy the width and height of their content and they don’t appear on a new line, hence the name “inline”.

Reading: Alignment basics

Reading

Practice Quiz: Knowledge check: CSS basics

To reference a CSS file in an HTML document, you use the ______________ tag.

In the following CSS rule, the `h1` part of the rule is called the ______________.

What is the padding box width of the following CSS rule?

Which CSS property and value change the HTML element to a block-level element?

Which CSS property and value change the text to center alignment?

Reading: Additional resources

Reading

Creating a web page


Quiz: Module Quiz: Introduction to HTML and CSS

Which two elements should be added to the html element to make the structure of an HTML document?

When using the anchor tag <a>, which attribute determines where the hyperlink links to?

When adding an image to a web page, which of the following is the correct HTML tag?

How many columns exist on the following HTML table?

When an HTML form is submitted to a web server, which HTTP methods can be used? Select all that apply.

For the following HTML code, which CSS selectors can be used to select the h1 element? Select all that apply.

In the following CSS code, what is the color: part known as?

Based on the following CSS, what will be the margin-box width for div elements?

True or false. In document flow, block-level elements always start on a new line.

Based on the following CSS code, how will the text be aligned for the p element?

Video: Module Summary – Introduction to HTML and CSS

This is the end of the introductory module on HTML5 and CSS. In this module, you learned the basics of HTML, CSS, and web accessibility.

HTML, or Hypertext Markup Language, is the foundation of all web pages. It defines the content and structure of a web page. CSS, or Cascading Style Sheets, is used to style the appearance of a web page. Web accessibility is the practice of making web pages usable for people with disabilities.

In this module, you learned how to write HTML tags, create basic HTML documents, add links to HTML documents, add images to web pages, manipulate data in tables, and build HTML forms. You also learned how to select and style elements within HTML documents, use the box model to style elements, and understand the concept of document flow.

In addition to the technical skills, you also learned about the importance of web accessibility. You learned about assistive technologies used to browse the web, and you learned basic techniques to improve the accessibility of your web projects.

This module is the first step on your journey to becoming a web developer. In the next modules, you will learn more about HTML, CSS, and JavaScript. You will also learn how to use these technologies to create more complex and interactive web pages.

You’ve reached the end of this introductory module
on HTML5 and CSS. It’s now time to review what you’ve learned in these lessons. This module started with
a lesson on the basics of HTML or Hypertext
Markup Language. You began this lesson
by exploring the answer to the question, what is HTML? You learned to, describe the purpose of HTML
in a web browser, demonstrate the
use of HTML tags, including how to ensure that
you use the correct syntax, identify how HTML elements are utilized in a web document. You also explored,
HTML documents, and you should now be able to outline the structure
of an HTML page, including the root, head, body and other elements, demonstrate how to build
basic HTML documents by combining common
key elements and tags in order to
create a structure, and explain how to
create a link between HTML documents with the use of an H ref or hypertext
reference attribute, to redirect users
between the documents. You should also be able to add images to a web page
with HTML code, utilize HTML to manipulate
data in tables, and build HTML forms with a
wide range of input types. You then moved on to study the Document Object
Model or DOM, a tree structure or model
of objects in an HTML page. During your investigation
of the DOM, you learned how to, explain what the DOM is, and the purpose that it
serves with an HTML document, demonstrate how the
DOM is created from an HTML page by exploring the
element that generate it, and visualizing it
in diagram form, and provide a high level
description of common instances in which web developers interact with the DOM
through JavaScript. These include, updating content, setting up events, and
animating HTML elements. You also explored the
concept of web accessibility by exploring how people with
disabilities can understand, navigate, and interact
with websites. Following your exploration
of this topic, you should now be able to, demonstrate high
level knowledge of assistive technologies used to browse the web like screen reading and speech
recognition software, make use of basic techniques to improve the accessibility
of your web projects, such as correct HTML structure. Following this
Introduction to HTML5, you then covered a
lesson on the basics of CSS or Cascading Style Sheets. The lesson began
with an overview of selecting and
styling in which you learn to write CSS rules in order to style elements
within HTML document, demonstrate and understanding of the box model and
its key features, and explain the concept
of document flow. You then demonstrated
your skills in these topics by creating
a biographical webpage, the first of many
webpages you’ll create in this specialization. You should now be
familiar with the basics of HTML5 and CSS. Well done. You’re making good progress on your
learning journey.