Foreword
Foreword
We live in a time when websites have become part of our everyday lives, replacing
newspapers and books, and offering users a whole range of new opportunities. You
probably visit at least a few of your favorite places on the Web every day, whether
it's for shopping, sending messages, playing games, checking the news or looking at
pictures of your friends. With the help of websites, you can have fun, make a living,
and even get to know other people.
So it seems that the ability to create websites would be extremely valuable. The
Internet provides us with many opportunities for development, and knowledge
about how the Internet is built allows us to understand the changes taking place in
modern society and economy.
But have you ever wondered how to create your own website?
If you have, I invite you to take a journey with me. We will look at websites that you
know and visit every day. By analyzing them, we'll use comparisons and analogies
that will help you better understand how they are built. I've been a web developer
and working on the web professionally for several years and I am confident that at
the end of our adventure together, we will create your first website.
Thanks to Kvba for design help; Peter Mierzejewski, Paul Czerski and Greg Kaliciak
for proofreading. Special thanks go to Veronica and Arnold.
2
CHAPTER 1
Websites and Legos
Websites and Legos
I really like The Verge. It's a website where you can find interesting articles about
new technologies, science and culture. On the home page, you can find dozens of
feature stories and articles which tend to look similar to each other. They have a
title, category, date, and image entry. It almost looks like it was built with bricks.
Let take a look at one of them:
3
At first glance, there's nothing complicated. Let's have some fun and highlight (or
block out) each element, as though we were dealing with Lego parts.
4
5
In total, we've got five pieces, arranged one on top of the other.
You might remember from your childhood that in order to build something specific,
you need a lot of different blocks that are useful for different things. Each of them
has a certain function. For example, when building a house, one type is useful for
walls, and the other more useful for floors. There is no single, universal block or
element from which you can make anything that comes to mind. See the figure
below? This is what a large selection of lego choices should look like, right?
The same is true with websites. When you build a website, you use the different
elements according to their ideal destination. In the Verge example, it seems that we
6
are dealing with various objects (or blocks), so doing something like applying the
same color or style to each block would be counterintuitive. After all, the title is not
the same as the date, or content of the paragraph. They each perform completely
different functions.
In order to continue then, we need to identify elements of the article by the
functions they perform on the site. Let's do this by adding a unique color to each
"block."
This is much more interesting. We now have a few different types of "building
blocks". Only two blocks are of the same type, specifically the two paragraphs below
7
the picture. This is no different than organizing legos. We're going to keep together
similar fragments of text that belong in the same group. As a formality, we'll name
each of the sections based on their function in the context of the article.
So we've marked each element according to their semantic meaning. This is exactly
the kind of behavior and logic that we can expect to see from a web browser. It's our
job to tell the browser, in a way that it can understand, what each of these elements
8
mean and how they fit together semantically. If this is not done, then our site will
appear as a clump of single text.
You might have created such pages or articles using a text editor program like
Microsoft Word or Pages. In text editors, achieving effects like "header styles" is a
matter of clicking a few buttons. In other words, when we select text in a word editor
and press "Header 1" we are assigning a bunch of different features in the
background that tell the editor to display stuff in a specific way.
Therefore, if we wanted to recreate a page like the above example in a word
processor, it would be simple and easy, at which point you could probably close this
book and go do something else (Breaking Bad would be a good alternative). The
problem is that we want to display this article on the web, which must be displayed
in a browser, and never in a text editing program.
9
CHAPTER 2
Let's build our first website
Let's build our first website
Let's suppose that we want to build a webpage with a job posting. It should look
something like this:
10
Before creating any kind of web page, it's a good idea to divide the content into
smaller components by their importance. Let's now try to identify and highlight each
element of this job posting, just as we did in the example from The Verge.
A brief analysis will help us to better understand which areas of the text should
stand out in the job posting. Red indicates the headline text. Green indicates the
accompanying image. And purple marks the two paragraphs (or "body") of the job
posting.
11
Let's return for a moment to the analogy of word processors and text editing
programs where web pages could be created in regular text documents. Perhaps
Open, Notepad, or Word, would contain the text of the announcement, and then
save as a text file. It should work right?
If you check this in a web browser, you get the following:
12
However, this doesn't look like what we designed, does it? It's just a mass of text and
doesn't display a picture. What now? Maybe we should try to create this post in
Word or Photoshop? We don't exactly want to do this. We've made an error here
that we'll soon fix. The key problem is that we've created a website with only plain
text.
A web browser cannot understand how to display a page properly with only plain
text — it doesn't know which part of the text should be the title or which part should
be a picture. In order to display the page properly, we need to define each element
by the function of the text and pass this information to the browser. We've partly
done it. Let's do a little definition work and use proper syntax so browser will also
understand it:
13
As you can see, there are special markings within the text. We'll get to these later.
For now, it's best to simply try to copy them exactly as they are, from top to bottom,
as if you were building with blocks, one by one.
You should get something like the following:
Next, save the HTML file. By the way: I recommend you Sublime Text Editor which is
one of the best code editors out there.
14
Now let's try the new file in a web browser.
15
It looks a bit better. Each section of the text is now formatted differently, and the
picture is also displayed. This is not the final version though. The lack of color, bold
text and style doesn't quite match up yet. But before we work on that, let's keep our
focus on the structure of the page.
First, let's compare the plain text document version of the posting, and the one
presented in HTML:
16
As a result of surrounding specific text fragments with special tags, we've created a
completely different document, which can be understood by the browsers. Way to
go!
This is called HTML, or Hypertext Markup Language, and is the primary markup
language for displaying information for a web browser. In simpler terms, it's a
language that uses "tags" (like - a paragraph of text
So, if you have a website with a few paragraphs of text, you surround each
paragraph with the tag. If the text is the main headline, then surround the text
with - headline (of the first degree)
•
- picture element
•
, and so on and so forth.
17
All tag constructions are extremely simple and look like this:
For our client, The Cat Factory, we need a skilled web developer in HTML and CSS. We offer a competitive salary, a bag of cat food, and toys.
Don't wait, apply now! Our crazy team is waiting for you right meow!
Notice that it contains two paragraphs of text, marked within tags:For our client, The Cat Factory, we need a skilled web developer in HTML and CSS. We offer a competitive salary, a bag of cat food, and toys.
Don't wait, apply now! Our crazy team is waiting for you right meow!
Suppose that we want the reader of our ad to pay particular attention to a certain part in a paragraph. Let's say that we wanted to mark the part of the text that says "we need a skilled web developer in HTML and CSS." How would an HTML tag let the browser know? The answer is the element, which will surround parts of text that should represent a strong importance for its contents.For our client, The Cat Factory, we need a skilled web developer in HTML and CSS. We offer a competitive salary, a bag of cat food, and toys.
Don't wait, apply now! Our crazy team is waiting for you right meow!
19 Note that the tag surrounds the text, and also sits within the tag. In
programming terms, we would say that the tag is a "child" element within
because it nests within the parent. There are many tags that can nest within
other tags, while many others cannot. Each tag has a specific list of possible HTML
elements which it can contain (or be contained by), and you will need to check
whether a certain tag is allowed or not. You can think of it like smaller blocks
combining to make a bigger block piece (in this case the block is a
component of , which is in turn a block of the whole text).
Here is what it looks like on the web.
Take note that a web browser we used above displayed the fragment in bold, but
this is not always a rule. It's very common that makes text bold, but
sometimes it will be ignored. The reason for using this HTML tag in this text is a good
example of how nesting works, but visual effects (like bold) can be better achieved
through a language called CSS, which we will learn more about in a moment.
20
CHAPTER 3
Features of Perfect HTML Code
Features of Perfect HTML Code
At this point we've established our first website. Unfortunately, according to the
current standards of good web pages, we are not yet complete. Just as engineers are
held to strict design guidelines, or restaurants have health and safety regulations, so
too do web developers have a top set of rules that define a job well done.
Let's begin with the code we have so far, with all of the elements we've learned
about:
For our client, The Cat Factory, we need a skilled
web developer in HTML and CSS. We offer a competitive
salary, a bag of cat food, and toys. Don't wait, apply now! Our crazy team is waiting for you
right
meow! For our client, The Cat Factory, we need a skilled
web developer in HTML and CSS. We offer a competitive
salary, a bag of cat food, and toys. Don't wait, apply now! Our crazy team is waiting for you
right
meow! For our client, The Cat Factory, we need a skilled
web developer in HTML and CSS. We offer a competitive
salary, a bag of cat food, and toys. Don't wait, apply now! Our crazy team is waiting for you
right
meow! For our client, The Cat Factory, we need a skilled
web developer in HTML and CSS. We offer a competitive
salary, a bag of cat food, and toys. Don't wait, apply now! Our crazy team is waiting for you
right
meow! For our client, The Cat Factory, we need a skilled
web developer in HTML and CSS. We offer a competitive
salary, a bag of cat food, and toys. Don't wait, apply now! Our crazy team is waiting for you
right
meow! For our client, The Cat Factory, we need a skilled
web developer in HTML and CSS. We offer a competitive
salary, a bag of cat food, and toys. Don't wait, apply now! Our crazy team is waiting for you
right
meow! For our client, The Cat Factory, we need a
skilled web developer in HTML and CSS. We offer a
competitive salary, a bag of cat food, and toys. Don't wait, apply now! Our crazy team is waiting for you
right
meow! PS 再见 For our client, The Cat Factory, we need a
skilled web developer in HTML and CSS. We offer a
competitive salary, a bag of cat food, and toys. Don't wait, apply now! Our crazy team is waiting for you
right
meow! for the author. There is no specific HTML tag for author, so in
this case we're using as a general container for a text.
In the code below, we've added tags to the header element:
38
Posted by: Damian Wielgosik within Posted by: Damian Wielgosik Justin Beaver confessed something that even his
greatest fans would have never expected of the skilled musicians
and lyricist. The young rock-and-roller admitted that since he
typed his first title tag, his life became easier. It has been
reported by those surrounding the Canadian that Beaver's private
mentors, Ryan Loseling and Nicolas Crate, often walk around Los
Angeles disputing what a great tool the HTML validator is. Posted by: Damian Wielgosik Justin Beaver confessed something that even his
greatest fans would have never expected of the skilled musicians
and lyricist. The young rock-and-roller admitted that since he
typed his first title tag, his life became easier. It has been
reported by those surrounding the Canadian that Beaver's private
mentors, Ryan Loseling and Nicolas Crate, often walk around Los
Angeles disputing what a great tool the HTML validator is. Posted by: Damian Wielgosik Justin Beaver confessed something that even his
greatest fans would have never expected of the skilled musicians
and lyricist. The young rock-and-roller admitted that since he
typed his first title tag, his life became easier. It has been
reported by those surrounding the Canadian that Beaver's private
mentors, Ryan Loseling and Nicolas Crate, often walk around Los
Angeles disputing what a great tool the HTML validator is. Beaver has already created some websites and does not
intend to stop there.
) as windows, and doors as the header
( Posted by: Damian Wielgosik Justin Beaver confessed something that even his
greatest fans would have never expected of the skilled musicians
and lyricist. The young rock-and-roller admitted that since he
typed his first title tag, his life became easier. It has been
reported by those surrounding the Canadian that Beaver's private
mentors, Ryan Loseling and Nicolas Crate, often walk around Los
Angeles disputing what a great tool the HTML validator is. Beaver has already created some websites and does not
intend to stop there. , Posted by: Damian Wielgosik elements and apply changes."
However, we only wanted to change the paragraph in the header line of the article.
We now need to modify the code so that the above selector to only apply to the
in , so the CSS selector "article header p" let's us specify
exactly where the changes will be applied.
Let's move on to the image in the article.
The dimensions of this article, let's say, should be 600 pixels wide. And remember
that our corresponding HTML tag for the image is We're looking for an HTML and CSS developer.
We're looking for an HTML and CSS developer.
We're looking for an HTML and CSS developer.
We're looking for an HTML and CSS developer.
In this case, the
tag was modified with the attribute src with the value of
images/white-cat.jpg.
The tag by itself is only
, so if we simply left it alone, the browser would have
no way to retrieve the source of information to display. When we define a "src," we
tell the browser, "Hey, load information from this source." The browser then knows
it should look for the folder "images" and the file "white-cat.jpg." We can also pass a
link to any resource on the web (if it's not restricted), for instance
http://4.bp.blogspot.com/-z4sMggeD4dg/UO2TB9INuFI/AAAAAAAAdwc/
Kg5dqlKKHrQ/s1600/funny-cat-pictures-032-025.jpg.
24
This is the gist of how attributes work. Thanks to attributes, we can add rich and
valuable information that is helpful for HTML tags.
Now let's return to our document and add in more necessary components. We'll be
looking at the tag , which contains elements that are not necessarily helpful
to your page's content, but essential for search engines, browser tabs, and so on.
For example, the title on a web page helps search engines find your content.
We're looking for an HTML and CSS developer.
We're looking for an HTML and CSS developer.
We're looking for an HTML and CSS developer.
tag in your document must always have the "alt" attribute
that will be used in case the browser cannot load the image. It will then display the
value of this alternate attribute instead (it's better than an empty space, right?).
Before we choose the value for the attribute "alt," let's look at the HTML specification
to make sure that we do it correctly:
Here is the page where we can check it: W3C:
“Gives the fallback content for the image. The requirements on the alt attribute's value
are described in the next section.”
It then further distinguishes several scenarios, for when and how to use the alt
attribute. In our case, we are dealing with an image that has been placed for
decoration of the post and does not contain any content, plot, or other information
to the reader. In this case, we should leave the alternate attribute empty, says the
specification:
29
“However, a decorative image that isn't discussed by the surrounding text but still has
some relevance can be included in a page using the img element. Such images are
decorative, but still form part of the content. In these cases, the alt attribute must be
present but its value must be the empty string.”
In other cases, we would want to choose a text string that might replace what is on
the picture. Detailed guidelines for several distinguished cases can be found at the
specification for the
tag here: http://www.w3.org/wiki/HTML/Elements/img.
Ok, so we know that we want to have the "alt" attribute empty.
Let's add it...
...And then check our document against the validator again.
It passed with no errors!
But we still have the 3 warnings. The most important of them is as follows:
“No character encoding information was found within the document, either in an HTML
meta element or an XML declaration. It is often recommended to declare the character
encoding in the document itself, especially if there is a chance that the document will
be read from or saved to disk, CD, etc.”
30
But not to worry, this is a simple warning. Before we try to understand it, let's
examine the following example which will allow us to better understand the
problem.
It's important to realize that your website may be visited by people from different
countries like Spain, China or Sweden not to mention others. If you allow it, they can
leave a comment or a message that might be displayed next to your articles. It's
important to note though, that different countries use different languages with their
own diacritical marks. In Swedish it can be ä or ö, while Chinese offer you a whole
range of them like 汉语拼音方案. In the global web it's crucial to let different
languages display correctly on the same page. Let's examine what would happen, if
we used Chinese language in our post:
We're looking for an HTML and CSS developer.
for the title
of the text, and
and
Justin Beaver: Ever since I learned HTML, my life
has made a complete 180
, there are lower levels of the
HTML headers that look like this:
•
•
•
•
•
These elements help to map the logical structure of headings and subheadings. So
for example, if we were writing in book format, its title would be contained between
the element
. Then, the names of the chapters will be tagged with
, and
subsections
.
Note that you shouldn't simply denote a header as
on a whim. Headers must
be nested under a higher priority header, so if you have
,
must occur
before it, and
must occur before that, and so on.
39
For example, this book could look like this:
Simple HTML5
My first website
W3C Validator
Meet CSS
Selectors in CSS
Let's move on to coding the first paragraph of text. We want to avoid nesting the
paragraph under the Justin Beaver: Ever since I learned HTML, my life
has made a complete 180
Justin Beaver: Ever since I learned HTML, my life
has made a complete 180
tag
within the new paragraph:
Justin Beaver: Ever since I learned HTML, my life
has made a complete 180
I will probably have a song about HTML on my next
album,
- the artist added.
), etc.
It would look like this:
p {
}
h1 {
}
Let's apply what we've learned in this analogy to our example and use the same
ideas to add a little color and life to the Justin Beaver article.
Recall that our code looks like this:
47
Justin Beaver: Ever since I learned HTML, my life
has made a complete 180
I will probably have a song about HTML
on my next album,
- the artist added.
, apply
the following appearances." Note that the braces are currently empty. Let's try to tell
it that we want the heading text to be green. We'll apply the property "color" and set
it to "green."
50
h1 {
color: green;
}
The operation for this rule is explained in the diagram below:
Let's check out how our page looks after the changes!
Yes! The title is indicated in green.
51
Now we want to address the next section with information about the author. Let's
say we want the text in white with a red background as shown a few pages ago. This
is the current HTML code:
Justin Beaver: Ever since I learned HTML, my life has
made a complete 180.
tag does not have any fixed width
and thus keeps its original size. It would be nice if the image took 100% of the width
of its parent