Post

HTML, CSS, and JavaScript — A Practical Overview

HTML, CSS, and JavaScript — A Practical Overview
Table of Contents

🧱 What is HTML?

🔹 HTML = HyperText Markup Language

HTML is the skeleton of a web page. It defines the structure and content—what elements appear on the page like text, images, buttons, links, etc. An HTML page is entirely made of:

  • HTML Tags (elements) → like <div>, <h1>, <a>, etc.
  • Attributes → like class, id, href, src, etc.
  • Text or content inside the tags

Basic Structure of HTML

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html>
  <head>
    <title>My First Page</title>
  </head>
  <body>
    <h1>Hello, World!</h1>
    <p>This is a paragraph.</p>
  </body>
</html>

Explanation:

  • <!DOCTYPE html> → Declares that this is an HTML5 document
  • <html> → Root of the HTML document
  • <head> → Metadata (not visible content), includes title, links to CSS/JS (optional but recommended)
  • <title> → Title shown in browser tab
  • <body> → Visible content of the webpage

What is <!DOCTYPE html> and Why Should You Use It?

  • <!DOCTYPE html> is a declaration, not an HTML tag.It tells the browser the document is written in HTML5.
  • Ensures the browser uses standards mode (modern rendering) else quirks mode (legacy behavior).
  • Quirks mode can break layouts and CSS, especially box sizing so include <!DOCTYPE html> for consistent rendering.

Common HTML Tags (Elements)

TagDescriptionExample
<html>Root element<html></html>
<head>Metadata (CSS, JS, title)<head></head>
<body>Page content<body></body>
<h1> – <h6>Headings<h1>Main Title</h1>
<p>Paragraph<p>Hello world</p>
<a>Link<a href="https://google.com">Visit</a>
<img>Image<img src="img.jpg" alt="desc" />
<ul>, <ol>, <li>Lists<ul><li>Item</li></ul>
<div>Container/block<div>Box</div>
<span>Inline text container<span>Text</span>
<input>Input field<input type="text" />
<button>Button<button>Click</button>
<form>Wraps inputs<form>...</form>
<script>JS inside HTML<script>code</script>
<link>Link CSS file (mostly)<link rel="stylesheet" href="style.css" />

Attributes in HTML

Attributes go inside tags to give extra information.

Example:

1
<a href="https://openai.com" target="_blank">Visit OpenAI</a>

Common Attributes:

AttributeDescriptionExample
classUsed for styling (CSS), can be reused<div class="container">...</div>
idUnique identifier (CSS or JS)<h1 id="main-title">Title</h1>
hrefURL in links<a href="https://openai.com">OpenAI</a>
srcSource for images or scripts<img src="image.jpg" alt="desc" />
altImage description (SEO, accessibility)<img alt="Logo">
typeType of input or button<input type="text" />
valuePre-filled value (input/button)<input value="Hello" />
placeholderHint text in input<input placeholder="Enter name" />
onclick, onchangeTrigger JS functions<button onclick="alert('Hi!')">Click</button>
targetHow link opens (_blank = open new tab)<a href="url" target="_blank">Link</a>

🎨 CSS: Styling

CSS changes how HTML looks.

✅ Ways to Use CSS

External (best method) ✅ Recommended

1
<link rel="stylesheet" href="style.css" />

In style.css:

1
2
3
4
h1 {
 font-size: 40px;
 color: purple;
}

Internal (inside <style> in <head>) ⚠️ May be recommended

1
2
3
4
5
6
7
8
9
<head>
 <meta charset="UTF-8">
 <title>Text Block Example</title>
  <style>
    p {
      color: blue;
    }
  </style>
</head>

Inline (inside an HTML tag) ❌ Not recommended

1
<p style="color: red;">Red text</p>

CSS Selectors

SelectorTargetsExample Usage
tagAll elements of that typep { color: red; } → All <p> tags turn red
.classAll elements with that class.box { padding: 10px; } → Elements with class="box" get padding
#idOne element with that id#hero { background: black; } → The element with id="hero" has a black background
div pAll <p> inside <div>div p { font-size: 14px; } → Only <p> tags inside <div> are affected
*Everything* { margin: 0; } → Removes default margin from all elements
Common CSS Properties
PropertyDescription
colorText color
background-colorBackground color
font-size, font-familyText size and font
margin, paddingSpace outside/inside elements
borderBorder style
width, heightSize of elements
displayLayout behavior (block, flex, grid)
positionPositioning method (static, absolute, etc.)
text-alignAligns text (left, center, etc.)
justify-content, align-itemsAlignment in flexbox containers

Example

1
<div class="card" id="firstCard">Card Content</div>
1
2
3
4
5
6
7
8
9
.card {
  background-color: #f3f3f3;
  padding: 20px;
  border: 1px solid gray;
}

#firstCard {
  border-color: red;
}

output:

Card Content

⚙️ What is JavaScript (JS)?

JS = Programming language of the web

  • HTML → Structure
  • CSS → Style
  • JS → Behavior

JavaScript makes the page interactive (buttons, animations, forms, etc.)

It uses the document objec if any html document object needes to be interacted with else built in functions or custome functions

JS Example in html

1
2
3
4
5
6
7
8
<button onclick="changeText()">Click Me</button>
<p id="demo">Hello</p>

<script>
  function changeText() {
    document.getElementById("demo").innerText = "You clicked!";
  }
</script>

When the button is clicked, JS changes the text of the paragraph.

✅ Ways to Use JS

External (best practice) ✅

1
<script src="script.js"></script>

In script.js:

1
2
3
function sayHi() {
 alert("Hello from external file!");
}

Internal (inside <script> in HTML) can be used if needed

1
2
3
4
5
<script>
 function sayHi() {
   alert("Hello!");
 }
</script>

Inline (bad practice) but can be done

1
<button onclick="alert('Hi')">Click</button>

What is the DOM?

🔹 DOM = Document Object Model

The DOM is a virtual representation of your HTML page that JavaScript can read and modify.

Think of it like this:

  • HTML is the code of the page.
  • The DOM is a live, tree-like structure created by the browser from that HTML.

🔸 Example:

HTML:

1
2
3
4
<body>
  <h1>Hello</h1>
  <p>This is a paragraph</p>
</body>

In the DOM, this becomes a tree like:

1
2
3
<body>
 ├── <h1>Hello</h1>
 └── <p>This is a paragraph</p>

JavaScript uses the DOM to access, change, or create elements on your page.

How DOM is acceed:- via document

🔹 document = JavaScript’s way of accessing the DOM

It’s a built-in object in JS that represents your entire web page.You use it to interact with HTML elements.

🔸 Common document Methods:

JS Method / PropertyWhat It DoesExample / Source of Element
document.getElementById("id")Gets one element by its idconst element = document.getElementById("header");
document.getElementsByClassName("class")Gets multiple elements with a classconst element = document.getElementsByClassName("item")[0];
document.querySelector("selector")Gets the first element matching a selectorconst element = document.querySelector(".my-class");
document.querySelectorAll("selector")Gets all elements matching a selectorconst element = document.querySelectorAll(".card")[0];
document.createElement("tag")Creates a new HTML elementconst element = document.createElement("div");
appendChild(element)Adds the element to the DOMdocument.body.appendChild(element);
element.innerTextGets/sets text content of the elementelement.innerText = "Hello!";
element.innerHTMLGets/sets HTML content of the elementelement.innerHTML = "<b>Hi</b>";
element.setAttribute("attr", "value")Sets/updates an attribute on the elementelement.setAttribute("href", "https://example.com");
element.classList.add("class")Adds a CSS class to the elementelement.classList.add("active");
element.addEventListener("event", fn)Adds an event listener to the elementelement.addEventListener("click", handleClick);

🔸 Example:

1
<p id="greet">Hello</p>
1
2
let p = document.getElementById("greet");
p.innerText = "Hi there!";

Every HTML page loaded in a browser automatically gets a DOM.The browser reads your HTML, parses it, and builds the DOM.


🔄 11. How They All Connect

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
  <head>
    <title>Full Web Example</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <h1 class="heading">Welcome!</h1>
    <button onclick="sayHi()">Click me</button>

    <script src="script.js"></script>
  </body>
</html>

Then in style.css:

1
2
3
4
.heading {
  color: green;
  font-size: 40px;
}

And in script.js:

1
2
3
function sayHi() {
  alert("Hello from JavaScript!");
}

  1. Is <link rel="stylesheet" href="style.css" /> Needed?

Yes, it’s required.
Without rel="stylesheet", the browser won’t apply the CSS.

1
2
<link rel="stylesheet" href="style.css" />

rel means relationship, telling the browser this is a CSS stylesheet or some other relationship

Normaly <link rel=stylesheet is use often for css linking from local or remote Example:

  • Google Fonts or Bootstrap:
    1
    2
    
     <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet" />
     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" />
    
  • local <link href="/assets/css/style.css" rel="stylesheet" />

  1. Is <link> Only for CSS?

No, <link> can link other resources too:

UsageExampleDescription
CSS<link rel="stylesheet" href="style.css">CSS stylesheet
Favicon<link rel="icon" href="favicon.ico">Website icon
Preload<link rel="preload" href="font.woff2" as="font" ...>Load fonts/images early
Manifest<link rel="manifest" href="manifest.json">Web app settings
  1. Can You Include Multiple CSS/JS Files?

Yes! It’s common practice.

CSS:

1
2
3
<link rel="stylesheet" href="reset.css" />
<link rel="stylesheet" href="layout.css" />
<link rel="stylesheet" href="theme.css" />

CSS loads top to bottom, so later styles override earlier ones.

Javascript:

1
2
3
<script src="utils.js"></script>
<script src="app.js"></script>
<script src="main.js"></script>

JS loads top to bottom, so earlier scripts are available to later ones.

This post is licensed under CC BY 4.0 by the author.