Although this tutorial covers the basics of styling with HTML and CSS, it is still recommended that you have at least a small understanding of what HTML is before reading this tutorial. If you would like to read this tutorial but are still a little unsure about what HTML is, then I would recommend that you read my other article "An Introduction to Writing HTML" before starting this one.
- An Introduction to Writing HTML
An introduction to HTML and text editors. Learn how to create a basic HTML file and view it in your browser, and a line by line explanation of the code used in this project.
What is CSS?
CSS stands for Cascading Style Sheets. Similar to HTML, CSS is a tool used for web design. In fact, HTML and CSS go hand in hand when it comes to designing a nice-looking website. The main difference between the two is that HTML is used mainly for creating the content of the website, while CSS is used for styling that content. HTML is a useful tool for creating a website, but without CSS your website would look very bland indeed. That being said, there are other tools a person can use for styling a website, but for someone just getting into web design CSS it where it all starts.
Getting Started With HTML
In order to use CSS, we will need to have some content on our website first, so let's get started by creating a simple HTML file and some of the more common elements that are found on a webpage. Go ahead and open your text editor and create a new named "index.html". For anyone who has not already found a text editor that they like, I would highly recommend using Brackets for writing HTML and CSS. Now, copy and paste the code below into your index.html file.
This text is common to almost every HTML file. The tag on the 1st line tells internet browsers that this is an html file, and the tags on the 2nd and 9th lines tell the browsers that everything between these two tags is HTML typed in English. Between the <head> tags on lines 3 and 5 is where you will put code for displaying your website name and logo in your web browser's tab. Between the <body> tags on lines 6 and 8 is where you will put the content of your website. it is literally your websites body.
Add Some Content With HTML
Now that we have the basic outline of our website, it is time to add some content to make it a bit more interesting. Let's start by adding a banner to our website.
<div> tags are used to separate a section of your website from the rest of the website. In this case, we are using div tags to separate the banner from the rest of the website. The <h1> tags are used for creating headers on your website. There are six different headers (h1, h2, h3, h4, h5, and h6) that can be used. The biggest differences between the headers is the text size. Headers are most commonly used to emphasize banner text and paragraph titles. Now, let's add a navigation bar, or navbar for short.
Again, we will use <div> tags to separate the navbar into its own section. The <ul> tags stands for unordered list with the <li> tags each being a list item on the unordered list. Inside of the <li> tags are <a> tags which are used to create links to other webpages or other pages of your website. The text in between the <a> tags is what is displayed as the link text, while the text inside the quotation marks after the href is the link destination. In this example, the first three links would direct you to different sections of your future website, and the fourth link would take you to the Hubpages website. Now, let's add some text the body of our website.
Here we can see another example of a header tag. We used an <h2> in this case to emphasize the paragraph header while still keeping it smaller than the banner text. The <p> tags are used to mark a paragraph of text, and the new <div> at the bottom of the code is to separate our disclaimer from the rest of the text on the page. While it is possible to add text to your website simply by typing in between the <body> tags, it is much cleaner and easier to style and organize your website if you place your text in paragraph or header tags or like in the case of our copyright disclaimer place it in its own <div>. Now, let's open our website and see what we have so far.
After opening your website, you should see something like the image above. While we can clearly see the different sections of our website, it still looks quite bland. this is where CSS comes in.
Add Some Style With CSS
Now that we have our website, let's add some styling with CSS. Using your text editor create another file and name it "style.css". Before we can start writing in our new CSS file, we need to add one more thing to our index.html file. For each of our main tags we will want to assign either an id or a class inside its opening tag. If the tag is a unique section of your website, then we will assign it an id, but for tags that represent a repeating element of the website that will have similar styling, like the body text, we will assign a class instead. Last, we need to link our HTML file to our CSS file inside the <head> tags.
Now that the main sections of our page have ids or classes, we can reopen our style.css file and start adding some color to our website.
As you have likely notice from the code above, CSS is styled slightly different from HTML. In CSS you can specify the piece of your website that you want to style in three ways. First, you can specify a section by referring to its id with a # followed by the elements id. Second, you can specify a section by referring to its tag name like body in the code above. And third, you can specify a group of section by referring to their matching class name with a period followed by the class name. No matter which way you choose to use, you will place an opening and closing bracket after the reference. Any styling in between these brackets will be applied the referenced section and any sub-sections inside that section. For example, if you were to put the code from line 10 inside of the body reference instead, then all the text inside your website body would turn that color instead of just the sections marked with the bodyText class.
Read More From Owlcation
The second thing you likely noticed is that there are several ways to refer to a color in CSS. Some colors have been pre-assigned names like blue, red, yellow, and saddlebrown, but for more specific color you can use alternative methods like RGB or hex. I won't dig deep into these alternative methods now, just know that they exist and that there are websites that you can use to find almost any color in RGB or hex. Now, let's take a look at our website and see the difference.
As you can see, even adding a small amount of CSS can make a big difference in the way your website looks. While I admit that the colors chosen are not the best, they are good enough for this example. Now that our website has some color, one problem that you might notice is that the banner is probably not the size that we would like it to be, so let's fix that next.
Above, in the #banner section, you can see that we specified the height of the banner to be 200 pixels, and that we also aligned text horizontal. But, that only wasn't enough to fix our banner, so we removed the margins from both the body and the h1 tags. Now, open your website and see the difference.
There, that looks much better. Now, that our header is looking better, the next thing that we'll want to focus on is making our navbar look nicer. Let's do that now.
Add the above code to the bottom of your CSS file. Here we are referencing different parts of our navbar. First, we reference the <li> tags and specify that we want them to have a padding of 10 pixels, then we switch to inline display so that the links will be listed horizontally. Next, we told the navbar that we wanted to have any text inside of it centered horizontally. Last, we specified that we wanted the links to be dark green, and we removed the underline by specifying none for text decoration. Now, let's see the difference.
Again, I'm using ugly colors for this example, but you can easily change the colors on your website by specifying a different one. Even with the ugly dark green color, the navbar looks much better than before. Now, the last thing that we will fix is the body text.
In the code above, you can see that we modified the bodyText reference to have 20 pixels of padding on its left and right side. This is to make the text easier to read by spacing it away from the edges of the browser. We also added a new reference for the <h2> tag and specified that we wanted it to have 5 pixels of padding on its left side. Keeping the <h2> closer to the edge of the browser will help give the impression that the <h2> is a header for the body text. Last, we added a reference for the copyright section. We specified that we wanted the <div> tag to be the full width of the browser, and that we wanted the text inside of the <div> to be center horizontally. It is necessary to make the copyright <div> have 100% width so that the text will be aligned properly. When centering text, the text is centered according to the width of its parent <div>, meaning that if the parent <div> is not full width, then the centering will be off. Now, let’s see our improved website.
There, that looks much better than when we started. While our website is still quite basic, it is clear how much difference CSS can make when doing web design.
Thank You for Reading
Thank you for reading this article, and I hope that you found it helpful. If you have any questions, please leave a comment below. I am more than happy to help with any issues you may have with this project or with HTML and CSS in general. In addition, here are some links to some of the more helpful websites for learning HTML and CSS.
- CSS Tutorial
- Learn HTML - Free Interactive HTML Tutorial
LearnHTML.org is a free interactive HTML tutorial for people who want to learn HTML, fast.
- Free tutorials on HTML, CSS and PHP - Build your own websiteenhomepage - HTML.net
Free tutorials on HTML, CSS and PHP - Build your own website - Free tutorials on HTML, CSS and PHP - Build your own website
- HTML Color Picker
Help Me Get a Better Idea of Where my Readers Stand With CSS
Bret Phillips (author) from Utah on May 01, 2019:
@damilare Thank you, this is one of my older articles, but I'm glad to see that people are still reading it.
Can you be a little more specific about what's confusing you?
Is there something specific that you are having trouble with, or are you looking for a good overall intro to web design?
These videos might be helpful if your looking for a good intro tutorial:
damilare on May 01, 2019:
the tutorial is perfect but how can I create a website with html and css