Codeloft
0 0 XP

Reference

Short entries with an example for each. When you need every detail, each entry links to the official documentation.

HTML

<h1> to <h6>

Headings, from the most important (h1) to the least (h6). Use one h1 per page.

<h1>Recipes</h1>
<h2>Breakfast</h2>
<h3>Pancakes</h3>

<p>

A paragraph of text.

<p>Fresh bread every morning.</p>

<strong> and <em>

strong marks important words (bold); em adds spoken emphasis (italic).

<p>This is <strong>important</strong>, <em>really</em>.</p>

<a>

A link. href says where it goes.

<a href="https://example.org">Visit</a>

<img>

An image. Always add alt text that describes it.

<img src="cat.jpg" alt="A cat asleep on a chair">

<ul>, <ol>, <li>

Bulleted and numbered lists. Each item is an li.

<ul>
  <li>Milk</li>
  <li>Eggs</li>
</ul>

<table>, <tr>, <th>, <td>

A table of data: rows (tr) made of header (th) and data (td) cells.

<table>
  <tr><th>Day</th><th>Class</th></tr>
  <tr><td>Mon</td><td>Maths</td></tr>
</table>

<div> and <span>

Plain containers: div for blocks, span for words inside a line.

<div class="card">Tea <span class="price">$3</span></div>

<label> and <input>

An input box and the label that names it. Match for and id.

<label for="name">Name</label>
<input id="name">

<select> and <option>

A dropdown list of choices.

<select>
  <option>Small</option>
  <option>Large</option>
</select>

<button>

A clickable button. Inside a form it submits the form.

<button>Save</button>

<header>, <nav>, <main>, <footer>

Page regions that say what each part is for, which helps screen readers and search engines.

<header><h1>Shop</h1></header>
<nav><a href="#">Home</a></nav>
<main><p>Welcome!</p></main>
<footer><p>Open daily</p></footer>

<title> and <meta>

Page information in the head: the tab title, a search description and the phone viewport.

<head>
  <title>Sunny Bakes</title>
  <meta name="description" content="Fresh bread daily.">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

CSS

font-size and font-family

Set the size and typeface of text. Always end the font list with a generic family.

p { font-size: 20px; font-family: Georgia, serif; }

Class and id selectors

A dot selects a class, a # selects an id.

.note { color: gray; }
#top { color: crimson; }

background

Sets the background color or image of an element.

.hero { background: #fef3c7; padding: 20px; }

padding

Space inside an element's border.

.card { padding: 20px; background: #eef2ff; }

margin

Space outside an element's border.

.card { margin: 24px; background: #eef2ff; }

border and border-radius

Draw a line around a box and round its corners.

.card { border: 2px solid navy; border-radius: 12px; padding: 12px; }

display: flex

Lays out an element's children in a row (or a column).

.row { display: flex; gap: 12px; }

display: grid

Places children in rows and columns.

.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

gap

Space between the items in a flex or grid container.

.row { display: flex; gap: 24px; }

@media

Applies styles only when the screen matches a condition, such as a narrow width.

@media (max-width: 600px) {
  body { background: lightyellow; }
}

:hover

Matches an element while the mouse pointer is over it.

a:hover { color: crimson; }

transition

Animates a change in a property over time.

.btn { transition: transform 0.3s; }
.btn:hover { transform: scale(1.2); }

JavaScript

console.log()

Prints values to the console for checking your work.

console.log("Total:", 42);

let and const

Declare variables. Use const when the value never changes.

const tax = 0.08;
let total = 0;
total = 100 + 100 * tax;
console.log(total);

function

Defines reusable code that can take inputs and return a result.

function area(w, h) {
  return w * h;
}
console.log(area(3, 4));

if...else

Runs code only when a condition is true, with an optional fallback.

let age = 15;
if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Not yet");
}

Arrays

Ordered lists of values. Indexes start at 0.

const colors = ["red", "green"];
colors.push("blue");
console.log(colors.length, colors[0]);

for...of

Loops over every item in an array.

for (const fruit of ["apple", "pear"]) {
  console.log(fruit);
}

Objects

Group related values under names (properties).

const pet = { name: "Milo", age: 3 };
pet.age = 4;
console.log(pet.name, pet.age);

Template literals

Strings in backticks that can include values with a dollar sign and braces.

const name = "Ada";
console.log(`Hi, ${name}!`);

querySelector()

Finds the first element that matches a CSS selector.

const title = document.querySelector("h1");
title.textContent = "Found it!";

addEventListener()

Runs a function when something happens, like a click.

button.addEventListener("click", () => {
  button.textContent = "Clicked!";
});

textContent

Reads or replaces the text inside an element.

document.querySelector("#msg").textContent = "Saved";

createElement() and append()

Make a new element, then add it to the page.

const li = document.createElement("li");
li.textContent = "New item";
list.append(li);

C++

std::cout

Writes text and values to the console.

std::cout << "Score: " << score << std::endl;

Variables and types

Every variable has a type: int, double, bool, std::string and more.

int age = 12;
double price = 4.99;
bool ready = true;
std::string name = "Ada";

if / else

Runs code when a condition is true, and other code when it isn't.

if (marks >= 40) {
    std::cout << "Pass";
} else {
    std::cout << "Try again";
}

for loop

Repeats a block a set number of times.

for (int i = 0; i < 3; i++) {
    std::cout << i;
}

Functions

Named, reusable code with a return type and parameters. Define them above main.

int square(int n) {
    return n * n;
}

std::vector

A list that can grow. push_back adds; size counts.

std::vector<int> v = {1, 2};
v.push_back(3);
std::cout << v.size();

class

A blueprint that bundles data and methods. Ends with };

class Dog {
public:
    std::string name;
    void bark() { std::cout << "Woof"; }
};

std::cin

Reads input typed by the user.

int age;
std::cin >> age;

C#

Console.WriteLine()

Prints a line of text to the console.

Console.WriteLine("Ready");

Variables and types

Typed variables: int, double, bool, string and more.

int age = 12;
double price = 4.99;
bool ready = true;
string name = "Ada";

String interpolation

Puts values inside text with $ and braces.

Console.WriteLine($"Total: {total}");

if / else

Runs code only when a condition is true.

if (age >= 18)
{
    Console.WriteLine("You can vote.");
}
else
{
    Console.WriteLine("Not yet.");
}

for and while

Repeat code a set number of times, or while a condition is true.

for (int i = 1; i <= 3; i++)
{
    Console.WriteLine(i);
}

Methods

Named, reusable code inside a class.

static int Square(int n)
{
    return n * n;
}

List<T> and foreach

A list that can grow; foreach visits every item.

List<string> names = new List<string>();
names.Add("Asha");
foreach (string n in names)
{
    Console.WriteLine(n);
}

Classes and objects

A class is a blueprint; new creates an object from it.

class Dog
{
    public string Name;
}

Dog rex = new Dog();
rex.Name = "Rex";

Console.ReadLine()

Reads a line of text typed by the user.

string name = Console.ReadLine();