HTML & CSS Crash Course - Learn HTML and Css With Easy To Follow-step-By-step Tutorials
HTML & CSS Crash Course - Learn HTML and Css With Easy To Follow-step-By-step Tutorials
Tutor
Line Breaks
We can change the appearance of the page again and make it more readable by adding
some line breaks. This is done with the tag <br>.Unlike other tags, a closing tag isn’t
necessary. You just add one for each line break you want. For now lets remove the center
tag and just add line breaks. So lets add a line break after the Hello World! string:
<!DOCTYPE html>
<html>
<body>
Hello World!<br>
My name is Joe.
My friend is Sally.
</body>
</html>
This produces:
Hello World!
My name is Joe. My friend is Sally.
We can add more line breaks to put each sentence on its own line:
<!DOCTYPE html>
<html>
<body>
Hello World!<br>
My name is Joe.<br>
My friend is Sally.<br>
</body>
</html>
And we get this:
Hello World!
My name is Joe.
My friend is Sally.
To make it double spaced, we can add extra <br>
tags.
<!DOCTYPE html>
<html>
<body>
Hello World!<br><br>
My name is Joe.<br><br>
My friend is Sally.
</body>
</html>
And now we see:
Hello World!
My name is Joe.
My friend is Sally.
Remember for line breaks you don’t need a closing tag. To add more line breaks, just add
more
tags. Suppose we want:
Hello World!
My name is Joe.
My friend is Sally.
To get this result our HTML looks like this:
<!DOCTYPE html>
<html>
<body>
Hello World!<br><br><br><br><br>
My name is Joe.<br><br>
My friend is Sally.
</body>
</html>
In the next lesson, we’ll learn how to add a headline to our webpage.
Header Tags
Header tags enable us to easily create nice bold text to spruce up the appearance of our
web pages. Header tags use the format <hx> where x is an integer 1,2,3,4…. The smaller
the number, the larger the header. The truth is you already know about this since you’ve
probably used different levels of headers in your word processing program.
Header tags have an opening and closing tag. The general syntax is:
<hx> Your text here </hx>
Working on our previous example, we can make the phrase “Hello World!” a header by
using the <h1> tag:
<h1>Hello World!</h1>
If our entire html looks like this:
<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1><br>
My name is Joe.<br>
My friend is Sally.
</body>
</html>
The result we see is:
Hello World!
My name is Joe.
My friend is Sally.
When you add a header tag, the text between the header tags is not automatically centered.
We have to to that manually. Let’s change our html to:
<!DOCTYPE html>
<html>
<body>
<center><h1>Hello World!</h1></center><br>
My name is Joe.<br>
My friend is Sally.
</body>
</html>
Now the page appears as:
Hello World!
My name is Joe.
My friend is Sally.
We can also enhance the appearance of the page by adding subheadings. This is done by
specifying the header level with an integer larger than 1. For example <h1>,<h2>,<h3>
etc. where <h1> is the highest header level, so will have the largest text.
Let’s add a header and put a subject heading as follows:
<!DOCTYPE html>
<html>
<body>
<center><h1>Hello World!</h1></center><br>
<h2>Friends</h2>
My name is Joe.<br>
My friend is Sally.
</body>
</html>
Now the web page looks like this:
Hello World!
Friends
My name is Joe.
My friend is Sally.
Notice that some spacing is automatically placed in between the header and the text that
follows. Basically this is working like you were just typing away in a word processor,
except we are putting in the formatting commands behind the scenes.
In the next chapter, we’ll see how to format text colors.
Font Color
A plain black and white web page might be functional, but its not very interesting. Let’s
see how we can improve the appearance of our web pages using the font tag and color
attribute. It’s very easy. Let’s return to the html we had in the last lesson:
<!DOCTYPE html>
<html>
<body>
<center><h1>Hello World!</h1></center><br>
<h2>Friends</h2>
My name is Joe.<br>
My friend is Sally.
</body>
</html>
To have a text string appear in a particular color, we use the syntax:
<font color=“color”>Text String</font>
Hence, we can have text appear red using:
<font color=“red”>Some text</font>
Changing our html file with this in mind we can make the main header red:
<!DOCTYPE html>
<html>
<body>
<center><h1><font color=“red”>Hello World!</font></h1></center><br>
<h2>Friends</h2>
My name is Joe.<br>
My friend is Sally.
</body>
</html>
The web page now appears as:
Hello World!
Friends
My name is Joe.
My friend is Sally.
Let’s say we wanted the “Friends” sub-header to appear as blue text. All we have to do is
add another font tag with the color attribute, for the “Friends” text string:
<!DOCTYPE html>
<html>
<body>
<center><h1><font color=“red”>Hello World!</font></h1></center><br>
<h2><font color=“blue”>Friends</font></h2>
My name is Joe.<br>
My friend is Sally.
</body>
</html>
Note that we need a closing tag when using different font attributes.
Now our web page looks like this:
Hello World!
Friends
My name is Joe.
My friend is Sally.
Besides changing color, you might want to make other changes to fonts such as setting the
type face. We will explore this in the next lesson.
Font Size and Type Face
In this lesson we’ll learn how to change font size and type face. This is also done using the
font tag. To specify a font size, we simply write:
<font size=“x”>This is some text.</font>
where x is an integer. For example we can write:
<font size=“3”>My name is Joe.</font>
You can set different attributes using the same font tag. Let’s suppose that we wanted the
text to appear red and set the font size to 5. This could be done by writing:
<font size=“5” color=“red”>My name is Joe.</font>
This produces:
My name is Joe.
To change the typeface, we can set the face attribute in a font tag. For example, to set the
font of a text string to verdana, we write:
<font face=“verdana”>My other friend is Bob.</font>
And we obtain:
My other friend is Bob.
As before we can combine multiple attributes including the typeface. So lets set the text to
green, the face to verdana, and also set the size attribute of the text.
<font color=“green” face=“verdana” size=“4”>My other friend is Bob.</font>
This gives us:
Hello World!
Friends
My name is Joe.
My best friend is Sally.
My other friend is Bob.
Paragraph and Div Tags
Next we explore two more formatting tags that can be used to improve the appearance of
your web pages and present content in a meaningful way. The first is the paragraph tag. It
does exactly what it says, it creates a formatted paragraph for any text enclosed within the
tags (by adding margins and spacing). The paragraph tag is defined by <p> and you must
add a closing tag </p> where you want the paragraph to end.
This is my heading
Another line of text.
Line two of new section.
Some more text.
A Different Section
Text for the second section.
Hyperlinks
The internet wouldn’t have much functionality if you could only look at one web page at a
time. To ease the ability of users to move about your website and to visit related pages of
interest we need to add hyperlinks to our html documents. This is done with the hyperlink
tag.
Oddly the hyperlink tag is denoted with the letter a. The letter a is used because it means
anchor text. Hence the <a> tag defines a hyperlink which will open a new web page when
the user clicks on it. As you probably know hyperlinks are displayed to the user as
underlined text, with blue color for a page they have not opened before and purple for web
pages they’ve already visited.
Several attributes can be specified with the hyperlink tag. The most important is the href
attribute which tells the browser which link to open when the user clicks on the text. So
href is just the url of the target web page.
HTML 5 introduces some new attributes. For example, you can use download to tell the
browser to begin downloading a file when the user clicks on the link.
The target specified in the href attribute can be a local file (relative to the web page) or
any URL. The syntax you will use is:
<a href=“url to open”>Text displayed to user</a>
The text displayed to the user can be anything, most web page developers put descriptive
text that makes it more readable and more amenable to search engines. But you can just
put the URL there if desired. These days people automatically know that underlined blue
text is a hyperlink so putting descriptive text is preferred.
Here is an example that will display a link to the New York Times website:
<body>
Hello World!
<br>
<a href=“http://nytimes.com”>New York Times</a>
<br>
This looks like so:
Hello World!
New York Times
If we wanted to just display the actual link to the user, we could write:
<body>
Hello World!
<br>
<a href=“http://nytimes.com”>http://nytimes.com</a>
<br>
Then we would get:
Hello World!
http://nytimes.com
Now suppose you want to open a file on your own server. Consider the case of a web page
named about.html in your same directory. Then you would write:
<body>
Hello World!
<br>
<a href=“about.html”>About Us</a>
<br>
If the file was in a folder called “Info” you’d write:
<body>
Hello World!
<br>
<a href=“about.html”>/Info/About Us</a>
<br>
Often links are placed in the middle of a text string, for example:
Here is some more text linking to <a href=“http://cnn.com”>cable news
network</a> find out what the latest news is.
This displays as:
Here is some more text linking to cable news network find out what the
latest news is.
Displaying Images
Images are displayed in a web page using the img tag. The syntax is:
<img src=“link to image” alt=“text to display if image not rendered” style=“width:Xpx;
height:Ypx;>
You can link to images on your own server or to any image on the web if you know the
URL. To give a specific example I have obtained a link to a picture of the actress Selma
Hayek. We can display the image in our web page as follows:
<body>
<h1>Selma Hayek!</h1>
<br>
<br>
<img src=“http://bit.ly/1HDOqOk” alt=“Selma Hayek Not Here”
style=“width: 200px; height: 250px;”>
<br>
<br>
</body>
If the image doesn’t render, the text “Selma Hayek Not Here” will be displayed in its
place. Here is how the web page looks:
Selma Hayek!
To see how the alt attribute works, you can copy and paste from the preview in Kompozer
to a text document, and you will see:
Selma Hayek!
Selma Hayek Not Here
You can put other tags around the image to improve the appearance of your web page. For
example, if we wanted to center the image, we would write:
<body>
<h1>Selma Hayek!</h1>
<br>
<br>
<center>
<img src=“http://bit.ly/1HDOqOk” alt=“Selma Hayek”
style=“width: 200px; height: 250px;”>
</center>
<br>
<br>
</body>
</html>
Video Players on Web Pages
Our next topic is the video tag. Using <video you can put a link to a video you want to use
and display it using HTML 5 in a full featured video player. The best way to teach this is
to simply show an example.
<body>
<video width=“400” controls=””>
<source src=“savagearchives.info/video/20-Timer-sm.mp4” type=“video/mp4”>
Your browser does not support HTML5 video.
</source>
</video>
<p>Video courtesy of <a href=“http://www.xcode-training-and-tips.com/”
target=“_blank”>Xcode Training</a>.
</p>
</body>
So we start with the <video> tag, then follow it with the <source> tag which tells the
browser the URL where the video is located. As usual the URL can be a local file or any
URL on the internet.
The code has a bit of text that is displayed if the browser does not support HTML 5.
Kompozer has not been updated in awhile so when I preview this code in Kompozer I see
this:
Your browser does not support HTML5 video.
Video courtesy of Xcode Training.
On the other hand, opening it in the browser we see:
Ordered and Unordered Lists
In this lesson we are going to learn how to display bulleted and numbered lists using
HTML. This is done using the unordered list or ordered list tag. An unordered list is just a
bulleted list of items. To create an unordered list, you will enclose your list items within an
opening <ul> tag and a closing </ul> tag. Each list item is denoted with a list item or <li>
tag. You need a closing </li> tag at the end of each list item.
As an example, let’s create a list of friends names. In most cases you will have a header of
some kind to denote the list. This can be done by putting the text introducing your list in
between paragraph tags. So let’s start by putting a title for our list as follows:
<p>My Best Friends:</p>
Now we will need to enclose the list with <ul> and </ul>:
<p>My Best Friends:</p>
<ul>
</ul>
Each bulleted list item will appear as <li>item text</li>. Let’s make our list of names:
<p>My Best Friends:</p>
<ul>
<li>Sally</li>
<li>Jose</li>
<li>Paul</li>
</ul>
What this produces is the following:
My Best Friends:
• Sally
• Jose
• Paul
Using <ol> in place of <ul> produces a numbered list. The tag <ol> means ordered listing.
For example:
<p>Countries:</p>
<ol>
<li>China</li>
<li>United States</li>
<li>United Kingdom</li>
<li>France</li>
</ol>
Gives us:
Countries:
1. China
2. United States
3. United Kingdom
4. France
Big and Small tags
The Big and Small tags provide you with a means to carefully control text size. You can
nest big and small tags to get a string of text to be the size you want.
To use a Big tag simply place an opening <big> at the start of the text you want to be
larger font. To make it larger add as many <big> tags as necessary. At the end of the text
you want to have larger font, place a corresponding closing tag</big>.
The small tag works in an analogous manner. For example:
<h1>My Web Page</h1>
<p>Video <big><big><big>is</big></big></big> <small><small>
<small>courtesy</small></small></small>
of Xcode training</a></p>
Produces:
Web
Page Backgrounds
Let’s say you want to set a background color for your web page and make headers appear
as white text. We can choose a cyan color for our background:
This is easy to do using <style>. Let’s look at the details and then put it together for the
entire page. You can set the background color of an object using background-color in your
css. In the example here:
background-color: #01c4de;
Will give the color displayed in the image. To set the background color for the entire web
page, we specify that we want this to be the background color for the body tag.
body{
background-color: #01c4de;
}
We can
tell the
browser
that we
want all
h1
headers
to be
Classes in CSS
In this lesson we’re going to take a step towards more advanced CSS by introducing the
notion of classes. We will use this to build a web page like this:
A class
is just
another
way to
identify elements in your HTML and give them certain attributes. The flexibility of the
class allows you to apply characteristics to multiple items in your HTML if they are
identified as being a member of that class. So in short, a class selector selects HTML
elements in your web page that have been identified as being that class and applies the
attributes you specify to it.
No doubt you’ve already seen classes in HTML files you’ve looked at. In your CSS a
class is denoted with a period followed by the name assigned to the class. You can have a
class without specifying the type of HTML tag it applies to such as:
.myclassname{
attributes here
}
Or you can apply it to a specific tag, such as the paragraph tag. This example will set the
font color of any paragraph marked “myblueclass” to blue:
p.myblueclass{
color: blue;
}
To specify that a paragraph in your html file is a member of this class,
<p class=“myblueclass”>This is a sample paragraph that uses the myblueclass CSS class.
</p>
Remember that you can apply a class to multiple elements in a page. For example, we can
specify that all HTML elements with class=“center” are center-aligned and have blue text:
.center {
text-align: center;
color: blue;
}
Then you can apply this to multiple elements:
<h1 class=“center”>Header with the center class</h1>
<p> A normal paragraph without special formatting</p>
<p class=“center”>A formatted paragraph.</p>
<p>Another normal paragraph</p>
This produces: