The Practical Programmer's Guide To HTML & CSS
The Practical Programmer's Guide To HTML & CSS
by David Hikari
Dedicated to:
CERN, for inventing the web browser and web server, and
giving birth to the World Wide Web,
the late Steve Jobs, for his immeasurable gifts to mankind, and
This book may be purchased for educational and/or informational use only.
Unauthorized reprint in any format and distribution, commercially or free of cost, is prohibited.
Attached Content
Please download the attached content that comes with this book at this URL: http://bit.ly/1LwoI0B and unzip it with
WinRAR or iZip. Henceforth, this unzipped folder named ‘files’ shall be referred to as ‘the downloadable content’ or as
‘the content attached with this book’.
Table of Contents
1. HTML & CSS – Learn to Build Static Web Pages
1.1. What is HTML?
1.2. Hello World! - Building your first webpage
1.3. Headings, Paragraphs and other HTML elements to get you started
1.4. What is CSS?
1.5. Implementing internal/external stylesheets
1.6. Customizing color, background and position of elements using CSS
1.7. Links, Forms, Text inputs, Buttons , and Lists
1.8. Customizing Forms, Links, Text inputs and Buttons
1.9. Adding Images and Videos
1.10. Full List of HTML elements
1.11. Full list of CSS Selectors
1.12. Full List of CSS properties and Possible values
1.13. Responsiveness – A Complete Reference
1.14. Parallax using HTML and CSS only
4. Miscellaneous
4.1. What’s next?
Section 1. HTML & CSS – Learn to Build Static
Web Pages
Chapter 1.1 – What is HTML?
Congratulations, you have created your first web page! Just click on index.html in your
file browser or open the file with a web browser and you should see something like this:
Though it’s not on the web yet and it’s certainly not much of a page, this is the inner
skeleton of modern websites. You have learned how to talk to the browser. In coding
HTML and CSS, our job is to tell the browser how to load and style the web page, and
what content should be in it.
Chapter 1.3 - Headings, Paragraphs and other HTML elements to get
you started
You have already used the <h1> tag. And you know there are six header tags – from <h1>
to <h6>, in decreasing order of size. Now let us talk about the <p> tag. This is a paragraph
tag. Content inserted within a <p> tag automatically begins on a new line. However, by
default the first word of a paragraph element isn’t indented, but we can do that using CSS
later on.
There are a couple of extra elements which come in handy sometimes. The <b> tag causes
text within it to become bold, and the I tag causes text within to become italic, and the
<U> tag causes text inside to be underlined. In HTML5, <strong> was introduced as a
replacement for <b> and <em> as a replacement for <i> so it is recommended that you use
them instead.
There is also an element called <span> and an element called <div>. These are simply
containers for blocks or pieces of text which you do not wish to be bold, underlined, but
want to be styled in a way not specifically defined in HTML. For example, you may want
a few words in a paragraph to be highlighted in red. You can do it with <span>. Or you
may want an entire set of paragraphs with images attached to have the same background
color. You can achieve this by wrapping them in a <div> element.
However, the magical properties of <span> and <div> have to be defined with CSS.
Before we go into that, let us see what elements we can use in the body by now.
<p>This is a paragraph</p>
<p>This is a paragraph with some <strong>bold text.</strong></p>
<em>Here is some italic text outside any paragraph.</em>
<p>Span lets us apply <span class=”red”>custom, not pre-defined CSS</span></p>
<div>Can be used as a wrapper or container for other elements.</div>
Next we will learn how to add CSS to individual elements and collectively to several
elements at once.
Chapter 1.4 - What is CSS?
Cascading Style Sheet(Cascading Style Sheets plural) or CSS, is a set of instructions for
affecting the styles of different elements in HTML.
The basic CSS syntax is:
selector {
property: value;
}
Chapter 1.5 – Implementing internal/external stylesheets
There are three ways of adding CSS to your web page:
1. Using the <style> element inside the <head> element, generally after the closing
</title> tag and before the closing </head> tag.
2. Using style=”/*your styles here*/” attribute in your opening HTML element tag, for
example,
<h1 style=”text-decoration:underline;”>Hello World!</h1>
will underline the heading 1 text, ‘Hello World!’.
3. External CSS file: If you are creating a set of pages and you want them to be styled
similarly, this is the best option. Just create an empty file called ‘style.css’ in the same
folder as index.html and put in your style definitions there. Then add
<link href=“style.css” type=“text/css” rel=“stylesheet” />
before the </head> tag and after the </title> tag.
You can also put style.css in a folder named for example ‘css’ then the linking would be
thus:
<link href=”css/style.css” rel=”stylesheet” type=”text/css” />
If you have a folder called ‘mypage’ and in it two folders called say ‘css’ and ‘html’,and
you put index.html in ‘html’ and style.css in ‘css’, the code in this case would be:
<link href=”../css/style.css” rel=”stylesheet” type=”text/css” />
The part ‘../’ simply goes to the parent folder of the current folder, that is it would go up
one folder, so to speak. You can specify ‘../../’ to go up two folders, and so on.
Chapter 1.6 - Customizing color, background and position of
elements using CSS
Let us open our Hello World page. Add one <p> element containing some text-any text
you like. Many people like to add some Latin as filler text when designing Web pages, to
do that search ‘Lorem ipsum copy paste’ in Google.
We want to set the font size of h1 element and p element to something of our choice. To
do that, add the following line of code within the <style> element or in the external
stylesheet, whichever you choose to create:
h1 {
font-size:40px;
}
Similarly, you can change the font size of the p element as well:
p{
font-size:20px;
}
Here I must mention something that can be quite irritating while churning out web pages,
it’s that different web browsers set different margin and padding on some elements.
Generally having all elements with a margin and padding 0px until we style them is ideal.
Hence, if you want, you can put the following code in to make the margin and padding of
all elements reset to zero by default. Remember, this will not override any of your
margin/padding specifications in CSS.
body {
margin:0px;
padding:0px;
}
You can also use ‘*’ instead of ‘body’. * is called the universal selector – it selects each
and every element that you have defined within the <body> element, including the
<body> element itself. However, using the universal selector to reset margins and
padding’s will remove all default margin and padding between paragraphs and heading
elements, so be careful.
Let us now change the background color of the header to orange and that of the paragraph
to pink. Why orange and pink? Because they are relatively brighter colors and our
background color is black. At least for now we will be benefited if our individual content
is clearly distinguishable. Add background: orange; to the h1 selector’s property-value
combinations. It should now look like this:
h1 {
font-size: 40px;
background: orange;
}
p{
font-size:20px;
background: pink;
}
By the way, here ‘h1’ is called the selector,because it selects the element(s) whose styles
are to be affected. ‘font-size’ and ‘background’ are called properties and ‘10px;’ and
‘orange;’ are called the values of their respective properties. The semi-colon at the end of
each line within the curly brackets is a must, except for an empty line, in which case it
should be avoided. We know, CSS syntax can be generalized thus:
selector {
property: value;
}
Now we want to indent the paragraph’s text. Add
p{
text-indent:25px; /* adds indentation before a paragraph */
line-height:2px; /* sets length between two lines */
letter-spacing:1px; /* sets distance between two letters */
}
The text within /* and */ are called comments. Comments are used to explain code to
other programmers or yourself so that code can be debugged and updated easily.
Comments are ignored by the browser, so there is no malfunction of the program. In CSS,
you open a comment with /* and close it with */
The body element’s style can be changed as well, usually to set a background color or set
universal styles like font-family or text color. Let us add some more styles to the body
element:
body {
background:#000;
color:white;
}
This will set the background to black and the color to white. We also add color:black; to
the p selector’s styles. This is because many properties of the parent element are inherited
or duplicated in the child element(s), so both the heading and <p> will have font color
white unless we change them.
A note about colors. There are at least four ways in CSS to define a color:
1. Text- orange, red, green, black, yellow;
2. Hex code: #ffffff for white, #000000 for black. Can be shortened to #fff for white and
#000 for black.
3. RGB: rgb(0,0,0) for black, rgb(255,255,255) for white.
4. RGBA: this is an addition in CSS3. It is like RGB, but accepts four parameters and the
last parameter is opacity. 1 is fully opaque and 0 is fully transparent. rgba(0,0,0,0.5);
means partly transparent black; rgba(255,255,255,0.5) means partly transparent white.
I am supplying a list of hex color codes to help you get started choosing colors for your
sites.
#ff700e - bright orange #b9d6c2 – off-white blue
#000080 – dark blue #b6ffd3 – off-white green
#662441 – dark violet #990000 – blood red
The finished page thus far looks like this:
We want the heading text ‘Hello World’ to be at the center of the line. We can do this by
adding the attribute align=”center” to the opening h1 tag, or adding text-align:center; to
the h1 selector’s styles, thus:
<h1 align=”center”>Hello World!</h1> OR in CSS,
h1 {
text-align:center;
}
Now, let us add some margin and padding to the paragraph. Within the curly brackets
following the p selector, add
margin:20px;
padding:10px;
You can also write margin:0px 20px; or margin:20px auto; or margin:0px 5px 10px 15px;
The syntax is-
margin: top left bottom right; or
margin: vertical horizontal;
We will go with our initial choice.
You can see that margin affects the amount of space left blank on the outside of the
element, while padding is the blank space inside the element.
Now let us style a div element. In our Hello World finished page, copy the entire
paragraph element and paste it right after the closing <p> tag. Add a couple of line breaks
between the closing</p> and the opening <p>
So basically it now looks like this:
<p>Mumbo jumbo in Latin
</p>
<div>
<p>Mumbo jumbo in Latin
</p>
</div>
<div>
<p>Mumbo jumbo in Latin
</p>
Now let us divide the paragraphs each into a column of width half the page. This is simple
enough to do. Add the following style to your <style> element or external stylesheet:
div {
display:block;
width:50%;
float:left;
}
And now we have two columns:
We can see that the design of the elements with respect to each other seem irregular,so we
just modify some styles:
h1 {
margin:15px;
padding:10px;
}
p{
margin:5px 15px;
padding:10px;
}
Note that adding any margin or padding to the div element will break apart the column and
put each div on a separate line, because the sum of width of the two divs includes non-
zero margin and padding and exceeds 100% of the page width.
The float property is used to indicate positions of elements. A value of left makes the
elements stick to the left, and a value of right makes them stick to the right. If the sum of
two elements floated left or right is less than or equals 100%, they appear in the same line,
even though their display property is set to block, not inline-block.
The result is:
…which looks much more clean and professional.
Also note that if any element within the div with width 50% has any margin or padding,
there is no problem because it adjusts it’s content to fit the div’s width.
Whenever we are creating columns in a web page, we are basically using the float
property to build makeshift columns. One thing can go wrong- If there is any less-than-
full-width element before or after the column area, they may slide into the columns and
misalign them making the result look disastrous. So before the first opening <div> and
after the last closing </div> add some line breaks and in the space add:
<div style= “clear:both;width:100%;”></div>
This will prevent tedious correction and a lot of headaches when we add more elements to
our index.html page.
The finished Hello World page is available in folder ‘chapter-1.6’ of the download-able
content attached with this book.
Chapter 1.7 – Links, Forms, Text Inputs and Buttons
Links
Inserting the following code into HTML will produce a link to the Google homepage:
<a href=”http://www.google.com”>Go to Google</a>
Now in the same directory as index.html,create another html file called home.html and put
any HTML content inside of it. Next in the index.html after the closing </h1> tag add:
<a href=”home.html”>Go to Home</a>
A link will appear and clicking on it will take you to home.html. This is called relative
linking. This is the same way we include styles in the <link> element.
Links are called dummy links when their href value is ‘#’. For example,
<a href-”#”>Dummy Link</a> is a dummy link. They are used for demo purposes only.
Inputs
Any text input area is generally a part of a form, so to create a form, first we need to learn
how to create a text input. Add a <br> tag after the closing </a> in index.html and then
add:
<input type= “text” size= “15” placeholder= “Text Input” />
After refreshing, you will see that a input box has popped up below the link. The
placeholder attribute creates the gray text in the text box, that disappears as soon as you
type in something.
In HTML5,there are input fields specifically for numbers, emails, date and time, and so
on. For now we will just touch upon password input box and you can check the others in
the reference. Insert the following after the text box code:
<input type= “password” placeholder= “Password” />
Refresh the page. You may notice that anything you type into the password field is not
shown, adding a layer of protection and security to web forms.
Here are some other input elements:
<input type= “radio” />
<input type= “checkbox” />
<input type= “email” />
<input type= “date” />
<input type= “datetime” />
<input type= “number” />
<input type= “time” />
<input type= “button” />
<input type= “search” />
<input type= “month” />
<input type= “range” />
<input type= “color” />
<input type= “tel” />
<input type= “url” />
All these must have an attribute of name=”something” in order to be accepted by the form.
Radio buttons need to all have the same name, if only one is to be chosen of a set.
Labels.
Sometimes placeholders are not sufficient for indicating the purpose of an input field, such
as a checkbox or radio button. Hence the <label> element is used:
<label for=”username”>Enter username:</label>
<input type=”text” id=”username” name=”user” />
The for attribute of <label> takes in the id value of the input element connected with the
label. The advantage is, when the user clicks the label,the required input is immediately
brought into focus and anything the user types then goes into the input field, in this case,
with an ID of “username”.
This is particularly important for check boxes and radio buttons.
Drop-down List:
The following code will create a drop down list:
<select name= “dropdown”>
<option value= “apple”>Apple</option>
<option value= “orange”>Orange</option>
<option value= “lemon”>Lemon</option>
</select>
Adding attribute multiple= “multiple” will allow more than one option to be chosen.
Buttons:
Now for the buttons. Each form requires a submit button, or some JavaScript to trigger a
submit action. The code below will create a submit button with the value “Submit”:
<input type= “submit” />
The next piece of code will create a submit button but instead of ‘Submit’ it will show any
text of your choice:
<input type= “submit” value= “Click Me!” />
Another type of button exists,which is mostly used to perform JavaScript functions:
<button onclick=“myFunction();”>Click Here</button>
Similar to the submit button, there is a reset button. Clicking the reset button in a form will
reset all the elements’ values and erase any data that the user input from the form. The
code for a reset button is:
<input type= “reset” />
Forms:
For the form to work, we must set a value for the name attribute of all text input fields
within the form. Add name= “username”, thus:
<input type= “text” placeholder= “Text Input” name= “username” />
Let me explain a bit about forms. They retrieve the data from text fields within the form
when the submit event is triggered, then send it using the GET or POST method to a
specific page. The page then acquires the values and depending upon what the values are,
it performs different actions. For this to work, there must be a web server which processes
the data using a server side language like PHP or ASP.
Insert the following before the Link:
<form method=“get”> or <form method= “post”>
and the following after the Submit button:
</form>
Lists:
There are two types of lists in HTML: ordered lists and unordered lists. Here’s how to
create an ordered list:
<ol>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ol>
And here’s how to create an unordered list:
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
An ordered list looks like:
And an unordered list looks like:
You can put other elements like hyperlinks and labels within the <li> element.
Additionally, you can change the ‘list-style-type’ property of li to suit your needs.
Chapter 1.8 – Styling Links, Text Inputs and Buttons
Let us style our links. Many developers find it necessary to remove the underline that is
set by default in links. So we write:
a{
text-decoration:none;
}
We can also change the default blue color for unvisited links and default purple color for
visited links:
a{
color:red;
}
a:visited {
color:yellow;
}
Elements can also be styled to change their properties when a user hovers over them, thus:
a:hover {
color:black;
}
To style text inputs, we shall use the class attribute within each text input that needs to be
styled and style them equally. So we write:
<input type=”text” />
and in CSS:
label, input, a {
font-size:20px;
padding:15px;
border-radius:10px;
}
When we style classes in CSS, we put in a period (.) followed by the class name.
Similarly, when we style an id in CSS, we put in a hash or pound sign(#) followed by the
id. Remember that the same class can be shared by many elements, but an id value is
unique to one element only. You cannot have more than one element with id=“username”
in a single HTML file.
Similarly, we can style buttons. Add a class= “button” to the button element(s) you want
to style and in CSS insert:
.button {
margin:15px;
}
.button:hover {
cursor:pointer;
background:#bbb;
}
Refer to the Full List of CSS properties and their possible values and experiment with
different properties.
If I have missed any elements that you feel should be included, you are free to e-mail me
at davidhikaribooks@gmail.com
Chapter 1.11 – Full List of CSS Selectors
This chapter is dedicated to providing the types and varieties of CSS selectors and
teaching you how to tackle complex situations that may arise when you are building a
professional webpage.
* - The universal selector, selects every element inside <body> and also the <body>
element itself
E – selects all h1 elements of type E in the document. ‘E’ can be replaced by ‘h1’,
‘p’, ‘a’, ‘body’, but not ‘title’
.class – selects every element that has a class of “class”. You can define a class in an
element by putting in ‘class= “class name” ’. An element can belog to more than one
class, a class can apply to more than one element.
#id – selects the element that has an ID of ‘id’. You can define an element to have an
id by adding the attribute ‘id= “id” ’ to the opening tag of the element. A specific ID
can only be applied to a single element, once.
E F – matches any element F that is a descendent of E. All F elements that are
present in an E element are affected
E > F – matches any element F that is a child of E. Does not affect F elements
wrapped in child element(s) of E
E:first-child – matches any element that is the first child of it’s parent
a:link – affects all hyperlinks that have not yet been visited by the user
a:visited – affects all links that have been visited by the user
textarea:active – affects styles of a text area but only when it is active,or the user is
clicking it.
textarea:hover – affects styles of a <textareaa> element when the user’s mouse
pointer is hovering over that element
textarea:focus – this particular style is valid only for text inputs, usually. It applies
the styles contained in the following curly brackets to, here, the <textarea> element
when the cursor is blinking in that particular element. In that case, the element is said
to be in focus.
E + F – selects an F element that is immediately preceded by an E element
E[foo] – matches any E element whose foo attribute is set to anything
E[foo=“warning”] - matches any E element whose foo attribute is set exactly to
“warning”
E[foo~=“warning”] - matches any Element that has a foo attribute of space-
separated words or numbers, one of which is exactly equal to “warning”
div.warning – selects a div element with a class of “warning”. Different from div
.warning which is of type E F and simply selects any element of class= “warning”
that may be present in a div element.
div#myid – selects all divs with an id of “myid”
selector:pseudo-class {
property: value;
} /* here is a collection of pseudoclasses: :link :visited :active :hover :focus
:first-child :last-child :nth-child :nth-last-child :nth-of-type :first-of-type :last-
of-type :empty :target :checked :enabled :disabled */
selector:pseudo-element {
property: value;
} /* :first-letter and :first-line are two pseudo-elements */
p:nth-child(3n+2) – selects every p element that is the nth child of it’s parent. n starts
from 1 until theree are no more elements to select. Here it will select the 5th, 8th,
11th and so on elements.
Chapter 1.12 Full List of CSS Properties with their Possible Values
‘background-
scroll | fixed | inherit scroll
attachment’
‘background-
<color> | transparent | inherit transparent
color’
‘background-
<uri> | none | inherit none
image’
[‘background-color’ || ‘background-
see
image’ || ‘background-repeat’ ||
‘background’ individual
‘background-attachment’ ||
properties
‘background-position’] | inherit
see
‘border-color’ [ <color> | transparent ]{1,4} | inherit individual
properties
see
‘border-style’ <border-style>{1,4} | inherit individual
properties
‘border-top’
see
‘border-right’ [ <border-width> || <border-style> ||
individual
‘border-bottom’ ‘border-top-color’ ] | inherit
properties
‘border-left’
‘border-top-
color’ ‘border-
the value of
right-color’
<color> | transparent | inherit the ‘color’
‘border-bottom-
property
color’ ‘border-
left-color’
‘border-top-style’
‘border-right-
style’ ‘border-
<border-style> | inherit none
bottom-style’
‘border-left-
style’
‘border-top-
width’ ‘border-
right-width’
‘border-bottom- <border-width> | inherit medium
width’ ‘border-
left-width’
see
‘border-width’ <border-width>{1,4} | inherit individual
properties
see
[ <border-width> || <border-style> ||
‘border’ individual
‘border-top-color’ ] | inherit
properties
depends on
‘color’ <color> | inherit
user agent
see
‘cue’ [ ‘cue-before’ || ‘cue-after’ ] | inherit
‘cue’ [ ‘cue-before’ || ‘cue-after’ ] | inherit individual
properties
[[ <family-name> | <generic-family> ]
depends on
‘font-family’ [, <family-name>| <generic-family>]*
user agent
] | inherit
<absolute-size> | <relative-size> |
‘font-size’ medium
<length> | <percentage> | inherit
see
[ ‘list-style-type’ || ‘list-style-position’
‘list-style’ individual
|| ‘list-style-image’ ] | inherit
properties
‘margin-right’
<margin-width> | inherit 0
‘margin-left’
‘margin-top’
<margin-width> | inherit 0
‘margin-bottom’
see
‘margin’ <margin-width>{1,4} | inherit individual
properties
see
[ ‘outline-color’ || ‘outline-style’ ||
‘outline’ individual
‘outline-width’ ] | inherit
properties
see
‘padding’ <padding-width>{1,4} | inherit individual
properties
‘page-break-
avoid | auto | inherit auto
inside’
see
[ [<time> | <percentage>]{1,2} ] |
‘pause’ individual
inherit
properties
depends on
depends on
‘quotes’ [<string> <string>]+ | none | inherit
user agent
‘speak-
code | none | inherit none
punctuation’
a nameless
value that
acts as ‘left’
if ‘direction’
‘text-align’ left | right | center | justify | inherit
is ‘ltr’,
‘right’ if
‘direction’ is
‘rtl’
[[<specific-voice> | <generic-voice>
depends on
‘voice-family’ ],]* [<specific-voice> | <generic-
user agent
voice> ] | inherit
Yes, this is the first wireframe converted partly to HTML. For the full code, open HTML
files ‘responsive1’, ‘responsive-2’ and ‘responsive-3’ in folder ‘chapter-1.13’ of the
download-able content in a text editor. Also open the HTML files in your desktop browser,
and resize the browser window to see the effects of the @media query.
Chapter 1.14 – Parallax using HTML and CSS only
Linear Gradients:
To change the background of <body> to a linear gradient, code:
body {
background:linear-gradient( to left top, blue, red);
}
Or it can be:
linear-gradient(45deg,blue,red); or
linear-gradient(to right, rgb(100,100,200),rgb(0,0,200)); or
linear-gradient(to left, #ff0000,#0000ff);
To know more about linear gradients, visit:
https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient
Radial Gradients:
These are usually not very useful. To know about them, visit:
http://www.w3schools.com/css/css3_gradients.asp
Chapter 2.4 – Changing Background Color to Transparent Color and
Images
In this chapter we shall learn how to set semi-transparent backgrounds to elements in
<body> and how to set a linear or radial gradient to <body> element.
First let us open the finished three-column Hello World page in your text editor. In the
CSS styles, previously we had coded:
p{
background: pink;
}
..we replace it with:
p{
background: rgba(255,255,255,0.5);
}
If we refresh our browser we find that the pink has been replaced by gray. Actually, the
color is a transparent white, but because of the uniform solid background color of <body>,
we see it as gray. So let’s change our body>’s background to an image, to appreciate the
transparent <p> elements. Copy any image file to your working folder and rename it
“img4.jpg”. Then in CSS, put in:
body {
background: url(img4.jpg);
}
Now we can clearly see the transparent background of <p>. However, we might add a
little more CSS to make the body’s background not repeat but remain static as the page is
svrolled. Add:
body {
background-attachment:fixed;
background-size:cover;
}
We may also want to add a border to each image defined in the previous chapter, like so:
img {
border:2px solid black;
}
Remember, ‘img’ affects the image element, while .img affects the divs with a class name
of ‘img’.
The finished page, called ‘finished.html’ is available in chapter-2.4 folder of the content
attached with this book.
Chapter 2.5 – Using @media Query to make Pages Responsive
In this chapter, we will be working with the finished HTML of Chapter 2.2. You can find
it as ‘starter.html’ located in ‘chapter-2.5’ folder of the downloadable content attached
with this book.
We will only make one break point in screen width for our responsive HTML page. That is
the 700px break-point. Any browser width of 700px or over will show the desktop site,
while a browser width of less than 700px will show the mobile-friendly version.
Let’s get started. In the style element, add:
@media only screen and (min-width:700px) {
div.one-third {
width:33.3333%;
}
}
@media only screen and (max-width:699px) {
div.one-third {
width:100%;
}
}
And finally remove the ‘width:33.3333%;’ style from the div.one-third selector’s styles
outside the media queries. So our final CSS styles will be:
h1 {
font-size: 40px;
background: orange;
margin:15px;
padding:10px;
text-align:center;
}
p{
font-size:20px;
background: pink;
margin:5px 15px;
padding:10px;
}
body {
background:black;
color:white;
}
p{
text-indent:25px; /* adds indentation before a paragraph */
line-height:25px; /* sets length between two lines */
letter-spacing:1px; /* sets distance between two letters */
color:black;}
div.one-third {
display:block;
float:left;
overflow:hidden;
}
img {
width:100%;
height:auto;
}
.img {
margin:5px 15px 15px 15px;
}
@media only screen and (min-width:700px) {
div.one-third {
width:33.3333%;
}
}
@media only screen and (max-width:699px) {
div.one-third {
width:100%;
}
}
You might want to redefine the margin properties of div.one-third when viewed on a
smaller screen. To do that, simply add ‘margin:20px 15px;’ to the div.one-third’s styles in
‘@media only screen and (max-width:699px)’, like this:
@media only screen and (max-width:699px) {
div.one-third {
width:100%;
margin:20px 15px;
}
}
Chapter 2.6 – Using @font-face to import Custom Fonts
Sometimes you may not be satisfied with the default fonts or the limited number of
additional fonts you can specify with the ‘font-family’ CSS property. There is a way to
specify the paths to a font file and make the browser load and display that font on the
whole page or on specific parts of the web page.
First, we use the @font-face method to indicate the source file of the font and assign a
name to the sourced font:
@font-face {
font-family: importedFont1;
src: url(path/to/font.ttf);
}
And then we set the page body’s font-family property to use that font:
body {
font-family: importedFont1;
}
Only TTF, OTF and WOFF font file formats are supported by most web browsers.
Open the ‘chapter-2.6’ folder of the downloadable content attached with this book. The
‘index.html’ file embeds an external font file from the same folder and applies it. You will
see a ‘fonts’ folder there. This contains a few open source(free to use) fonts that you might
want to try out on your sites. See http://www.w3schools.com/cssref/css3_pr_font-
face_rule.asp for more details.
Chapter 2.7 – Concept of Header
The <header> element is a crucial part of a web page. It is the first thing that users
see when they load a web page, and is instrumental in providing the general
outlook of the site. Usually, the header contains the company logo, some
navigation links and, sometimes, a search bar. For example, the header of the
bbc.com site:
And samsung.com:
And wordpress.org:
In this chapter, we will start another web page from scratch and this time create it
with all professional elements until we have a good professional-looking website.
Remember, this is only HTML and CSS, so we don’t have any control over how
individual controls will be processed. We just assume it will be done by someone
else, for now.
So let’s begin! Create an empty HTML web page with no styles and no content.
Now inside the <body> element, insert:
<header>
<div id=“logo”><img src=“logo.png” /></div>
<div id=“topsearch”>
<input type=“text” placeholder=“Search” />
<button><img src=“search.png” /></button>
</div>
<nav>
<ul>
<li><a href=“home.html”>Home</a></li>
<li><a href=“about.html”>About Us</a></li>
<li><a href=“portfolio.html”>Our Work</a></li>
<li><a href=“contact.httml”>Contact Us</a></li>
</ul>
</nav>
</header>
<div style=“clear:both;width:100%;”></div>
Now, let us add the styles for the header and then we will analyze the code. Here’s
the stylesheet:
body {
margin:0px;
padding:0px;
}
#logo{
display:inline-block;
float:left;
}
#logo img {
border:2px solid black;
}
#topsearch, nav ul, nav {
display:block;
float:right;
}
nav ul li {
display:inline-block;
}
#topsearch {
margin:20px;
padding:0px;
}
#topsearch button img {
height:25px;
width:auto;
}
#topsearch input {
height:25px;
margin:3px;
}
#topsearch button {
cursor:pointer;
position:relative;
top:6px;
background:white;
border:2px solid black;
padding:0px;
}
nav {
clear:right;
}
header {
background:#afa;
display:block;
border-bottom:2px solid black;
overflow:hidden;
}
nav ul li a {
margin:10px 5px;
border-radius:10px;
font-size:30px;
text-decoration:none;
padding:5px 10px;
color:black;
transition:all 0.4s ease-out;
}
nav ul li a:hover {
background:black;
color:white;
}
You can find the full HTMl code in the folder ‘chapter-2.8’ of the downloadable content.
Chapter 2.9 – Concept of Footer
Now let’s add our footer HTML at the bottom of the page, just before the </body>
tag. Of course, we will use our clearfix div to separate the footer from the rest of
the page:
<div style=“clear:both;width:100%;”></div>
<footer>
<div class=“one-third”>
<h3>Contact Us</h3>
<p>Email: davidhikaribooks@gmail.com</p>
<p>Phone: +1987654321</p>
<p>Address:</p><p> 23/10 Des Moines,</p>
<p>California - 112233</p>
</div>
<div class=“one-third”>
<a href=”#”>Facebook</a>
<a href=”#”>Google+</a>
<a href=”#”>Twitter</a>
<a href=”#”>Instagram</a>
</div>
<div class=“one-third”>
<p> Copyright Information with second logo</p>
</div>
Now to style the footer. As you can see, in this case we have made three divs in the footer
so as to take advantage of the full desktop screen size. Our CSS code will be:
footer {
background:black;
overflow:hidden;
color:white;
}
.one-third:nth-child(2n) {
text-align:center;
}
.one-third a{
font-size:23px;
color:white;
font-weight:bold;
display:block;
padding:7px;
}
footer .one-third {
width:33.3333%;
margin:0px;
padding:0px;
float:left;
}
Now our page looks like:
That’s it! You have now styled the header, footer and sidebar sections of your web page. In
the next chapter, we will proceed to insert information or content into our <main>
element, and style the content appropriately.
Chapter 2.10 – Building a professional webpage
Now to make our webpage responsive, good-looking and professional, we will add a
@media query, a parallax image header with some legend, and insert content into out
<main> element.
Let us first add a parallax area to our page. In HTML, after the closing </header> tag,
insert some line breaks and add:
<div style=“clear:both;width:100%;”></div>
<div id=“container”>
<div id=“parallax”>
<div class=“legend”>Travel and Tourism for all generations
<p>We provide Lorem ipsum dolor sit amet, so that lorem ipsum dolor sit amet</p></div>
</div>
</div>
height:300px;
}
#parallax .legend {
width:40%;
margin-top:40px;
padding:20px;
margin-left:18%;
float:left;
font-size:40px;
background:rgba(0,0,0,0.3);
color:white;
}
.legend p {
font-size:20px;
}
Refresh the page, and there we have our parallax element. Usually websites tend to
introduce themselves with something stylish and professional, like parallax or slideshows.
Next we make our parallax responsive by changing the width element of the text inside the
parallax with a media query:
@media only screen and (max-width:699px) {
#parallax .legend {
width:80%;
margin-left:auto;
margin-right:auto;
float:none;
}
}
And our parallax part is finished. Now, we insert our content into the <main> element. So
just after the <main> tag, insert a few like breaks and put in:
#lowernav ul li {
list-style-type:none;
display:inline-block;
}
#lowernav ul {
float:right;
}
#lowernav ul li a {
padding:10px 15px;
font-size:20px;
background:#aaa;
color:black;
text-decoration:none;
transition:all 0.4s ease-out;
}
#lowernav ul li a:active {
position:relative;
top:2px;
left:2px;
}
#lowernav ul li a:hover {
background:#ccc;
}
@media only screen and (max-width:699px) {
#parallax .legend {
width:80%;
margin-left:auto;
margin-right:auto;
float:none;
}
aside, main {
width:100%;
}
.item img {
width:100%;
}
#logo {
width:80%;
margin:0px auto;
float:none;
}
#logo img {
width:100%;
}
}
@media only screen and (min-width:700px) {
.item img {
width:30%;
}
}
.item {
background:white;
border-bottom:2px solid black;
border-top:2px solid black;
border-left:2px solid black;
border-right:2px solid black;
overflow:hidden;
padding:5px;
}
.item img {
As you can see, I’ve already inserted the @media queries that are needed to make the
main and aside elements responsive and also make the images present in our main content
responsive. The finished page is available in ‘chapter-2.10-2’ folder of the downloadable
content. Usually there is a ‘heading’ of an individual page present, and this can be
something like, “Showing posts in Category ‘birds’” or ‘Showing search results for…’ and
so we have added a heading 1 element to our main content simply as ‘Title of the Page’.
However, all is not finished. You can choose to remove the borders completely, but I
decided to edit the borders a bit to look more aesthetically pleasing. Also, I needed to style
the lower pagination to look like buttons. Here’s the additional CSS:
aside {
border-top:2px solid black;
width:25%;
}
.heading1 {
border-top:2px solid black;
text-align:center;
border-left:2px solid black;
border-right:2px solid black;
margin:0px;
padding:10px;
}
#lowernav ul li {
list-style-type:none;
display:inline-block;
}
#lowernav ul {
float:right;
}
#lowernav ul li a {
padding:10px 15px;
font-size:20px;
background:#aaa;
color:black;
text-decoration:none;
transition:all 0.4s ease-out;
}
#lowernav ul li a:active {
position:relative;
top:2px;
left:2px;
}
#lowernav ul li a:hover {
background:#ccc;
}
Then, in the menu that pops up, click ‘Embed’, like so:
And now you can copy the code from the text box that shows up, and paste it directly into
the HTML file of your website. Let’s look at the <iframe> sourced from YouTube again:
<iframe width=“560” height=“315” src=“https://www.youtube.com/embed/1qKQDXVQdkw” frameborder=“0”
allowfullscreen></iframe>
Here, you can change the attributes height and width of the iframe to be what suits your
needs, for example,
<iframe width=“640” height=“480” src=“https://www.youtube.com/embed/1qKQDXVQdkw” frameborder=“0”
allowfullscreen></iframe>
You can also remove the ‘allowfullscreen’ attribute if you do not want to allow the video
to play in full screen in your site.
Section 3 – Advanced Projects
Chapter 3.1 – PSD to HTML
Congratulations! If you have read this book upto here, you should have a fairly
good grasp of HTML and CSS. So what good is the knowledge in real life? Well,
unlike what we learned in high school, this isn’t only helpful for teaching our
kids. If you can demonstrate your skill, you might be able to land a job in the
Information Technology industry. But before that we must ask, what niches in the
job market can you actually fulfill with a skill set of just HTML and CSS? The
answer is PSD to HTML. PSD is the Adobe Photoshop format for saving images.
It can store information about layers and stuff that are helpful in editing the image
with Photoshop. Basically, a business owner first approaches a web designer who
makes a photo mockup of the website. When the employer approves, the mockup
lands on the hands of a web developer, which is you. Usually, a web developer
needs to know HTML, CSS, Javascript, jQuery and PHP to be able to tackle
advanced sites. But oftentimes, employers are looking to get it done by different
persons, for example, the design by person A, the conversion to HTML by person
B, adding logic and Javascript by person C, and adding server-side programming
by person D. At this moment, you can quite comfortably fit in as person B So
your primary work will be PSD to HTML.
PSD under the General Public License are really hard to find, but finding free-to-
use-commercially PSDs to experiment with is much easier. Simply search ‘free
web templates psd’ in Google to get a few PSDs for experimenting purpose. To
open PSDs, you need Adobe Photoshop.
If you do not have Photoshop, you need to first convert the .psd files to .jpg or
.png. This can be done by several free software, simply search ‘psd to jpg
software download windows/mac/linux’ depending on whether you use Windows,
a Mac, or Linux. Then you need any image editor that can crop images and save
them as separate files. This process is called ‘Image slicing’. You will need them
to extract logos or other graphical content from the image file that you cannot
duplicate using CSS.
In the ‘templates’ folder of the downloadable content, you will find three image
files that can help you get started. Simply open them up in Photoshop or an image
editor and crop out the logos or any irreplaceable content, then code separate
HTML files in which you try and duplicate their individual looks and feel. This
will make you seasoned in HTML and CSS, and even give you some portfolio to
start out with. The finished files(courtesy of html5up.net) are found in the
‘psd2html’ folder of the downloadable content.
Chapter 3.2 – Career Advice
If you feel confident about your code and want to start charging for it immediately, I
recommend that you become a freelancer in either of these sites:
http://www.freelancer.com
http://www.upwork.com
There you can find plenty of paid projects being posted, projects that you can complete. I
recommend Upwork, because one doesn’t have to pay to take their proficiency tests.
Completing the tests is vital for landing a project. I also advise you to go for the fixed
priced projects initially because if you choose an hourly project it may take you too much
time to complete, resulting in client dissatisfaction. Good luck!
Section 4 – Miscellaneous
Chapter 4.1 – What’s next?
Now that you have mastered HTML and CSS, it is time for you to take a short
break and relax a bit. You are officially a web developer. But there are still ways
to go. A full-stack web developer works with:
1. HTML and CSS for the structure and layout
2. JavaScript and jQuery for Logic and advanced elements like slideshows,
AJAX,etc.
3. PHP or ASP which are server-side languages. PHP is open source and used in
Facebook. ASP is by Microsoft and used in w3schools and StackOverflow, the
biggest online resource for programmers.
The next logical step for you is to learn JavaScript and jQuery. After that you can
learn how to work with server-side language(s).