HTML5&CSS
HTML5&CSS
HTML5 Basics
Q1: What is HTML5?
• HTML5 is the latest version of HyperText Markup Language (HTML) used for
structuring web content.
• It includes new semantic elements, multimedia support, and APIs.
• Semantic elements clearly de ne their meaning for both browsers and developers.
Tag Description
<header> De nes a header section
<nav> De nes navigation links
<section
De nes a section in a document
>
<article
De nes independent content
>
<aside> De nes sidebar content
De nes the footer of a
<footer> document
<header>
<h1>Welcome to My Website</h1>
</header>
<nav>
<a href="#">Home</a> | <a href="#">Contact</a>
</nav>
<section>
<article>
<h2>Article Title</h2>
<p>Article content goes here...</p>
fi
fi
fi
fi
fi
fi
fi
</article>
</section>
<footer>
<p>© 2025 My Website</p>
</footer>
Featur
e
<section> <article>
Purpos Groups related Represents independent
e content content
Usage Chapters, topics Blog posts, news articles
<section>
<h2>About Us</h2>
<p>Details about our company...</p>
</section>
<article>
<h2>Latest News</h2>
<p>New features added to our product...</p>
</article>
• Web Form 2.0 is an enhancement in HTML5 that provides new input types, attributes, and
validation features.
Attribute Description
autocomplet
Suggests previously entered values
e
Automatically focuses on input
autofocus eld
required Makes input mandatory
placeholder Displays hint text inside input
pattern De nes a custom validation pattern
<form>
<input type="text" placeholder="Enter your name"
required>
<input type="password" pattern="[a-zA-Z0-9]{8,}"
title="At least 8 characters">
</form>
// Retrieve data
console.log(localStorage.getItem("username")); // Output:
Alice
// Remove data
localStorage.removeItem("username");
// Store data
sessionStorage.setItem("sessionUser", "Bob");
// Retrieve data
console.log(sessionStorage.getItem("sessionUser")); //
Output: Bob
// Remove data
sessionStorage.removeItem("sessionUser");
4. Geolocation API
Q13: What is the Geolocation API in HTML5?
Q14: How do you get the user’s current location using the Geolocation API?
navigator.geolocation.getCurrentPosition(
position => {
fi
console.log("Latitude:", position.coords.latitude);
console.log("Longitude:", position.coords.longitude);
},
error => {
console.error("Error:", error.message);
}
);
navigator.geolocation.getCurrentPosition(
showPosition,
showError,
{ enableHighAccuracy: true, timeout: 5000 }
);
function showPosition(position) {
console.log("Lat: " + position.coords.latitude);
console.log("Long: " + position.coords.longitude);
}
function showError(error) {
console.error("Error:", error.message);
}
navigator.geolocation.watchPosition(position => {
fi
console.log("Updated Latitude:",
position.coords.latitude);
});
h1 {
color: blue;
font-size: 24px;
}
2. CSS Selectors
Q3: What are the different types of CSS selectors?
/* Universal Selector */
* { margin: 0; padding: 0; }
/* Element Selector */
h1 { color: red; }
/* Class Selector */
.card { border: 1px solid black; }
/* ID Selector */
#main-title { font-size: 24px; }
/* Group Selector */
h1, h2 { text-align: center; }
/* Descendant Selector */
div p { font-size: 18px; }
Example:
/* Class - Reusable */
fi
fi
fi
.card { background: lightgray; }
/* ID - Unique */
#header { background: yellow; }
3. Styling Basics
Q5: How to change text color and background color?
h1 {
color: blue; /* Text color */
background-color: yellow; /* Background color */
}
p {
font-size: 18px;
font-family: Arial, sans-serif;
}
.box {
border: 2px solid black; /* Solid border */
border-radius: 10px; /* Rounded corners */
}
Property Description
Space outside an
margin element
paddin
Space inside an element
g
Example:
.container {
margin: 20px; /* Space outside */
padding: 10px; /* Space inside */
}
Position Description
static Default position
relativ
Moves relative to its normal position
e
absolut Moves relative to the nearest positioned
e ancestor
fixed Stays in the same position on scroll
sticky Sticks when scrolling reaches a certain point
.fixed-box {
position: fixed;
top: 10px;
right: 10px;
}