CSS Overview and Task 02.02 Focus
#
Session VideoThis class was cancelled due to inclement weather conditions. Here is a video that will walk you through getting your task 02.02 completed. If you are not able to see the video, you will need to login into https://uncch.hosted.panopto.com.
#
HTML ReviewThis is the basic page that we hard coded last week. We did it line by line in notepad without any auto completion:
There is no CSS styling in the code above. (You can change the Title to 02.02 Assignment)
#
Inline CSSThe best way to learn css concepts is to continue to hard code; however, cutting and pasting simple code is acceptable. Or you can use your autocomplete function in your text editor. Open up your last project and add some more code:
So let's put in some inline css with a style attribute.
Find the h1 tag, click after the 1 and before the closing h1 bracket and add some space, like this:
Confirm that you moved the angle bracket with the text.
Then, add add the following code in the space:
Every part of the attribute data has to go inside the first pair of brackets. Now look below. Look back and forth between these two boxes and confirm that style="color:red;"
is in there in that particular order. This syntax will be repeated over and over with all of the other attributes that you will learn. Eventually, it will get easier to recognize the pattern.
See this codepen example, to see it live.
Different text editors use different highlighting, but are consistent regarding elements, attributes, and values.
Save your file with the changes, leaving the inline style in place. We need to keep that in the document to demo and discuss the concept of cascading. Preview your saved file in a browser to confirm that the heading is red.
This is what your code should look like after saving:
#
Adding Styles in the head of the HTML DocumentInline styles are not optimal. It is much better to add them to the head of the document. But not the best way. We will get to the best way after this example.
Add this code block in the head:
Like this:
Leave the code as is and save and preview your file. Confirm that you have a blue paragraph and a red heading 1. If you add another paragraph element in the body, will it also be blue?
There are some situations where it is okay to put one or two styles in the head of the same document. But it is never optimal to put a lot of CSS code in the top of your document.
#
CSS Placed in a Separate DocumentThe best way to add multiple styles to your webpage or website is to put the styles in a separate document and then link them to your web page or web pages.
Within your root folder, create a file named style.css
.
You can also name it as you wish. Examples might be main.css
or primary.css
or unc_theme.css
but make sure you don't leave spaces or use bad naming conventions. Do try to make it understandable and relevant.
In the new empty file enter a body tag and give it a background color of tan or choose another. Here is a list of CSS Named background colors. There are only 140 named colors. All named colors can also be described in HEX values
Notice that you do not need to include the
<style>
tags if you are putting your styles in a separate css file.
It's time to address a common confusion before we go on... Linking a css file to an HTML file is very different than creating a text link out to a funny cat gif. That is an anchor link and it looks like this:
HENCE, why it starts with an a; a is for anchor. These anchor links have to go in the body of your html file. By the way, as you may have already found out, the link is not really a funny cat gif.
#
Linking The webpage to the CSS File (Same level in folder)Here is what the HTML Link tag looks like:
The link to a CSS file must go in the head of your web page. If you have multiple web pages that use the same css file, they also must include the exact same link in the head of the html page.
The file name must exactly match the file name you created, and, this is very important, it must be at the same level of your index.html or other html files, in the directory, for it to work as shown above.
#
Linking to a Stylesheet (Stylesheet(s) in Directory in the root folder)Look at the file set up below. There are two stylesheets in the stylesDirectory. You must reference them so that they can be found in the directory:
Here is how you link to just the style.css file:
Here is how you link to both style.css
and unc_theme.css
file:
#
Experiment with some cssYou now know all you need to know to start experimenting with CSS. Have some fun! But make sure your CSS validates before you submit your 02.02 Assignment in Sakai.