HTML is a markup language used to create the structure of web pages. It consists of elements, which are represented by tags. For example, the <html>
tag is the root element of an HTML page, and it encapsulates all other elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>My First HTML Page</title>
</head>
<body>
<h1>Welcome to my page</h1>
<p>This is a simple paragraph.</p>
</body>
</html>
In this example, the <!DOCTYPE html>
declaration tells the browser that this is an HTML5 document. The <html>
element contains a <head>
and a <body>
. The <head>
holds meta - information about the page, such as the character encoding and the page title. The <body>
contains the visible content of the page.
CSS is used to style HTML elements. It uses selectors to target specific HTML elements and declarations to define how those elements should look.
h1 {
color: blue;
font - size: 36px;
}
In this CSS code, the h1
is the selector, which targets all <h1>
elements on the page. The declarations color: blue;
and font - size: 36px;
define the text color and font size of the <h1>
elements respectively.
HTML has a wide variety of tags for different purposes. For example, the <div>
tag is a generic container used for grouping other elements, while the <span>
tag is used for inline grouping.
<div class="container">
<p>This is a paragraph inside a div.</p>
<span style="color: red;">This text is red.</span>
</div>
Here, the <div>
with the class container
groups the <p>
and <span>
elements. The <span>
has an inline style that makes its text red.
CSS selectors can be based on element names, classes, IDs, or other attributes.
/* Element selector */
p {
line - height: 1.5;
}
/* Class selector */
.container {
width: 80%;
margin: 0 auto;
}
/* ID selector */
#special - heading {
font - weight: bold;
}
The element selector targets all <p>
elements. The class selector targets all elements with the class container
, and the ID selector targets the element with the ID special - heading
.
Semantic HTML means using HTML tags that convey the meaning of the content. For example, instead of using a <div>
for every section, use tags like <header>
, <nav>
, <main>
, <article>
, <section>
, and <footer>
.
<header>
<h1>My Website</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
</ul>
</nav>
</header>
<main>
<article>
<h2>Article Title</h2>
<p>Article content goes here...</p>
</article>
</main>
<footer>
<p>© 2024 My Website</p>
</footer>
Semantic HTML improves accessibility and search engine optimization (SEO).
Responsive design ensures that web pages look good on different devices and screen sizes. CSS media queries are commonly used for this purpose.
@media (max - width: 768px) {
.container {
width: 100%;
}
}
This media query changes the width of the .container
class to 100% when the screen width is 768 pixels or less.
Keep your HTML and CSS code organized. In HTML, use indentation to clearly show the hierarchy of elements. In CSS, group related styles together and use comments to explain complex parts.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Main content -->
<main>
<section>
<h2>Section Title</h2>
<p>Section content...</p>
</section>
</main>
</body>
</html>
/* Global styles */
body {
font - family: Arial, sans - serif;
}
/* Main content styles */
main {
padding: 20px;
}
Minimize the use of inline styles, as they can make the HTML code hard to maintain and can’t be cached. Instead, use external CSS files. Also, optimize image sizes and use CSS sprites to reduce the number of HTTP requests.
HTML and CSS are essential technologies for web development. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can create high - quality, accessible, and visually appealing web pages. The knowledge shared in O’Reilly resources can further enhance your skills and keep you updated with the latest trends in web development.