CSS JavaScript
CSS JavaScript
---
CSS is used to style and layout web pages. It controls the appearance of elements in
HTML, from fonts and colors to layout and positioning.
1. CSS Syntax
selector {
property: value;
}
Example:
p{
color: blue;
font-size: 16px;
}
This CSS code selects all <p> (paragraph) elements and makes the text color blue and
the font size 16px.
2. CSS Selectors
CSS selectors specify which HTML elements to style. Here are some common types
of selectors:
h1 { color: red; }
* { margin: 0; padding: 0; }
3. CSS Properties
Text Styling
p { color: blue; }
h1 { font-size: 24px; }
p { text-align: center; }
Box Model (Margins, Padding, Borders, Width, Height) Every element in HTML is
considered as a rectangular box. The box model consists of:
Padding: Space inside the element, between the border and content.
Backgrounds
Positioning CSS positioning properties allow you to position elements on the web
page:
Example:
.box {
position: relative;
top: 50px;
left: 100px;
}
Flexbox Layout: Flexbox is a layout mode designed for arranging items in rows or
columns.
Example:
.container {
display: flex;
justify-content: center;
align-items: center;
}
4. Media Queries
Media queries make web pages responsive. They allow you to apply different styles
based on screen size, orientation, or other characteristics.
Example:
This CSS rule changes the background color to light blue when the screen width is
600px or less.
---
JavaScript
1. JavaScript Basics
JavaScript is embedded in HTML using the <script> tag. You can either write
JavaScript directly inside the HTML file or link to an external .js file.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Example</title>
<script>
function showMessage() {
alert("Hello, world!");
}
</script>
</head>
<body>
<button onclick="showMessage()">Click me</button>
</body>
</html>
Here, when the button is clicked, it triggers the showMessage() function, which
displays an alert box saying "Hello, world!".
Variables store data that can be used later. You define a variable using var, let, or
const.
3. Functions
Functions are blocks of code that perform specific tasks. You can define a function
using the function keyword.
function greet() {
console.log("Hello!");
}
function greet(name) {
console.log("Hello, " + name);
}
4. DOM Manipulation
The DOM (Document Object Model) represents the HTML structure of the web page.
JavaScript can interact with the DOM to change content, structure, or style
dynamically.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>DOM Manipulation Example</title>
<script>
function changeText() {
document.getElementById("demo").innerHTML = "Text changed!";
}
</script>
</head>
<body>
<p id="demo">This is a paragraph.</p>
<button onclick="changeText()">Change Text</button>
</body>
</html>
In this example, clicking the button changes the text inside the paragraph.
5. Events
JavaScript events are actions or occurrences that happen in the browser, like clicking
a button or pressing a key.
document.getElementById("myButton").addEventListener("click", function() {
alert("Button clicked!");
});
6. Conditional Statements
7. Loops
For Loop:
While Loop:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
8. Arrays
9. Objects
let person = {
firstName: "John",
lastName: "Doe",
age: 25
};
You can combine CSS and JavaScript to create interactive and dynamic user
experiences. For example, a form validation system that checks if the user has entered
valid data:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.error { color: red; }
</style>
<script>
function validateForm() {
let name = document.getElementById("name").value;
if (name === "") {
document.getElementById("error").innerHTML = "Name cannot be empty!";
return false;
} else {
document.getElementById("error").innerHTML = "";
return true;
}