0% found this document useful (0 votes)
14 views

Introduction To HTML+CSS+Javascript - PPTX - 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Introduction To HTML+CSS+Javascript - PPTX - 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Introduction to web

technologies
HTML + CSS + Javascript

Javi Agenjo (@tamat)


Technologies

● HTML
● CSS
● Javascript
Goals
Introduction to web technologies:

● HTML to create the document


structure and content

● CSS to control is visual aspect

● Javascript for interactivity


HTML

● Stands for HyperText Markup Language


● Used to create a Web page
● Made up of tags that specify the structure
of the document (this section is a
heading, this section is a paragraph, etc..)

4
HTML Tags
● Most HTML tags work in pairs. There is an opening and a closing tag. For
example:
<p>Some content here.</p>
● The <p>…</p> tag displays a paragraph
● <p> opens the paragraph (opening tag)
● </p> closes the paragraph (closing tag)
● “Some content here.” will be displayed on the page
● Some HTML tags are self closing. For example:
<br />
● The <br /> tag will display a line break.

5
Required Tags
● All HTML documents should have html, head and body
tags, along with the DOCTYPE identifier.

○ !DOCTYPE – Tells the browser which set of standards the page


adheres to

○ <html>…</html> -- Surrounds the contents of the entire page

○ <head>…</head> -- Lists the identification information on the


page, such as the title

○ <title>…</title> -- Gives the name of the page that


appears in the top of the browser window

○ <body>…</body> -- Frames the content of the page to be


displayed in the browser
6
Basic HTML Template
<!DOCTYPE html>
<html>
<head>
<title>CMSC104 HTML Template</title>
</head>
<body>
This is just a basic HTML template to be used in CMSC104.
</body>
</html>

Example file: template.html 7


Basic HTML Template Screenshot

8
Some Common HTML Tags and Their Meanings
● <p>…</p> -- Creates a paragraph
● <br /> -- Adds a line break
● <hr /> -- Separates sections with a
horizontal rule
● <h1>…</h1> -- Displays a heading (h1-h6)
● <!--…--> -- Inserts a comment
● <ol>…</ol> -- Creates an ordered list
● <ul>…</ul> -- Creates an unordered list
● <img /> -- Inserts an image into the
document
● <a>…</a> -- Inserts a link into the document
9
Paragraph Example

<p>
The exam next week will consist of T/F,
multiple choice, short answer and
pseudocode questions. You cannot use a
calculator.
</p>
<p> After the exam, we will learn JavaScript.
It should be fun!!
</p>
10
Paragraph Example Screenshot

11
Line Break Example

<p>
Roses are Red. <br />
Violets are Blue. <br />
You should study for Exam 1. <br />
It will be good for you!
</p>

12
Line Break Example Screenshot

13
Horizontal Rule Example

<p>
The exam next week will consist of T/F,
multiple choice, short answer and
pseudocode questions. You cannot use a
calculator.
</p>
<hr />
<p>
After the exam, we will learn JavaScript.
It should be fun!!
</p> 14
Horizontal Rule Example Screenshot

15
Heading Example

<h1>This is heading 1</h1>


<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
<h4>This is heading 4</h4>
<h5>This is heading 5</h5>
<h6>This is heading 6</h6>

16
Heading Example Screenshot

17
Comment Example

<!-- This is just some sample


html to illustrate the use of a
comment -->
<p>
Here is my paragraph.
</p>
<!-- Here is another comment -->

18
Heading Example Screenshot

19
Ordered List Example

<ol>
<li>Print Review Questions for Exam 1.</li>
<li>Work on Review Questions for Exam
1.</li>
</ol>

20
Ordered List Screenshot

21
Unordered List Example

<ul>
<li>country music</li>
<li>monday mornings</li>
<li>brussels sprouts</li>
</ul>

22
Unordered List Screenshot

23
Link Example

<a href="http://www.cs.umbc.edu/104/">CMSC104 Main page</a>

24
Link Screenshot

25
Image Example

<img src="linux-tux.png" alt="Tux the Penguin" />

26
Image Screenshot

27
HTML: wrapping the info
We use HTML tags to wrap different
information on our site.

The more structure has the information, the


easier will be to access it and present it.

We can change the way the information is


represented on the screen depending on the
tags where it is contained, so we shouldn't be
worried about using too many tags.
HTML: tagging correctly
Try to avoid doing this: Do this instead

<div> <div>
Title <h1>Title</h1>
<p>Here is content.</p>
Here is some content <p>Here is more content</p>
Here is more content </div>
</div>

D O THIS
T
DON
HTML good use
It is good to have all the information properly wrapped in tags that give it some semantics.

We also can extend the code semantics by adding extra attributes to the tags:

● id: tells a unique identifier for this tag


● class: tells a generic identifier for this tag

<div id="profile-picture" class="mini-image">...</div>


HTML references
HTML Reference: a description of all HTML tags.

The 25 Most used tags: a list with information of the more


common tags.

HTML5 Good practices: some tips for starters


Technologies

● HTML
● CSS
● Javascript
CSS
CSS (Cascading Style Sheets) is a
stylesheet language used for describing the
look and formatting of a document written in
HTML or XML.

Thanks to CSS we can control all the


aspects of the visualization and some other
features:
● Colors: content, background, borders
● Margins: interior margin, exterior
margin
● Position: where to put it
● Sizes: width, height
● Behaviour: changes on mouse over
CSS fields
Here is a list of the most common CSS fields and an example:
● color: #FF0000; red; rgba(255,00,100,1.0); //different ways to specify colors
● background-color: red;
● background-image: url('file.png');
● font: 18px 'Tahoma';
● border: 2px solid black;
● border-top: 2px solid red;
● border-radius: 2px; //to remove corners and make them more round
● margin: 10px; //distance from the border to the outer elements
● padding: 2px; //distance from the border to the inner elements
● width: 100%; 300px; 1.3em; //many different ways to specify distances
● height: 200px;
● text-align: center;
● box-shadow: 3px 3px 5px black;
● cursor: pointer;
● display: inline-block;
● overflow: hidden;
CSS example
* {
color: blue; /*a comment */
margin: 10px;
font: 14px Tahoma;
}

This will change all the tags in my web ( ‘*‘ means all) to look blue with font Tahoma with
14px, and leaving a margin of 10px around.
CSS how to add it
There are four ways to add CSS rules to your website:

● Inserting the code inside a style tag


<style>
p { color: blue }
</style>
● Referencing an external CSS file
<link href="style.css" rel="stylesheet" />
● Using the attribute style on a tag
<p style="color: blue; margin: 10px">
● Using Javascript (we will see this one later).
CSS selectors
Let's start by changing the background color of one tag of our website:

div {
background-color: red;
}

This CSS rule means that every tag DIV found in our website should have a red background
color. Remember that DIVs are used mostly to represent areas of our website.

We could also change the whole website background by affecting the tag body:

body {
background-color: red;
}
CSS selectors
What if we want to change one specific tag (not all the tags of the same type).

We can specify more precise selectors besides the name of the tag. For instance, by class
or id. To specify a tag with a given class name, we use the dot:

p.intro {
color: red;
}

This will affect only the tags p with class name intro:

<p class="intro">
CSS Selectors
There are several selectors we can use to narrow our rules to very specific tags of our website.

The main selectors are:

● tag name: just the name of the tag


○ p { ... } //affects to all <p> tags
● dot (.): affects to tags with that class
○ p.highlight { ... } //affects all <p> tags with class="highlight"
● sharp character (#): specifies tags with that id
○ p#intro { ... } //affects to the <p> tag with the id="intro"
● two dots (:): behaviour states (mouse on top)
○ p:hover { ... } //affects to <p> tags with the mouse over
● brackets ([attr='value']): tags with the attribute attr with the value 'value'
○ input[type="text"] {...} // affects to the input tags of the type text
CSS Selectors
You can also specify tags by its context, for example: tags that are inside of tags matching a
selector. Just separate the selectors by an space:

div#main p.intro { ... }

This will affect to the p tags of class intro that are inside the tag div of id main

<div id="main">
<p class="intro">....</p> ← Affects this one
</div>

<p class="intro">....</p> ← but not this one


CSS Selectors
And you can combine selectors to narrow it down more.

div#main.intro:hover { ... }

will apply the CSS to the any tag div with id main and class intro if the mouse is over.

And you do not need to specify a tag, you can use the class or id selectors without tag, this
means it will affect to any node of id main

#main { ... }
CSS Selectors
If you want to select only elements that are direct child of one element (not that have an
ancestor with that rule), use the > character:

ul.menu > li { ... }

Finally, if you want to use the same CSS actions to several selectors, you can use the
comma , character:

div, p { … } ← this will apply to all divs and p tags


HTML arrange
It is important to understand how the browser
arranges the elements on the screen.

Check this tutorial where it explains the


different ways an element can be arranged
on the screen.

You can change the way elements are


arranged using the display property:

div { display: inline-block; }

Also check the property float.


Box Model
It is important to note that by default any
width and height specified to an element will
not take into account its margin, so a div with
width 100px and margin 10px will measure
120px on the screen, not 100px.

This could be a problem breaking your


layout.

You can change this behaviour changing the


box model of the element so the width uses
the outmost border:

div { box-sizing: border; }


Layout
One of the hardest parts of CSS is
construing the layout of your website (the
structure inside the window) .

By default HTML tends to put everything in


one column, which is not ideal.

There has been many proposals in CSS to


address this issue (tables, fixed divs, flex,
grid, …).
HTML

Grid system <div class="grid-container">


<div class="grid-item1">1</div>
<div class="grid-item2">2</div>
</div>
Because most sites are structured in a grid, I
recommend to use the CSS Grid system.
CSS
Check this tutorial to create the site structure .grid-container {
easily display: grid;
grid-template-rows: 100px; 100px;
grid-template-columns: 100px; 100px; 100px;
grid-gap: 5px;
}

.grid-item1 {
background: blue;
border: black 5px solid;
grid-column-start: 1;
grid-column-end: 5;
grid-row-start: 1;
grid-row-end: 3;
}
CSS
Fullscreen divs html, body {
width: 100%;
height: 100%;
Sometimes we want to have a div that covers }
the whole screen (to make a webapp),
div {
instead of a scrolling website (more like margin: 0;
regular documents). padding: 0;
}

In that case remember to use percentages to #main {


width: 100%;
define the size of elements, but keep in mind height: 100%;
that percentages are relative to the element's }
parent size, so you must set the size to the
<body> element to use 100%.
Trick to center

Centering divs can be hard sometimes, use this trick:


.horizontal-and-vertical-centering {
display: flex;
justify-content: center;
align-items: center;
}
CSS further reading
There are many more rules for selectors.

Check some of the links to understand them better.

One line layouts tutorials

Understanding the Box Model: a good explanation of how to position the information on your
document.

All CSS Selectors: the CSS selectors specification page.

CSS Transition: how to make animations just using CSS

TailwindCSS: a CSS Framework


Technologies

● HTML
● CSS
● Javascript
Javascript
A regular programming language, easy to start, hard to master.

Allows to give some interactivity to the elements on the web.

Syntax similar to C or Java but with no types.

You can change the content of the HTML or the CSS applied to an element.

You can even send or retrieve information from the internet to update the content
of the web without reloading the page.
Javascript: insert code
There is three ways to execute javascript code in a website:

● Embed the code in the HTML using the <script> tag.

<script> /* some code */ </script>

● Import a Javascript file using the <script> tag:

<script src="file.js" />

● Inject the code on an event inside a tag:

<button onclick="javascript: /*code*/">press me</button>


Javascript example
<html>
<body>
<h1>This is a title</h1>
<script>
var title = document.querySelector("h1");
title.innerHTML = "This is another title";
</script>
</body>
</html>
Javascript API
Javascript comes with a rich API to do many things like:

● Access the DOM (HTML nodes)


● Do HTTP Requests
● Play videos and sounds
● Detect user actions (mouse move, key pressed)
● Launch Threads
● Access the GPU, get the Webcam image, ...

And the API keeps growing with every new update of the standard.

Check the WEB API reference to know more


Javascript: retrieving element
You can get elements from the DOM (HTML tree) using different approaches.

● Crawling the HTML tree (starting from the body, and traversing its children)

● Using a selector (like in CSS)

● Attaching events listeners (calling functions when some actions are


performed)
Javascript: crawling the DOM
From javascript you have different variables that you can access to get
information about the website:

● document: the DOM information (HTML)

● window: the browser window

The document variable allows to crawl the tree:

document.body.children[0] // returns the first node inside body tag


Javascript: using selectors
You can retrieve elements using selectors:

var nodes = document.querySelectorAll("p.intro");

will return an array with all <p class="intro"> nodes in the web.

Or if we have already a node and we want to search inside:

var node = mynode.querySelectorAll("p.intro")


Javascript: modify nodes
From JS you can change the attributes
mynode.id = "intro"; //sets an id
mynode.className = "important"; //set class
mynode.classList.add("good"); //to add to the current classes

Change the content


mynode.innerHTML = "<p>text to show</p>"; //change content

Modify the style (CSS)


mynode.style.color = "red"; //change any css properties

or add the behaviour of a node


mynode.addEventListener("click", function(e) {
//do something
});
Javascript: create nodes
Create elements:
var element = document.createElement("div");

And attach them to the DOM:


document.querySelector("#main").appendChild( element );

Or remove it from its parent:


var element = document.querySelector("foo");
element.parentNode.removeChild( element );

You can clone an element also easily:


var cloned = element.cloneNode(true);
Javascript: hide and show elements
Sometimes it may be useful to hide one element or show another.

You can change an element CSS directly by accessing its property style.

To avoid being displayed on the web change display to "none"

element.style.display = "none"; //hides elements from being rendered


element.style.display = ""; //displays it again
Using Inputs
If you want the user to be able to input some text we use the tag <input>:

<input type="text"/>

There are other inputs, you can check this list.

From Javascript we can attach events like "click" or "keydown".

To read or modify the content of the input:

my_input_element.value = ""; //this will clear the text inside the input
Example of a website
HTML in index.html Javascript in code.js
<link href="style.css" rel="stylesheet"/> //fetch the button from the DOM
<h1>Welcome</h1> var button = document.querySelector(“button”);
<p>
<button>Click me</button> //attach and event when the user clicks it
</p> button.addEventListener(“click”, myfunction);
<script src=”code.js”/>
//create the function that will be called when the
button is pressed
CSS in style.css
function myfunction()
{
h1 { color: #333; } //this shows a popup window
button { alert(“button clicked!”);
border: 2px solid #AAA; }
background-color: #555;
}
Execution flow
It is important to have a clear understanding of
the execution flow of your code.

Scripts are executed when the html is being


parsed.

Be careful accessing the DOM as the DOM


won’t contain all until all the HTML is parsed.

It is good practice to start your code with an init


function called at the end of your HTML.
jQuery
jQuery is a library that makes working with the DOM much easier, using an unified
syntax and taking advantage of selectors:

$("p").remove(); //remove all tags p


$("#main").hide(); //hides the element of id main
$("#main").append("<h1>titulo</h1>") //adds content to an element

$("#wrap").css({ color: "red" }); //change the css

$("button#send").click( function() { /* code */ });

To include this library just add this to your HTML:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
Using the Dev Tools
Press Control + Shift + I (or F12) to open DevTools
Exercise
Create the layout for a
messaging application.

Structured like:

● Main container
○ Messages area
■ message
○ Typing area area
■ input
Further info
API References: DevDocs.io

Selectors: MDN Tutorial

To learn Javascript.

http://codeacademy.com

To learn jQuery:

http://docs.jquery.com/Tutorials

You might also like