Learn HTML and CSS: Building Your First Website

When I first started learning web development, I was amazed at how simple it is to create a basic website. With just a few lines of code, I could build something that looked like a real website. And while the internet is full of complex, professional websites, the foundation of nearly all of them comes down to two essential languages: HTML and CSS.

In this guide, I’ll walk you through building your first website using HTML for the structure and CSS for styling. By the end of it, you’ll have a simple, functioning webpage that you can expand upon and customize further as you get more comfortable with the concepts.


What is HTML?

HTML, or HyperText Markup Language, is the backbone of all websites. It provides the structure and content of a webpage, determining where elements like text, images, links, and buttons appear. Think of HTML as the skeleton of a webpage.

With HTML, we use tags to define elements. These tags are enclosed in angle brackets (like <html>, <p>, and <div>) and usually come in pairs with an opening tag and a closing tag (</tag>).


What is CSS?

CSS, or Cascading Style Sheets, is what brings your webpage to life with styles like colors, fonts, and layout design. While HTML provides the structure, CSS handles the presentation.

Think of CSS as the skin and clothing that make your webpage visually appealing. CSS allows you to define styles for individual elements or entire sections of your website.


Setting Up Your First Website

1. Creating the HTML Structure

Let’s start by setting up the HTML structure for your website. Open a text editor like Notepad, Sublime Text, or Visual Studio Code—any editor will do—and create a new file. Name it index.html.

Inside the file, type the following:

<!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> </head> <body> <header> <h1>Welcome to My First Website</h1> </header> <section> <h2>About Me</h2> <p>Hi, I'm learning to build websites using HTML and CSS. This is my first project, and I'm excited to share it with you.</p> </section> <section> <h2>Contact</h2> <p>You can reach me via email at <a href="mailto:myemail@example.com">myemail@example.com</a>.</p> </section> <footer> <p>&copy; 2024 My First Website</p> </footer> </body> </html>

Let’s break down what’s happening here:

  • <!DOCTYPE html>: This declares that the document is written in HTML5, which is the latest version of HTML.
  • <html>: This is the root element of your webpage.
  • <head>: Inside the head section, we include metadata (information about the webpage like the character set and viewport settings). We also set the title of the page, which will appear in the browser tab.
  • <body>: This is where the actual content of the page goes.
  • <header>, <section>, and <footer>: These tags define different parts of the page. The header contains the main title, the sections hold the content, and the footer contains copyright information.

Now, save the file and open it in a browser. You should see something like this:

  • A big "Welcome to My First Website" heading.
  • A section with some text about yourself.
  • A contact section with an email link.

2. Adding Basic CSS

Now that we have the structure in place, it’s time to style it with CSS.

Create another file in the same folder and name it styles.css. In this file, we’ll add some basic styles to make the website look better. Type the following:

css


/* Resetting some default styles */ * { margin: 0; padding: 0; box-sizing: border-box; } /* Styling the body */ body { font-family: Arial, sans-serif; background-color: #f4f4f4; color: #333; line-height: 1.6; } /* Header styles */ header { background-color: #333; color: #fff; padding: 10px 0; text-align: center; } /* Section styles */ section { margin: 20px auto; padding: 20px; max-width: 600px; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } /* Footer styles */ footer { text-align: center; padding: 10px 0; background-color: #333; color: #fff; position: fixed; width: 100%; bottom: 0; }

Let’s break this down:

  • CSS Reset (*): The * selector targets all elements on the page and removes default margins and padding. It also sets the box-sizing property, ensuring that padding and borders are included in the element's width.
  • Body Styling: Here, I set the font family to Arial, the background color to a light gray, and the text color to a dark gray. This gives a clean and modern look.
  • Header and Footer: Both the header and footer have a dark background (#333) with white text (#fff). They are centered and padded for better spacing.
  • Section Styling: I added some margin and padding to the sections to give the content room to breathe. The box-shadow adds a subtle shadow effect, making the content stand out against the background.

3. Linking the CSS to HTML

To make sure the CSS file styles your HTML page, we need to link the two. Go back to your index.html file, and inside the <head> tag, add this line:

html

<link rel="stylesheet" href="styles.css">

This line tells the browser to load the CSS file and apply its styles to the HTML elements. Save everything and refresh your browser. Your webpage should now look much more polished!


4. Adding More Content

Let’s expand our webpage with more content. Here’s how you can add a navigation bar, an image, and a list.

Adding a Navigation Bar

Inside your <header>, you can add a navigation menu that links to different sections of the page.

html

<header> <h1>Welcome to My First Website</h1> <nav> <ul> <li><a href="#about">About Me</a></li> <li><a href="#contact">Contact</a></li> </ul> </nav> </header>

We use the <nav> tag to define the navigation bar and a list (<ul>, <li>) to hold the links. The href attribute in the anchor tags (<a>) is set to #about and #contact, which link to the respective sections on the same page.

Next, style the navigation in your CSS file:

css

nav ul { list-style: none; padding: 0; } nav ul li { display: inline; margin: 0 10px; } nav ul li a { color: #fff; text-decoration: none; }

This removes the bullet points from the list, aligns the links horizontally, and styles the links with white text.

Adding an Image

To include an image, place it in one of the sections. Let’s add it to the "About Me" section:

html

<section id="about"> <h2>About Me</h2> <img src="profile.jpg" alt="My Profile Picture" width="200"> <p>Hi, I'm learning to build websites using HTML and CSS. This is my first project, and I'm excited to share it with you.</p> </section>

Make sure to replace "profile.jpg" with the actual image file path.

To style the image:

css

section img { display: block; margin: 20px auto; border-radius: 50%; }

Here, we center the image and give it a border-radius of 50%, which makes it circular.

Adding a List

In the "About Me" section, let’s add a list of your favorite hobbies:

html

<section id="about"> <h2>About Me</h2> <img src="profile.jpg" alt="My Profile Picture" width="200"> <p>Hi, I'm learning to build websites using HTML and CSS. This is my first project, and I'm excited to share it with you.</p> <h3>My Hobbies</h3> <ul> <li>Coding</li> <li>Reading</li> <li>Traveling</li> </ul> </section>

No need to add extra CSS for this unless you want to style the list specifically.


Advanced CSS: Adding a Hero Section

Let’s take things up a notch by adding a hero section—a big banner at the top of the webpage. In your HTML, create a new section inside the <body> tag, right after the header:

html

<section class="hero"> <h1>Building Websites with HTML & CSS</h1> <p>Join me in my journey of learning web development!</p> </section>

Now, let’s style this hero section in your CSS:

css

.hero { background-image: url('hero.jpg'); background-size: cover; background-position: center; color: white; text-align: center; padding: 100px 20px; } .hero h1 { font-size: 3rem; } .hero p { font-size: 1.5rem; }

This CSS gives your hero section a background image (hero.jpg), centers the text, and adds padding to make it look spacious. Replace 'hero.jpg' with any image you like.


Conclusion

Congratulations! You’ve just built your first website using HTML and CSS. We started by learning about the structure provided by HTML and then used CSS to make it look visually appealing. From adding navigation and images to creating a hero section, you've now got the building blocks to expand and personalize your site.

This is just the beginning of your web development journey. The more you practice, the better you'll get at creating more complex and dynamic websites. As you continue, you’ll want to learn about JavaScript for interactivity, responsive design for mobile optimization, and much more. But for now, take pride in the fact that you’ve built something from scratch.

Comments