HTML is a markup language used to structure content on the web. It consists of a series of elements, each defined by tags. Tags are enclosed in angle brackets (<>
). For example, the <html>
tag is the root element of an HTML page, and it encloses all other elements.
Here are some basic HTML elements:
<head>
: Contains meta - information about the page, such as the page title, character encoding, and links to external resources.<body>
: Holds the visible content of the page, including text, images, and multimedia.<h1>
- <h6>
: Heading tags, where <h1>
is the most important (usually the page title) and <h6>
is the least important.<p>
: Defines a paragraph of text.<a>
: Creates a hyperlink to another web page or resource.<!DOCTYPE html>
<html>
<head>
<title>My First HTML Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is a sample paragraph.</p>
<a href="https://www.example.com">Visit Example.com</a>
</body>
</html>
CSS is used to style HTML elements. It separates the presentation from the structure, allowing for more flexibility and easier maintenance. 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.
/* Select all <h1> elements and set their color to blue */
h1 {
color: blue;
}
There are three ways to apply CSS to an HTML page:
style
attribute.<p style="color: red;">This text is red.</p>
<style>
tag in the <head>
section of an HTML page.<!DOCTYPE html>
<html>
<head>
<style>
p {
color: green;
}
</style>
</head>
<body>
<p>This text is green.</p>
</body>
</html>
.css
file and linked to the HTML page using the <link>
tag.
styles.cssbody {
background - color: lightgray;
}
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>My Styled Page</h1>
</body>
</html>
<header>
, <nav>
, <main>
, <article>
, <section>
, and <footer>
to give meaning to the structure of your page. This helps search engines understand the content better and improves accessibility.<!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>
<input>
, <textarea>
, and <select>
are commonly used in forms.<form action="submit.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<input type="submit" value="Submit">
</form>
div {
width: 200px;
height: 100px;
padding: 20px;
border: 1px solid black;
margin: 10px;
}
<!DOCTYPE html>
<html>
<head>
<style>
.flex - container {
display: flex;
justify - content: space - around;
}
.flex - item {
background - color: lightblue;
padding: 20px;
}
</style>
</head>
<body>
<div class="flex - container">
<div class="flex - item">Item 1</div>
<div class="flex - item">Item 2</div>
<div class="flex - item">Item 3</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Indented HTML</title>
</head>
<body>
<h1>Indented Content</h1>
<p>Readable code is good code.</p>
</body>
</html>
.my - class {
font - size: 18px;
}
#my - id {
background - color: yellow;
}
<p class="my - class">This text has a font size of 18px.</p>
<div id="my - id">This div has a yellow background.</div>
alt
text for images. This helps screen readers describe the image to visually impaired users and also improves SEO.<img src="image.jpg" alt="A beautiful landscape">
em
, rem
, or percentages. This makes your website more responsive on different devices.body {
font - size: 16px;
}
h1 {
font - size: 2em; /* 32px */
}
HTML and CSS are essential technologies for building modern websites. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can create websites that are not only visually appealing but also accessible and easy to maintain. Whether you are a beginner or an experienced developer, continuously learning and applying these principles will help you stay ahead in the ever - evolving world of web development.