Introduction to HTML and CSS
HTML and CSS are two fundamental technologies used to build websites. HTML organizes webpage content, while CSS controls how that content looks and how it is arranged on the screen.
This beginner-friendly guide explains the main concepts, provides practical code examples, and suggests projects you can build to develop your web development skills.
1. What Is HTML?
HTML stands for HyperText Markup Language. It is the standard markup language used to describe the structure and content of webpages.
HTML elements can represent headings, paragraphs, links, images, lists, tables, forms, and other types of content.
The <h1> element represents a main heading, while
<p> represents a paragraph.
<h1>Welcome to My Website</h1>
<p>This is my first webpage.</p>
2. What Is CSS?
CSS stands for Cascading Style Sheets. It describes how HTML content should appear, including its colors, fonts, spacing, borders, and layout.
CSS can be written in a separate stylesheet, inside a
<style> element, or directly on an HTML element
using the style attribute. A separate stylesheet is
usually easier to maintain on larger websites.
h1 {
color: #0d47a1;
font-size: 32px;
text-align: center;
}
p {
line-height: 1.7;
}
In this example, CSS makes headings blue, increases their size, centers them, and improves paragraph readability.
3. HTML vs CSS
| HTML | CSS |
|---|---|
| Defines webpage structure and content. | Controls presentation and visual layout. |
| Creates headings and paragraphs. | Changes colors, fonts, and text spacing. |
| Provides links, images, and forms. | Controls element sizes, alignment, and spacing. |
| Uses elements and attributes. | Uses selectors, properties, and values. |
Think of HTML as the structure of a house and CSS as the paint, decoration, and arrangement of its rooms.
4. How HTML and CSS Work Together
A browser reads HTML to understand the page content and applies CSS rules to determine how that content should look.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Page</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
h1 {
color: blue;
}
</style>
</head>
<body>
<h1>Welcome!</h1>
<p>HTML provides structure, and CSS adds style.</p>
</body>
</html>
This complete example creates a webpage with a blue heading and a paragraph.
5. Common HTML Elements
Here are some commonly used HTML elements and their purposes.
| Element | Purpose |
|---|---|
<h1> | Main heading |
<h2> | Section heading |
<p> | Paragraph |
<a> | Hyperlink |
<img> | Image |
<button> | Clickable button |
<ul> | Unordered list |
<ol> | Ordered list |
<li> | List item |
<div> | Generic block container |
<form> | Form for user input |
<input> | Input control |
6. HTML Attributes
Attributes provide additional information about an HTML element. They are normally written in the opening tag.
Example: A Link
<a href="https://example.com">
Visit Example Website
</a>
The href attribute specifies the link destination.
Replace the example address with the website you want to link to.
Example: An Image
<img src="photo.jpg"
alt="A computer on a desk"
width="300">
The src attribute identifies the image file.
The alt attribute describes the image for people who
use screen readers or when the image cannot load.
7. HTML Headings and Paragraphs
HTML provides six heading levels, from <h1>
to <h6>. Use them to organize content into
a meaningful hierarchy.
<h1>Web Development</h1>
<h2>HTML Basics</h2>
<h3>HTML Headings</h3>
<p>This is a paragraph of text.</p>
Use headings according to their meaning rather than choosing them only because of their default size. CSS can change their appearance.
8. HTML Links
Links allow users to navigate between pages or open other resources.
<a href="about.html">About Us</a>
<a href="https://developer.mozilla.org/">
Visit MDN Web Docs
</a>
A relative link such as about.html points to a page
relative to the current URL. An absolute URL includes the full
web address.
9. Adding Images to a Webpage
The <img> element displays an image.
It is a void element, meaning it does not need a closing tag.
<img src="technology.jpg"
alt="Technology equipment"
width="300"
loading="lazy">
Use descriptive alternative text when an image conveys information. Optimize image files so that they load quickly, and specify dimensions where practical to reduce unexpected layout movement.
10. CSS Selectors
CSS selectors identify which HTML elements a rule should style.
/* Select all paragraph elements */
p {
color: #333;
}
/* Select an element with class="notice" */
.notice {
background: #eef6ff;
}
/* Select an element with id="main-title" */
#main-title {
color: #0d47a1;
}
A class can be reused on multiple elements. An ID should identify a unique element within a page.
11. CSS Colors and Backgrounds
CSS supports named colors, hexadecimal values, RGB, HSL, gradients, and other color formats.
body {
background-color: #f5f7fb;
color: #222;
}
h1 {
color: rgb(13, 71, 161);
}
.card {
background: white;
}
Choose colors with sufficient contrast so that text remains readable for people with different visual needs.
12. CSS Fonts and Text
CSS controls font families, font sizes, font weights, line height, text alignment, and other text properties.
body {
font-family: Arial, Helvetica, sans-serif;
line-height: 1.7;
}
h1 {
font-size: 32px;
font-weight: 700;
text-align: center;
}
p {
font-size: 16px;
}
Use readable font sizes and enough line spacing. A small number of consistent fonts generally makes a website easier to read.
13. CSS Margin and Padding
Margin and padding are both used for spacing, but they affect different areas of an element.
- Margin: space outside an element's border.
- Padding: space between the content and its border.
.card {
margin: 20px;
padding: 20px;
border: 1px solid #ddd;
}
The box-sizing: border-box rule used in this guide
makes width and height calculations easier by including padding
and borders in the specified dimensions.
14. CSS Borders and Rounded Corners
Borders help separate content and can make interface elements easier to identify.
.box {
border: 2px solid #0d47a1;
border-radius: 10px;
padding: 16px;
}
You can customize the border color, thickness, style, and corner radius to suit the design of your page.
15. CSS Flexbox
Flexbox is a layout system for arranging elements along a row or column. It is useful for navigation bars, aligned buttons, and groups of cards.
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.item {
flex: 1 1 200px;
}
In this example, the items can wrap onto another line when there is not enough horizontal space.
16. CSS Grid
CSS Grid is designed for two-dimensional layouts involving rows and columns. It is useful for image galleries, dashboards, and multi-column content.
.grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 20px;
}
.grid-item {
padding: 16px;
background: #eef6ff;
}
You can combine Grid with media queries to change the number of columns on smaller screens.
17. Responsive Web Design
Responsive web design allows a website to adapt to different screen sizes, including mobile phones, tablets, and desktop computers.
The viewport meta tag, flexible layouts, and CSS media queries are common parts of a responsive design.
.grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 16px;
}
@media (max-width: 600px) {
.grid {
grid-template-columns: 1fr;
}
h1 {
font-size: 25px;
}
}
18. HTML Forms
Forms allow visitors to enter information. They are commonly used for contact forms, searches, registrations, and feedback.
<form action="/contact" method="post">
<label for="name">Your name</label>
<input
type="text"
id="name"
name="name"
required>
<button type="submit">Submit</button>
</form>
The label identifies the input, and required
asks the browser to check that the field is completed before submission.
The example's action URL must be connected to a server
endpoint that can process the form. HTML alone does not send the
information to a working backend unless an appropriate destination
is configured.
19. Semantic HTML Elements
Semantic elements describe the purpose of different areas of a page. They help developers organize content and can improve accessibility.
<header>— introductory content for a page or section.<nav>— a group of navigation links.<main>— the primary content of a page.<section>— a related thematic section.<article>— self-contained content.<footer>— footer information for a page or section.
Use elements according to their intended meaning rather than choosing them only for their default appearance.
20. HTML, CSS, and JavaScript
These technologies often work together in modern web development.
| Technology | Main role |
|---|---|
| HTML | Structure and content |
| CSS | Appearance and layout |
| JavaScript | Interactive behavior and dynamic features |
For example, HTML creates a button, CSS styles it, and JavaScript can respond when a visitor clicks it.
Read our JavaScript Guide to learn more about interactive websites.
21. Build Your First Webpage
Follow these steps to create a basic webpage using a text editor such as Acode or another code editor.
- Create a new file named
index.html. - Copy the complete HTML example below into the file.
- Save the file.
- Open it in a browser or use your editor's preview feature.
- Change the heading, paragraph, and CSS values to experiment.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Website</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f5f7fb;
padding: 20px;
}
h1 {
color: #0d47a1;
}
p {
line-height: 1.7;
}
</style>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is my first webpage.</p>
</body>
</html>
22. Where Can HTML and CSS Be Used?
HTML and CSS are used in many kinds of web projects, including:
- Personal and portfolio websites
- Business websites
- Blogs and educational websites
- News websites
- Online stores
- Landing pages
- Documentation websites
- Web application interfaces
- Online learning platforms
23. Common Beginner Mistakes
- Forgetting to close elements that require closing tags.
- Using invalid or incorrectly nested HTML.
- Writing CSS selectors that do not match the intended elements.
- Forgetting to test the page on mobile screens.
- Using large, unoptimized images.
- Using too many colors and fonts without a consistent design.
- Ignoring keyboard access and alternative text for meaningful images.
- Copying code without understanding what it does.
- Forgetting to save changes before previewing the page.
Validate your HTML, test links, and check your browser's developer tools when available to identify errors.
24. How to Learn HTML and CSS
Consistent practice is one of the most useful ways to improve. A beginner can follow this learning path:
- Understand basic HTML elements and document structure.
- Practice headings, paragraphs, links, and images.
- Learn CSS selectors and the box model.
- Practice colors, typography, borders, and spacing.
- Learn Flexbox for arranging elements.
- Learn Grid for row-and-column layouts.
- Build responsive layouts with media queries.
- Practice accessible forms and navigation.
- Build small projects from start to finish.
- Learn JavaScript when you are ready to add interactivity.
25. Beginner Projects to Build
Use what you learn to create practical projects such as:
- A personal profile page
- A business landing page
- A simple blog layout
- A restaurant website
- A portfolio website
- A product information page
- A technology tutorial page
- A contact form interface
- A responsive photo gallery
Start with a small project, test it carefully, and add new features as your skills improve.
26. Recommended Learning Resources
The following resources provide further explanations, references, and practical exercises.
- MDN Web Docs: Learn Web Development — tutorials for learning the foundations of the web.
- MDN HTML Reference — information about HTML elements and attributes.
- MDN CSS Reference — explanations of CSS properties and layout techniques.
- W3C Web Accessibility Introduction — an introduction to making websites accessible.
27. Frequently Asked Questions
Is HTML a programming language?
HTML is a markup language. It structures content rather than providing general-purpose programming logic.
Is CSS a programming language?
CSS is a stylesheet language that describes the presentation and layout of documents.
Do I need a computer to learn HTML and CSS?
A computer can be convenient, but you can also learn and practice using a suitable code editor and browser on an Android phone or tablet.
Can HTML and CSS create a website?
Yes. They can create the structure and design of many websites. Dynamic features may require JavaScript, while features such as accounts, server-side processing, and database storage usually require a backend service.
Can I earn money with HTML and CSS?
These skills can help you build websites for businesses, create landing pages, and pursue freelance or employment opportunities. Income depends on your skills, experience, clients, and market demand.
What should I learn after HTML and CSS?
JavaScript is a common next step. You can later explore frontend frameworks, backend development, databases, and web hosting.
How long does it take to learn HTML and CSS?
It depends on your practice schedule and goals. You can learn basic concepts relatively quickly, but building polished, accessible, responsive websites takes continued practice.
Conclusion
HTML and CSS are essential foundations of web development. HTML organizes content, while CSS controls its visual presentation and layout.
Learn the basics, practice with small examples, and gradually build complete projects. Testing your work and understanding why your code works will help you become a more confident web developer.
Continue learning with the other tutorials available on TechLearn Hub.