Frontend

HTML & CSS

Showing 11 topics

Basic Document Elements

  • !DOCTYPE, html, head, meta, title, link
  • body
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Page Title</title>
    <link rel="stylesheet" href="styles.css" />
    <script src="script.js" defer></script>
  </head>
  <body>
    <!-- Content goes here -->
  </body>
</html>

Page Layout Elements (HTML5 Semantic)

  • header, nav, ul, li
  • main, section, article, header, h1
  • aside, h3, p
  • footer
<header>
  <nav>
    <ul>
      <li><a href="#home">Home</a></li>
      <li><a href="#about">About</a></li>
    </ul>
  </nav>
</header>

<main>
  <section>
    <article>
      <header>
        <h1>Article Title</h1>
        <time datetime="2025-09-27">September 27, 2025</time>
      </header>
      <p>Article content...</p>
      <footer>
        <p>Author: John Doe</p>
      </footer>
    </article>
  </section>

  <aside>
    <h3>Sidebar</h3>
    <p>Related links or ads</p>
  </aside>
</main>

<footer>
  <p>&copy; 2025 Company Name</p>
</footer>

Text Content Elements

  • h1, h6, p, strong, pre, code
<h1>Main Heading</h1>
<h6>Smallest Heading</h6>

<p>Regular paragraph text</p>
<strong>Important text (bold)</strong>
<em>Emphasized text (italic)</em>
<pre>
    Preformatted text
    preserves   spaces
    and line breaks
</pre>

<code> function example() { console.log("Hello World"); } </code>

Unordered & Ordered Lists

  • ul, ol, li
<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>
    Third item
    <ul>
      <li>Nested item</li>
      <li>Another nested item</li>
    </ul>
  </li>
</ul>

<ol type="a">
  <li>Item a</li>
  <li>Item b</li>
</ol>

Links & Media

  • a herf, img src, video controls, source
<a href="https://example.com">External link</a>
<a href="/page.html">Internal link</a>
<a href="#section">Anchor link</a>

<img src="image.jpg" alt="Description of image" width="300" height="200" />
<video controls width="400" height="300">
  <source src="video.mp4" type="video/mp4" />
  Your browser does not support the video element.
</video>

Tables

  • table, caption
  • thead, tr, th
  • tbody, tr, td
<table>
  <caption>
    Month
  </caption>
  <thead>
    <tr>
      <th>Month</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>January</td>
    </tr>
    <tr>
      <td>Febuary</td>
    </tr>
  </tbody>
</table>

Forms

  • form action method, fieldset, legend
  • label for, input type id name placeholder
  • button type
<form action="submit" method="post">
  <fieldset>
    <legend>Personal Information</legend>

    <label for="name">Full Name:</label>
    <input type="text" id="name" name="name" required />

    <input type="number" min="0" max="100" step="1" />
    <input type="search" placeholder="Search..." />
  </fieldset>

  <button type="submit">Submit</button>
  <button type="reset">Reset</button>
</form>
  • checkbox, radios
<!-- Checkboxes -->
<label>
  <input type="checkbox" name="interests" value="sports" /> Sports
</label>
<label> <input type="checkbox" name="interests" value="music" /> Music </label>

<!-- Radio buttons -->
<label> <input type="radio" name="gender" value="male" /> Male </label>
<label> <input type="radio" name="gender" value="female" /> Female </label>
  • label, select, option value
<label for="country">Country:</label>
<select id="country" name="country">
  <option value="">Choose a country</option>
  <option value="us">United States</option>
  <option value="ca">Canada</option>
  <option value="uk">United Kingdom</option>
</select>

There're more...

Convert Node List to Array

const pArray = [...document.querySelectorAll("p")];

console.log(pArray);
// [
//   <p id="first">First paragraph</p>,
//   <p class="highlight">Second paragraph</p>,
//   <p><strong>Third paragraph</strong></p>
// ]

console.log(pArray.length); // 3

console.log(pArray[0].id); // "first"

console.log(pArray[1].className); // "highlight"

console.log(pArray[2].textContent); // "Third paragraph", preferred

console.log(pArray[2].innerHTML); // "<strong>Third paragraph</strong>"

.innerHTML vs .outerHTML vs .innerText vs .textContent

<div id="example">
    Hello <span style="display:none">Hidden</span> <b>World</b>!
</div>;

const el = document.getElementById("example");

console.log(el.innerText); // "Hello World!"  (the <span> is hidden), what user actually sees

console.log(el.textContent); // "Hello Hidden World!", fastest

console.log(el.innerHTML);
// "Hello <span style="display:none">Hidden</span> <b>World</b>!"

console.log(el.outerHTML);
// "<div id="example">Hello <span style="display:none">Hidden</span> <b>World</b>!</div>"

Common CSS Properties Reference

  • color, background-color, font-weight, text-align, display, cursor, border, box-sizing
.container {
  color: red; /* text color */
  background-color: lightgray;
  background-image: url("image.jpg");

  font-size: 16px;
  font-weight: bold;

  text-align: center; /* left, right, center, justify */
  text-decoration: underline; /* none, underline, overline, line-through */
  text-transform: uppercase; /* lowercase, uppercase, capitalize */

  /* same for height */
  width: 100px;
  max-width: 1000px;
  min-width: 1000px;
}
.element {
  /*  same for padding */
  margin: 20px; /* All sides */
  margin: 10px 20px; /* Top/bottom, Left/right */
  margin: 10px 15px 20px 25px; /* Top, Right, Bottom, Left (margin-bottom etc)*/

  border: 1px solid black; /* width style color, none */
  border-radius: 5px;

  display: block; /* block, inline, inline-block, none */
  display: flex; /* Flexbox */
  display: grid; /* CSS Grid */
  visibility: hidden; /* visible, hidden */
  position: static; /* static, relative, absolute, fixed, sticky */

  cursor: pointer; /* default, pointer, text, wait, help, not-allowed */

  box-sizing: content-box; /* Default - padding/border added to width */
  box-sizing: border-box; /* Width includes padding and border */
}
    /* Relative units */
    em    /* Relative to parent font-size */
    rem   /* Relative to root font-size */
    %     /* Percentage of parent */
    vw    /* Viewport width (1vw = 1% of viewport width) */
    vh    /* Viewport height (1vh = 1% of viewport height) */

Basic Selectors

Universal Selector(*), Type Selector, Class Selector(.), ID Selector

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

h1 {
  font-size: 2em;
}

.btn {
  padding: 10px 15px;
  border: none;
}

#header {
  background-color: navy;
}

Attribute Selectors

[disabled] {
  opacity: 0.5;
}

[type="text"] {
  border: 1px solid #ccc;
}

/* Starts with */
[href^="https"] {
  color: green;
}

Combinators

Descendant Combinator (space), Child Combinator (>), Adjacent Sibling Combinator (+), General Sibling Combinator (~)

article p {
  line-height: 1.6;
}

Pseudo-Classes

select elements based on their state or position

a:hover {
  color: red;
}
a:visited {
  color: purple;
}
li:first-child {
  font-weight: bold;
}

Pseudo-Elements

lets you style a portion of an element

p::first-letter {
  font-size: 3em;
  float: left;
}

Flexbox

Center an element

.container {
  display: flex; /* default flex-direction: row */
  justify-content: center; /* main axis*/
  align-items: center; /* cross axis*/
}

Main Axis Controller: justify-content

Alignment

  • flex-start (default): 123------
  • flex-end: ------123
  • center: ---123---

Space

  • space-between: 1---2---3
  • space-around: -1--2--3-
  • space-evenly: --1--2--3--

Cross Axis Controller: align-items

  • stretch (default): stretch to fill the container’s cross-axis size
  • flex-start:
  • flex-end:
  • center:
.special {
  /*  ignore align-items */
  align-self: flex-start;
}

A good practice

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

Flexbox Layout

3 key properties for space distribution

  • flex-grow
  • flex-shrink
  • flex-basis: decides grow or shrink
.container {
  display: flex; /* default: row */
}

/*  flex-grow flex-shrink flex-basis*/
.A {
  flex: 1 1 300px; /* width = 300px starting point */
}

Space Enough: flex-grow

  • remaining = container width - total basis
  • individual space = grow / total grow * remaining
  • final width = flex-basis + individual space

Space Not Enough: flex-grow

  • excess = container - total basis
  • individual space = grow / total grow * remaining
  • final width = flex-basis - individual space

Flexbox Wrap

By default, Flexbox try to put all element in the same line, flex-wrap: nowrap;

.container {
  display: flex;

  /*  allow elements to put in next line, and align-content */
  flex-wrap: wrap;

  /*  properties similar justify-content: flex-end, center space-between, space-around... handle space between lines*/
  align-content: flex-start;
}

Three Column Layout

<div class="container">
  <div class="left">Left</div>
  <div class="middle">Middle</div>
  <div class="right">Right</div>
</div>
.container {
  display: flex;

  /* center screen */
  justify-content: center;
}

.left,
.right {
  width: 200px;

  /*  prevent shrinking when screen size decrease*/
  flex-shrink: 0;
  min-width: 200px;
}

.middle {
  flex-grow: 1; /* fill the remaining space */

  /* prevent expanding when screen size increase */
  max-width: 1000px;
}