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 ) to mark text, so that you can describe the text to your browser. This also includes search engines and a few other programs which we'll cover later on. HTML tags are like grammar forms in verbal communication, without them, you cannot build a coherent website, much like you cannot form correct sentences or paragraphs without grammar. In our case, we used only a few simple tags, and below is a list of the functions that each tag performs: •

- headline (of the first degree) • - picture element •

- 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

, and so on and so forth. 17 All tag constructions are extremely simple and look like this: content You might have noticed that the second tag is different. This is because each tag must "close," as the saying goes in HTML jargon. The forward-slash appears in the closing tag to tell the browser that this marks the end of an element. For example, all paragraph elements end in

, while a Header 1 ends in

. There are also tags which do not need to be enclosed. For example image tags, , which are used to insert pictures. In our example above, we used it as follows: There are several more tags that do not need to to be "closed", which you will learn about later in this book. One important realization to make about HTML tags is that, like dictionaries contain the vocabulary of a language, so too does HTML have a kind of dictionary that defines tags and describes when and where to use them. At the moment we have covered only a few, but there are many more. The international organization known as The World Wide Web Consortium (W3C) encourages best practices, standards, and compatibility across the web. You can view their definition of HTML elements here: http://www.w3.org/TR/html-markup/elements.html. Also, it's helpful to remember that, like natural languages, HTML evolves over time. Today, we use the fifth version of the HTML language, known as HTML5. Returning to our example, it should look like the following in HTML code: 18

We're looking for an HTML and CSS developer

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:

We're looking for an HTML and CSS developer.

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!

At the very top of any self-respecting HTML page, there should be an indication of the document type. This is the doctype:

We're looking for an HTML and CSS developer.

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!

21 The highlighted section above "tells" the browser that the page is an HTML document, and the page's code should be treated according to the established standards for the language. This informs the browser of several rules, which will guide how our website is viewed. Among other things, it helps the browser to know which tags may be allowed or not allowed. It helps the browser better interpret our code. Below the doctype, we want to put the marker :

We're looking for an HTML and CSS developer.

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!

Note that this tag needs to be closed, so we want to be sure to add at the end: code As a result, everything that belongs to our website is contained under the tag . When writing HTML, it only makes sense that the code should be enclosed by . 22 However, the tag is not yet complete. We want to add more information about the language of our site. Since this is an English example, let's designate it as such:

We're looking for an HTML and CSS developer.

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 the highlighted section has modified the tag. This is called an "attribute." Attributes are modifiers in HTML, and are always written next to the tag, between the <> enclosure. Attributes have the following template when writing code. tag attribute="value" As a HTML developer grows, he/she will come across many different attributes. In addition to "lang" there are a large variety. Here are just a few examples. • - the name of the attribute is "id", and the value is "sidebar" •

- the name of the attribute is "class" and the value is "landscape" • - the name of the attribute is "type", and the value is "text" We use attributes because in many HTML tags (for example ) do not contain all the necessary information. With attributes, we can modify the tags we use and add even more useful information to them. In this case, we have let the browser 23 known that our HTML document is written in English, so we've modified the tag with the attribute "lang" and given it a value for "en" (English). Multiple attributes can even occur in a single tag. Here are a few examples: HTML Training It's important to note that attributes are also part of HTML standards, just like nesting tags, and lists of these attributes, along with the tags that they can modify, can be found on the web. In fact, we've already used an attribute when we were first building our web page! Remember the image code? You placed it in an HTML document with the following line: 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.

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!

At the moment, our head tag is empty, but usually this is the location you want to put all meta information like the title of the whole document. As we create the page with our job offer, let's say that we decide on a title "Job Offer: HTML and CSS developer." Let's go ahead an add it! 25 Job Offer: HTML and CSS Developer

We're looking for an HTML and CSS developer.

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!

As you might have noticed, we added the title between the tag . What's the difference, and why did we do it that way? The below image explains what happens where we don't have a <title> tag (left), and our page that has the <title> tag (right). With title, our websites are more usable for browser user and navigating among many tabs is much easier. Another example might be my company website. I've used "Functionite - JavaScript workshop team" as a text inside title tag. Thus, it appears as that in Google search results: 26 As you can see, this is a very important to consider when coding your site. Without it, the site would be without a name, which would leave a bad impression on visitors. Personally, I wouldn't trust a page that doesn't even have a title. We need to do one more thing after adding a head, and that is to add the <body> tag. This separates the content of the entire page from the head. Everything displayed on your web page will be placed within <body>. <!DOCTYPE html> <html lang="en"> <head> <title>Job Offer: HTML and CSS Developer

We're looking for an HTML and CSS developer.

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!

27 Hooray! It looks like the end of our efforts and it seems that everything is correct. As a novice coder, it's probably a good idea to check if everything is OK (e.g. nested tags, good attributes, etc.). But not everyone has friends among advanced HTML developers, who may be asked for consultation. Luckily, there is a special tool we can use to check if our code is correct. The W3C organization, the same group that defines the standards and upholds best practices of HTML documents, has made a tool to check code. The tool is simply called an HTML validator, and is available at http://validator.w3.org. The page should look like the one below: Let's use the tool and check to see if our code is correct. 28 ...Woops! We have one error and 3 warnings. Let's take a look at them. The single error that the validator reported has to do with our image. Let's take a look at the content of the error: An img element must have an alt attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images. It turns out that the 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:

PS 再见

Let's check the effect in the browser: 31 Note the last line where, instead of a proper Chinese sentence, we can see some odd signs that don't make sense at all in any of modern languages. Don't panic, it's related to the warning that we have received in the HTML validator. It turns out that we haven't inserted a special line, that would allow other languages to display properly. In computers world, it's called Character encoding. If we passed this information to the HTML page, it would "tell" the browser that it's ready to display a wide range of characters including all different diacritical marks. 32 Let's do this! Job Offer: HTML and CSS Developer

We're looking for an HTML and CSS developer.

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!

This time we have had to use a special tag with the attribute charset and given it a value for "utf-8". utf-8 means a special character set that incorporating those from a majority of languages around the world, if not all of them. And that's it! You have just built your first website ever! Congratulations! 33 CHAPTER 4 HTML Exercise: Coding a Blogpost HTML Exercise: Coding a Blogpost Time for another exercise that will show you the philosophy of writing more advanced websites in HTML5. We'll try to produce the following example as a website, an article on a blog. 34 Note that here we have one header for the article. In addition, information about the author, as well as a photo and a "quote" paragraph. It is worth noting that as a front- end web developer, you will often receive graphic designs (like the one shown above) and have to recreate everything in HTML. Let's begin with the elements which will always be fixed and do not change. As we did earlier, we want the doctype tag, , the and . Note that in 35 our we'll want to make sure we have character encoding for "utf-8" so that we can display special characters on our site.
 Our starting template for the code will look like this: Let's add the title now: Justin Beaver fascinated by HTML At this point, we've finished the template which will now serve as the basis for the contents of the article. If we save the file as article.html, and then open in a browser, we will see nothing at all because the tag has no content. We generally start from the most general content, followed by more specific, detailed content as we progress. Let's analyze our pieces: 36 1. In the article, the most important information would probably be the header information which contains the title: "Justin Beaver: Ever since I learned HTML, my life has made a complete 180." 2. The next most important information is about the author, "authored by Damian Wielgosik." 3. The first paragraph of text comes next, then a place for a photo and its description. 4. Finally, the last paragraph, which contains a quote, "I will probably have a song about HTML on my next album". And those are our pieces. This type of analysis where we look at the pieces helps us visualize how the HTML code will look. The hierarchy starts from the most general element (the parent), and then continues to elements which a parent contains (children), which are more specific, detailed elements. With this understanding of hierarchy, our information should be organized as follows in the HTML code: Justin Beaver fascinated by HTML
You might have noticed that we used the
element which is contained within the parent. Articles consist of a header and content, so all tags representing heading and paragraphs will naturally be children of
. 37 Adding to our list of elements that we've discovered so far, we're going to now use an element that marks headers, the
tag. We can add the
code as a child of
. As we move from top to bottom in the code (in terms of importance), note that we also move rightwards with indentations to show that an element is lower on the hierarchy than its above component. In the example below,
is not only below
, but also indented to show where it is on the hierarchy. Justin Beaver fascinated by HTML
Our header section is now ready, but what should go inside the
? Well, we discussed earlier that we have a title and author. We'll use the tag

for the title of the text, and

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

and

tags to the header element: 38 Justin Beaver fascinated by HTML

Justin Beaver: Ever since I learned HTML, my life has made a complete 180

Posted by: Damian Wielgosik

By the way, you should note that in addition to

, 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
. It makes sense that the paragraph should be part of the
. So, we'll add the first

within

, with the same priority as
, but underneath it: Justin Beaver fascinated by HTML

Justin Beaver: Ever since I learned HTML, my life has made a complete 180

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.

40 Next we'll add the photo plus a description. For this type of content, and therefore all related to the entire document such as photos, charts or maps, we'll use the
tag. It is worth noting that the
condition of use is important into that you can use the extra element
, which puts a description of the image beneath it. Justin Beaver fascinated by HTML

Justin Beaver: Ever since I learned HTML, my life has made a complete 180

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.

Justin Beaver's cat is pleased
Justin Beaver's happy cat
After we've added the image, we just have one more paragraph to add to the article. Note that this paragraph has a quote, "I will probably have a song about HTML on my next album." We can annotate this quote so that our code has a greater 41 semantic value. Perhaps in the future, someone will look for quotes from Justin Beaver, and this designation will help them find the quote faster. Otherwise, search engines would always have to deal with one huge chunk of text. In order to indicate that a section of text is a quote, we're going to use the tag within the new paragraph: Justin Beaver fascinated by HTML

Justin Beaver: Ever since I learned HTML, my life has made a complete 180

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.

Justin Beaver's cat is pleased
Justin Beaver's happy cat

Beaver has already created some websites and does not intend to stop there. I will probably have a song about HTML on my next album, - the artist added.

42 Now, let's save the code to a file with the extension .html and display it in the browser. You have now completed your second page. Great! This is another step to becoming a professional web developer. 43 CHAPTER 5 Site Visuals in CSS3 Site Visuals in CSS3 You may have noticed that, with exception of our images, our site does not look very interesting. Black text on a white background isn't very welcoming to our visitors. Let's work on the appearance of our site with CSS (Cascading Style Sheets). Using this language, we can manipulate traits of the site like color, font size, and many other qualities. There is a huge list of possibilities available with CSS. In the previous sections, we used HTML to describe the content of the site, and divide it into fragments according to their importance. CSS will be responsible for the appearance of our web sites. CSS code can be placed in a separate file with the extension .css and inserted via a special HTML tag. You can also put it directly into the HTML document. Imagine for a moment, in this abstract example, that we want to construct a house with CSS code, where we choose items such as windows, doors, roofing, walls, gutters and so on. We would want to buy windows of specific sizes, and paint for each of the necessary parts. If we built this house in CSS, one of the many solutions to this task might look like this: 44 roof { background-color: green; } doors { background-color: yellow; width: 100px; height: 300px; } windows { border: 5px solid brown; width: 150px; } Let's analyze the "roof" block of this code from top to bottom (note that top-to- bottom reading is a rule when reading all types of code, not just HTML and CSS). roof { background-color: green; } If we translate the code above into normal English, we have chosen an element called "roof" and set the background color to green. windows { border: 5px solid brown; width: 150px; } The code above says, "for all windows, set the following: a frame (border) with a width of 5 pixels (5px), marked by a continuous line (solid) color (brown). Also, the window itself should have a (width) of 150 pixels (150px). 45 You might have noticed a recurring pattern in the code. On the first line, we write the name of the element (termed "selector"), and then define the appearance of that element between bracket. The template has the basic structure shown below: selector { property_name: property:value; } This type of construction is a typical CSS rule. The rule consists in turn of a selector (everything before the first bracket) followed by a list of properties that you write between the brackets. There are various way to specify exactly what we want to design. Let's say that we only wanted to specify design for windows on the ground floor. What then? We could write something like: ground floor window { border: 5px solid brown; width: 150px; } The result is that only the selector has changed. Instead of "window {" we have specified "ground floor window {" This code reads from left to right like "find the ground floor, and then find its window and set the following values." If we put a sub-selector under "ground floor windows" like: adjacent wall windows { 46 Then we're telling the browser: "find the ground floor window, windows next to it, and fill them with the following values," and so on. If you remember the analogy in which we talked about nested HTML tags as the children and parents, this is the same concept, elements nested within other elements. Unfortunately a browser can't quite build a house, but our example tells us how CSS works. This analogy is useful because as we code, we're not always able to see the changes. But we can think of paragraphs (

) as windows, and doors as the header (

), 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 fascinated by HTML

Justin Beaver: Ever since I learned HTML, my life has made a complete 180

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.

Justin Beaver's cat is pleased
Justin Beaver's happy cat

Beaver has already created some websites and does not intend to stop there. I will probably have a song about HTML on my next album, - the artist added.

Recall the analogy of building a house. Instead of building doors, windows, etc., we are dealing with elements like
,

,

, ,
and so on. These tags build the page and now CSS will help to give them style. I've prepared a screenshot on the next page so you can see how our modifications will change the resulting website. 48 As you can see, much has changed. We've added simple colors, backgrounds, changed the font style and so on. Let's proceed step-by-step for how to accomplish the effects in the image above. The first step is to save the entire HTML code into a separate file. For me it's called article.html. Then create a separate file in which we keep our CSS rules. Let it be main.css. 49 The files look like this on my computer: We can now try to open article.html in a browser and the main.css file using a text editor. I recommend Sublime Text Editor or TextMate. After each change made in main.css, we can refresh the browser the page in order to update its appearance. We now need to open the article.html in a browser, and load code from the main.css file. This is done through the tag in the in the HTML code.
 
 Just add a tag like this in the head: The "href" attribute indicates where the file is located. "Stylesheet" tells us that it is a CSS style sheet. Okay, to start making changes to visual appearance in CSS, let's try to find the right selector for the heading, similar to the code for windows and walls. h1 { } Here we are! It is here that we can tell the browser "for all elements in

, 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.

Posted by: Damian Wielgosik

Let's use CSS, and find the appropriate selector ("p {" ) and try to give it a background red color and a white color text: p { } background-color: red; color: white; Our main.css code should currently look like this: h1 { color: green; } p { } background-color: red; color: white; As you can see, we add a rule one under the other. Time to see how it looks now our website... 52 Oops, that's not quite right. It seems all the other paragraphs have also been changed to have the new background and text color. It's a problem with our code, because we used the following: p { } background-color: red; color: white; What we actually told the browser is to "find allall

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

, which is a "child" of the
. The code should reflect this hierarchy: article header p { background-color: red; color: white; } Let's see the effect of these changes. 53 Much better! It seems we were able to target the correct paragraph. But how did this happen? Well, we used the above code to tell the browser to know which tags the CSS selector should target. We do this by examining the HTML code and finding all the tags which should match the selector. In our case, we had nested tags of
,
, and

, 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

. Let's specify our CSS code to reflect this: article figure { width: 600px; } With this code, every
in the
tag will have a width of 600 pixels. Note that the "article" distinction would be helpful if we had multiple images throughout the blog post and wanted to specify different criteria for each. But since we only have one image, let's move on to the border code: article figure { width: 600px; border: 3px solid black; } Here we've added a property called "border." After the colon, we specify the width of the border (3 pixels), and the style of the border "solid" with the color "black." Let's see how it looks: 54 It looks like we have a problem. While the border is displayed with the correct style and color, the image displays beyond our 600 pixels. This is because we established the width of the element
, but the 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
. This is coded very simply: article figure img { width: 100%; } It now looks like this: 55 It would be nice to add some "padding" or space between the border and image. We do this by adding the property "padding." We can modify the code as follows: article figure { width: 600px; border: 3px solid black; padding: 5px; } The result: 56 You can try yourself to modify the value of the "padding" and see how the white gap changes between the picture and the border. Our page is looking good now, but we're not yet finished. The current paragraph text extends almost the entire width of the browser window which isn't very readable. Perhaps it would be fitting to somehow reduce the width of the text? Maybe limit it to 800 pixels? 
Let's choose a special CSS selector for this: article { width: 800px; } 57 That's better. Now what about font? If you look at the original image of our site, we have a slightly different font. Just as you can edit font styles in Microsoft Word, you can edit them in CSS too. In order to specify font, you want to add this property to the highest tag so that it applies to all text within that tag. For example, we'll set the font as a property for , so that every element below will have this setting. In the picture, I used a font called Verdana. Let's try to apply it: body { font-family: Verdana; } You can see the differences by deleting this line or changing the font-family to a different style. For the header, paragraphs, etc. the browser will display everything in Verdana. Finally, our code in the main.css file should look like this: 58 body { font-family: Verdana; } article { width: 800px; } article header h1 { color: green; } article header p { background-color: red; color: white; } article figure { width: 600px; border: 3px solid black; padding: 5px; } article figure img { width: 100%; } In general, it's good practice to start your code with the most general selectors and move into more complex ones. I started from body, followed by article, and so on, going from top to bottom. The higher the detail, the lower it sits in the list. 59 CHAPTER 6 Menu, Please! Menu, Please! Another popular part of websites is menu. Basically, it's a list of items which are often just simple links pointing to other places on the site. Let's implement it! We will start with the following HTML code: Menu Our menu will consist of four items: 60 • Home • Training • Conferences • About us We want it to look something like this: You might notice that under the tag, we've added the new tags