The basic structure of an HTML document starts with the <!DOCTYPE html>
declaration, followed by the <html>
tag that encloses the entire page. Inside the <html>
tag, we have the <head>
and <body>
sections.
<!DOCTYPE html>
<html>
<head>
<title>My First Web Page</title>
</head>
<body>
<h1>Welcome to My Web Page</h1>
<p>This is a sample paragraph.</p>
</body>
</html>
In this example, the <title>
tag in the <head>
section sets the title of the web page that appears in the browser tab. The <h1>
tag is used for the main heading, and the <p>
tag is for a paragraph.
CSS selectors are used to target specific HTML elements to apply styles. A CSS rule consists of a selector and a declaration block.
/* Target all <p> elements */
p {
color: blue;
font - size: 16px;
}
In this example, the selector is p
, which targets all <p>
elements on the page. The declaration block contains two properties (color
and font - size
) and their respective values.
To apply this CSS to an HTML page, we can use an internal style sheet in the <head>
section:
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: blue;
font - size: 16px;
}
</style>
<title>Styled Web Page</title>
</head>
<body>
<p>This is a styled paragraph.</p>
</body>
</html>
Semantic elements in HTML provide meaning to the structure of the web page. Instead of using generic <div>
tags everywhere, we can use tags like <header>
, <nav>
, <main>
, <article>
, <section>
, and <footer>
.
<!DOCTYPE html>
<html>
<head>
<title>Semantic HTML Example</title>
</head>
<body>
<header>
<h1>My Website</h1>
</header>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
</ul>
</nav>
<main>
<article>
<h2>Article Title</h2>
<p>Article content goes here.</p>
</article>
</main>
<footer>
<p>© 2024 My Website</p>
</footer>
</body>
</html>
Semantic elements improve accessibility and search engine optimization.
The CSS box model describes how elements are laid out on a web page. Each element has content, padding, a border, and a margin.
.box {
width: 200px;
padding: 20px;
border: 1px solid black;
margin: 10px;
}
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
padding: 20px;
border: 1px solid black;
margin: 10px;
}
</style>
<title>Box Model Example</title>
</head>
<body>
<div class="box">This is a box.</div>
</body>
</html>
In this example, the width
sets the width of the content area. The padding
adds space inside the element, the border
creates a visible boundary, and the margin
adds space outside the element.
<!-- This is a comment -->
, and in CSS, they are /* This is a comment */
.Responsive design ensures that a web page looks and functions well on different devices, such as desktops, tablets, and mobile phones. We can use media queries in CSS to achieve this.
/* For mobile devices */
@media (max - width: 767px) {
body {
font - size: 14px;
}
}
This media query applies the specified styles only when the screen width is 767 pixels or less.
HTML and CSS are essential technologies in web development. Understanding their full - forms, fundamental concepts, usage methods, common practices, and best practices is crucial for creating high - quality web pages. By using semantic HTML, the CSS box model, and following best practices like clean code and responsive design, developers can build engaging and user - friendly websites.