HTML is the standard markup language for creating web pages. It uses tags to define the structure and content of a web page. Each tag has a specific purpose, such as defining headings, paragraphs, lists, and links.
Here is a simple example of an HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First HTML Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is a simple paragraph.</p>
<a href="https://www.example.com">Visit Example.com</a>
</body>
</html>
In this example, the <!DOCTYPE html>
declaration tells the browser that this is an HTML5 document. The <html>
tag is the root element of the page. The <head>
section contains meta-information about the page, such as the character encoding and the page title. The <body>
section contains the visible content of the page.
<header>
, <nav>
, <main>
, <article>
, <section>
, and <footer>
to make the code more readable and accessible.<p>
tag should be closed with a closing </p>
tag before starting a new tag.CSS is used to style HTML elements. It allows you to control the layout, colors, fonts, and other visual aspects of a web page. CSS rules consist of a selector and a declaration block. The selector targets an HTML element, and the declaration block contains one or more property-value pairs.
Here is an example of how to link an external CSS file to an HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Styled Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Styled Heading</h1>
<p>Styled paragraph</p>
</body>
</html>
And here is the content of the styles.css
file:
h1 {
color: blue;
font-size: 32px;
}
p {
color: green;
font-size: 16px;
}
In this example, the h1
and p
selectors target the <h1>
and <p>
elements in the HTML file, respectively. The color
and font-size
properties are used to style these elements.
@media (max-width: 768px) {
body {
font-size: 14px;
}
}
Image files are used to enhance the visual appeal of a web page. Common image file formats for the web include JPEG, PNG, GIF, and SVG. Each format has its own advantages and disadvantages.
Here is an example of how to insert an image into an HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Example</title>
</head>
<body>
<img src="example.jpg" alt="An example image">
</body>
</html>
The src
attribute specifies the path to the image file, and the alt
attribute provides alternative text for the image, which is useful for accessibility and in case the image cannot be loaded.
<picture>
element or the srcset
and sizes
attributes to serve different image sizes based on the device’s screen size.Script files, usually written in JavaScript, are used to add interactivity to web pages. JavaScript allows you to manipulate HTML elements, handle user events, and perform other dynamic tasks.
Here is an example of how to link an external JavaScript file to an HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Example</title>
</head>
<body>
<button id="myButton">Click me</button>
<script src="script.js"></script>
</body>
</html>
And here is the content of the script.js
file:
const button = document.getElementById('myButton');
button.addEventListener('click', function() {
alert('Button clicked!');
});
In this example, the JavaScript code selects the button element by its ID and adds a click event listener. When the button is clicked, an alert message is displayed.
CSS, image, HTML, and script files are essential components of web development. Understanding their fundamental concepts, usage methods, common practices, and best practices is crucial for creating high-quality and user-friendly websites. By following these guidelines, you can ensure that your web pages are well-structured, visually appealing, and interactive.