HTML is a markup language used to create the structure of a web page. It consists of a series of elements, each defined by tags. For example, the <html>
tag is the root element of an HTML document, and other tags like <head>
and <body>
are used to organize different parts of the page. HTML files typically have the .html
or .htm
file extension.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to my website</h1>
<p>This is a sample paragraph.</p>
</body>
</html>
CSS is a style sheet language used for describing the presentation of an HTML document. It allows web developers to control the layout, colors, fonts, and other visual aspects of a web page. CSS files have the .css
file extension.
h1 {
color: blue;
font-size: 36px;
}
p {
color: gray;
font-size: 18px;
}
To apply CSS styles to an HTML page, we need to link the CSS file to the HTML file. This is done using the <link>
tag in the <head>
section of the HTML document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Welcome to my website</h1>
<p>This is a sample paragraph.</p>
</body>
</html>
Inline CSS is used to apply styles directly to a single HTML element. It is added as a style
attribute within the opening tag of the element.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Inline CSS Example</title>
</head>
<body>
<h1 style="color: red; font-size: 30px;">This is a red heading</h1>
<p style="color: green; font-size: 16px;">This is a green paragraph</p>
</body>
</html>
Internal CSS is defined within the <style>
tags in the <head>
section of an HTML document. This method is useful when you want to apply styles to a single page only.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Internal CSS Example</title>
<style>
h1 {
color: purple;
font-size: 32px;
}
p {
color: orange;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Internal CSS Heading</h1>
<p>Internal CSS Paragraph</p>
</body>
</html>
External CSS is the most recommended method for large - scale projects. As mentioned earlier, it involves creating a separate .css
file and linking it to the HTML file using the <link>
tag.
styles.css
h2 {
color: teal;
font-size: 28px;
}
span {
color: pink;
font-size: 14px;
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>External CSS Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h2>External CSS Heading</h2>
<span>External CSS Span</span>
</body>
</html>
In CSS, you can group selectors to apply the same styles to multiple elements. This reduces code duplication.
h1, h2, h3 {
font-family: Arial, sans-serif;
color: #333;
}
Classes and IDs are used to target specific elements in HTML. Classes are used for multiple elements, while IDs are used for a single unique element.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Classes and IDs Example</title>
<style>
.highlight {
background-color: yellow;
}
#unique - element {
border: 1px solid black;
}
</style>
</head>
<body>
<p class="highlight">This paragraph is highlighted</p>
<div id="unique - element">This is a unique element</div>
</body>
</html>
With the increasing use of different devices, responsive design is essential. You can use media queries in CSS to apply different styles based on the device’s screen size.
@media (max - width: 768px) {
body {
font-size: 14px;
}
}
Use a logical structure in your CSS file. Group related styles together, and use comments to explain different sections.
/* Global styles */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
/* Header styles */
header {
background-color: #f4f4f4;
padding: 20px;
}
Inline CSS makes the HTML code messy and difficult to maintain. It also violates the separation of concerns principle. Use external or internal CSS instead.
Minify your CSS files by removing unnecessary whitespace, comments, and shortening property names. This reduces the file size and improves page loading speed.
In conclusion, understanding the relationship between CSS file extensions and HTML is fundamental for web development. By mastering the concepts of linking CSS to HTML, different usage methods, common practices, and best practices, you can create visually appealing and highly maintainable websites. Whether you are a beginner or an experienced developer, these techniques will help you build better web applications.