Week 01 HTML-cheat-sheet 01
Week 01 HTML-cheat-sheet 01
Tag Description
<title> text </title> title shown on page tab
<meta attribute="value" ... /> page metadata
<link href="url" type="text/css" rel="stylesheet" /> links to a CSS style sheet
<script src="url"></script> link to JavaScript code
comment (can appear in head or
<!-- comments -->
body)
1
Tags Used in the <body> Section (Continued)
2
HTML Input Tags
Note that input tags are inline tags.
Tag Description
<button type="type"> clickable button
content type can be submit, reset, button
</button>
<input type="type" name="name"> form input tag
content type can be text, number, submit, reset,
</input> checkbox, radio, file, etc.
<textarea rows="num" cols="num"> multi-line text input box
initial text
</textarea>
<label> text </label> clickable text label around a form control
<select> drop-down selection box (select);
<option> text </option> each option within the box (option);
<option> a labeled group of options (optgroup);
<optgroup label="text">
<option> text </option>
<option> text </option>
</optgroup>
...
</select>
<fieldset> a grouped set of form fields with a legend
<legend> text </legend> content
</fieldset>
3
CSS
For the following property and value tables, anything emphasized represents values that should be replaced with
specific units (e.g., length should be replaced with a px, pt, or em for many properties, and color should be
replaced with a valid color value such as a hex or rgb code).
A use of | refers to separation of possible values (where you cannot provide two of these possible values for one
property) and [value value value] refers to a grouping of possible values that can optionally be used together
(e.g., [h-shadow v-shadow blur spread color] for box-shadow).
Selector Types
Background Styles
Property Values
background-attachment scroll | fixed
background-color color | transparent
background-image url | none
background-origin border-box | padding-box | content-box
background-position top left | top center | top right |
center left | center center | center right |
bottom left | bottom center | bottom right
[x-% y-% ] | [x-pos y-pos ]
background-size length | % | auto | cover | contain
background-repeat repeat | repeat-x | repeat-y | no-repeat
background-attachment scroll | fixed
4
Border Styles
Note: Replace ’*’ with any side of the border (top, right, left, bottom) for the desired effect.
Example style: ’border: 2px solid red’ applies a solid red border with a width of 2px to all four sides of
the element, while ’border-left: 2px solid red’ only applies that border to the left border’.
Property Values
border, border-* (shorthand) border-width, border-*-width
border-style, border-*-style
border-color, border-*-color
border-width, border-*-width thin | medium | thick | length
border-style, border-*-style none | hidden | dotted | dashed | solid |
double | groove | rigid | inset | outset
border-color, border-*-color color
box-shadow none | inset | [h-shadow v-shadow blur spread color ]
border-radius length
Property Values
font-style normal | italic | oblique | inherit
font-family fontname
font-size length | %
font-weight normal | bold | inherit
text-align left | right | center | justify
text-decoration none | [underline overline line-through blink]
text-shadow none | [color length ]
letter-spacing, word-spacing normal | length | %
text-indent length | %
text-transform none | capitalize | uppercase | lowercase
list-style-type none | asterisks | box | check | diamond | disc | hyphen |
square | decimal | lower-roman | upper-roman |
lower-alpha | upper-alpha | lower-greek | upper-greek |
lower-latin | upper-latin | footnotes
list-style-image none | url
Color Values
Value Description
colorname standard name of color, such as red, blue, purple, etc.
rgb(redvalue, greenvalue,
Example: red = rgb(255, 0, 0) or red = rgb(100%, 0, 0)
bluevalue)
#RRGGBB Example: red = #FF0000
5
Box Model
Property Values
float left | right | none
height, width auto | length | %
min-height, max-height none | length | %
min-width, max-width
margin, margin-* auto | length | %
padding, padding-* length | %
display none | inline | block | inline-block | flex |
list-item | compact | table | inline-table
overflow, overflow-x, overflow-y visible | hidden | scroll |
auto | no-display | no-content
clear left | right | both | none
Flex Box
(on next page)
6
Property Values Element Type Description
display flex Flex Container Sets all children to become ’flex-items’
justify-content Flex container Indicates how to position the flex-items
in the parent container
flex-start
flex-end
center
space-around
space-between
flex-direction row | row-reverse | Flex container Indicates if the container flows horizon-
column | column-reverse tally (row) or vertically (column)
align-items stretch (default) Flex container Indicates how to space the items inside
the container along the cross axis
flex-start
flex-end
center
baseline
flex-basis auto (default) | length | Both Specifies the default size of an element
% before the extra space is distributed
among the flex-items
order number Flex item Specifies the order in which the ele-
ment appears in the flex container (by
default, flex items are laid out in the
source order)
align-self flex-end | flex-start | Flex item Indicates where to place this specific
center | baseline | item along the cross axis
stretch (default)
7
JavaScript
Window Methods and Properties
Method/Property Description
document Returns a reference to the document contained in the window
Returns an object that reports the values of all CSS properties of an
getComputedStyle(element) element after applying active stylesheets and resolving any basic compu-
tation those values may contain
Method/Property Description
getElementById(id) Returns a DOM object whose id property matches the specified string
Returns an array-like object of all elements which have all of the given
getElementByClassName(cn)
class names
getElementByName(name) Returns an collection of all elements which have all of the given name
Returns the first DOM element that matches the specified selector, or
querySelector(sel)
group of selectors. If no matches are found, null is returned
Returns a list of the document’s elements that match the specified group
querySelectorAll(sel)
of selectors.
getElementsByTagName(name) Returns a NodeList containing all elements with the specified tag name
createElement(elType) Creates and returns an Element node
createTextNode Creates and returns a Text node
Method/Property Description
children Returns a collection of an element’s child elements
parentNode Returns the parent node of an element
classList Returns the class name(s) of an element
className Sets or returns the value of the class attribute of an element
appendChild(child) Adds a new child node, to an element as the last child node
addEventListener(event, fn) Attaches an event handler to the specified element
getAttribute(attr) Returns the specified attribute value attr of an element node
innerHTML Sets or returns the content of an element
innerText Sets or returns the text content of the specified node
id Sets or returns the value of the id attribute of an element
removeChild(child) Removes a child node from an element
8
Other DOM Element Properties
Recall that if you have an HTML element on your page that has attributes, you can set those properties through
JavaScript as well. For instance if your
Your could do the following in your JavaScript code (using the $ alias for document.getElementById):
Example DOM Element attributes include (other than src, and alt above) are:
Property Description
disabled Whether or not this DOM element is disabled on the page
value The value of the value attribute of a text field
name The value of the name attribute of a a form element
href The href for a link or a tag
Method/Property Description
Adds specified class values. These values are ignored if they already exist
add(class)
in the list
remove(class) Removes the specified class value
Toggles the listed class value. If the class exists, then removes it and
toggle(class)
returns false, if it did not exist in the list add it and return true
Returns true if the specified class value is exists in the classList for this
contains(class)
element
Method/Property Description
target Returns the element that triggered the event
type Returns the name of the event
Returns the horizontal coordinate of the mouse pointer, relative to the
offsetX
DOM element clicked
Returns the vertical coordinate of the mouse pointer, relative to the DOM
offsetY
element clicked
stopPropagation Prevents further propagation of an event during event flow
9
Event Types
Function Description
parse(string) Returns the given string of JSON data as the equivalent JavaScript object
stringify(object) Returns the given object as a string of JSON data
Function Description
function parses a string argument and returns an integer of the specified
parseInt(string, radix)
radix (the base in mathematical numeral systems)
console.log(string) Writes the string to the JavaScript console
Method/Property Description
length Sets or returns the number of elements in an array
push(el) Adds new elements to the end of an array and returns the new length
pop() Removes and returns the last element of an array
unshift(el) Adds new elements to the beginning of an array and returns the new length
shift() Removes and returns the first element in an array
sort() Sorts the elements of an array
slice(start, end) Selects a part of an array and returns the new array
join() Joins all elements of an array into a string
concat(list2, ...) Joins two or more arrays and returns a copy of the joined arrays
toString() Converts an array to a string and returns the result
indexOf(el) Returns the index of the element in the array, or -1 if not found
10
JavaScript String Methods and Properties
Method/Property Description
length Returns the length of a string
charAt(index) Returns the character at the specified index
Returns the position of the first found occurrence of a specified value in
indexOf(string)
a string
split(delimiter) Splits a string into an array of substrings
substring(start, end) Extracts the characters from a string between two specified indices
trim() Removes whitespace from both ends of a string
toLowerCase() Returns a lowercase version of a string
toUpperCase() Returns an uppercase version of a string
concat(str2, ...) Joins two or more strings and returns a new joined string
Method Description
Executes a function after waiting a specified number of milliseconds.
setTimeout(fn, ms)
Returns a value representing the ID of the timeout being set.
Repeats a given function at every given time-interval. Returns a value
setInterval(fn, ms)
representing the ID of the interval being set.
clearTimeout(id) Stops the execution of the function specified by id
clearInterval(id) Stops the execution of the functions specified by id
Method Description
Math.random() Returns a double between 0 (inclusive) and 1 (exclusive)
Math.abs(n) Returns the absolute value of n
Math.min(a, b, ...) Returns the smallest of 0 or more numbers
Math.max(a, b, ...) Returns the largest of 0 or more numbers
Math.round(n) Returns the value of n rounded to the nearest integer
Math.ceil(n) Returns the smallest integer greater than or equal to n
Math.floor(n) Returns the largest integer less than or equal to n
Math.pow(n, e) Returns the base n to the exponent e power, that is, ne
Math.sqrt(n) Returns the square root of n (NaN if n is negative)
11
The Module Pattern
Whenever writing JavaScript, you should use the module pattern, wrapping the content of the code (window.onload
handler and other functions) in an anonymous function. Below is a template for reference:
(function() {
// any module-globals (limit the use of these when possible)
window.onload = function() {
...
};
// other functions
})();
function $(id) {
return document.getElementById(id);
}
function qs(selector) {
return document.querySelector(selector);
}
function qsa(selector) {
return document.querySelectorAll(selector);
}
function callAjax(){
let url = ..... // put url string here
fetch(url) // don't worry about cloud9 credentials
.then(checkStatus)
.then(JSON.parse) //optional line for processing json
.then(function(responseJSON) {
//success: do something with the responseJSON
})
.catch(function(error) {
//error: do something with error
});
}
12