27 Jan 2023

Building a website with HTML and CSS from scratch

Building a website from scratch can seem like a daunting task, but with a basic understanding of HTML and CSS, it can be a fun and rewarding experience. In this blog post, we will walk through the process of creating a simple website using just HTML and CSS.

Before we begin, it's important to understand the difference between HTML and CSS. HTML (Hypertext Markup Language) is used to create the structure and content of a website. It is the backbone of any website, and is used to create the layout, headings, paragraphs, images, and other elements that make up the content of a webpage. CSS (Cascading Style Sheets) is used to control the presentation and layout of a website. It is used to change the color, font, spacing, and other visual elements of a webpage.

To start building our website, we will need a text editor. There are many options to choose from, such as Notepad, Sublime Text, and Atom. For this tutorial, we will be using Sublime Text, but any text editor will work.

The first step in building a website is to create a new file and save it as "index.html" in a new folder. This will be the main file for our website, and it is important to save it as "index.html" so that it will be the first file that a server looks for when someone visits our website.

Once we have our index.html file created and saved, we can start adding HTML code to it. The basic structure of an HTML document is as follows:

<!DOCTYPE html>
<html>
<head>
  <title>My Website</title>
</head>
<body>
  <!-- Content goes here -->
</body>
</html>

The first line is the doctype, which tells the browser which version of HTML we are using. In this case, we are using HTML5. Next, we have the <html> tag, which is the container for all of the elements on our webpage. Inside the <html> tag, we have the <head> and <body> tags. The <head> tag is used to store information about the webpage that is not displayed to the user, such as the title of the webpage, which is displayed in the browser's tab. The <body> tag is where all of the visible content on the webpage goes.

Now that we have the basic structure of our HTML document set up, we can start adding content to our webpage. One of the most basic elements in HTML is the paragraph element, which is represented by the <p> tag. To add a paragraph of text to our webpage, we can simply add the following code inside the <body> tags:

<p>Hello, welcome to my website!</p>

We can also add headings to our webpage using the <h1>, <h2>, <h3>, etc. tags. These tags are used to create headings of different sizes, with <h1> being the largest and <h6> being the smallest. For example, to add a level 1 heading to our webpage, we can use the following code:

<h1>My Website</h1>

We can also add images to our webpage using the <img> tag. The <img> tag is used to embed images into a webpage, and it requires a source attribute (src) that points to the location of the image file. For example, to add an image to our webpage, we can use the following code:

<img src="images/myimage.jpg" alt="My Image">

The alt attribute is used to provide a text description of the image, which is useful for accessibility and in case the image doesn't load properly.

Now that we have some basic HTML content on our webpage, we can start using CSS to control the presentation and layout of our website. To apply CSS styles to our HTML elements, we can use the class and id attributes. The class attribute is used to apply styles to multiple elements, while the id attribute is used to apply styles to a single unique element.

We can link a CSS file to our HTML document by using the <link> tag in the <head> section of our HTML document. For example, to link a CSS file named "style.css" to our HTML document, we can use the following code:

<link rel="stylesheet" type="text/css" href="style.css">

Once we have linked our CSS file to our HTML document, we can start adding styles to our HTML elements. For example, to change the font of all <p> elements to Arial, we can add the following code to our CSS file:

p {
  font-family: Arial;
}

We can also use the class and id attributes to target specific elements. For example, to change the background color of a specific <div> element with the class "container", we can use the following code:

.container {
  background-color: #f5f5f5;
}

In addition to changing the font and background color, we can also use CSS to control the layout of our website. One of the most common layout techniques is to use the display: flex property to create a flexible box layout. This allows us to create a layout where elements are arranged in a row or column, and can be easily re-arranged based on the size of the screen.

In addition to layout, CSS also allows us to create responsive designs, which adapt to different screen sizes. This is achieved by using media queries, which allow us to apply different styles based on the width and height of the screen. For example, to change the font size of all <h1> elements when the screen width is less than 800px, we can use the following code:

@media (max-width: 800px) {
  h1 {
    font-size: 20px;
  }
}

Building a website from scratch with HTML and CSS can seem like a daunting task, but with a basic understanding of these technologies, it can be a fun and rewarding experience. By following the steps outlined in this tutorial, you should now have a basic understanding of how to create a website using HTML and CSS. Remember, there are many resources available online, including tutorials, documentation, and forums, that can help you as you continue to learn and improve your skills.