CSS
Welcome to the lovely world of CSS. We have finally learned HTML and now we can move onto the more interesting part of the web, CSS. As said before for HTML there will be terminology here that is unfamiliar to you but by the end of this tutorial you will be able to build a website using HTML and CSS.
What is CSS?
CSS means Cascading Style Sheets. CSS styles a webpage but does not display a webpage, where HTML displays a webpage it cannot style a page. CSS has a hierarchy which it uses to determine which styles will be displayed. The hierarchy is as follows:
- In-line CSS Rules
- Internal CSS
- External CSS
- Browser defined styles
These above state in which order the rules will be displayed and also if any rules will be over-ridding (Why its called cascading) . If none of the CSS you wrote is used for styling then the browser will determine how it should be styled. For example if you do not say how a table is to be styled the browser will take control and style it.
What is In-line CSS rules?
In-line CSS is where the styling is in line with the HTML code. It is the first step in the hierarchy and over-rides all other styles. It takes a long time to style a page using In-line but it does have its advantages, say if you want to style only 1 piece of the page and you will not be using this style again then In-line is great for this.
What is Internal CSS?
Internal CSS is where the styling is written in the same document as the HTML code but the styling is at the top of the document. You would use this if you had 1 page in your site that you want to have a different styling for, or if you want to change a lot of a single pages styling to be different from the rest.
What is External CSS
External CSS is like internal only that its in an entire different document to the HTML. External CSS is in its own document with nothing else. This is the most common use for CSS and is also the biggest advantage of using CSS. If you have styles that must be repeated in every page or most pages then you would put that style in the external CSS file.
Using external CSS allows you to create a common look across all pages and you only would have to write it once, also if you want to edit a small portion of your website you would only need to edit it once and the change would be seen across all pages. Saving you time writing it in the first place and also editing.
First off we will learn In-line CSS, as the is always the starting point for CSS and also it really shows you why CSS has internal and external. Also it allows you to see what CSS is capable of.
A good website uses a combination of all the different ways to write CSS, the difficult part is figuring out how you want to style the page and also if it should be an Internal style, In-line or External.
In-line CSS
In-line CSS Rules are written slightly differently than external and internal. But don?t let that put you off learning it, as it is only a small difference and also you may need to use In-line sometime.
In-line rules cant be over-ridden by any other CSS styles, cause it is at the top of the hierarchy. Best way to learn CSS is to see it in work, so here we go.
With in-line we will be learning majority of CSS, that way you will only have to learn the different syntax for Internal and External and you will fully understand CSS.
Styling Text Using CSS
Today we will be learning the syntax for in-line styles and also we will be learning some of the most commonly used styles for text.
Steps:
- Open Skeleton.html in Notepad
- Save file as styling_text.html
- Type as follows in body:
Code:
<body style="font-family:"Times New Roman"Georgia,Serif;">
[INDENT]<h1 style="color:blue; text-align:center;">This heading is blue and also it is centered. You can set basic colors using the name of the colors, but you are limited to choice</h1>
[/INDENT]
[INDENT]<h2 style="color:rgb(255,0,0); text-align:left;"> This heading is red and is left aligned</h2>
[/INDENT]
[INDENT]<h3 style="color:#669900; text-align:justify;"> This heading is using hexadecimal and is the most common way of coloring text or anything else in CSS.</h3>
[/INDENT]
[INDENT]<br />[/INDENT]
[INDENT]<h4 style="text-decoration:underline; text-transform:uppercase;"> This is how you underline text and also how to make it all uppercase.</h4>
[/INDENT]
[INDENT]<h5 style="text-decoration:line-through;text-transform:capitalize;"> This is how to strike a line through the text and also change the first letter of each word to uppercase.</h5>
[/INDENT]
[INDENT]<h6 style="text-decoration:overline;text-transform:lowercase;"> This is how to put an overline above the text and also to make all the text lowercase.</h6>[/INDENT]
[INDENT]<hr />
[/INDENT]
[INDENT]<p style="text-indent:50px;text-shadow: 2px 2px #ff0000;"> This paragraph is indented by 50px and it is also got a shadow.</p>
[/INDENT]
</body>
- Save file
- Open stying.html in browser to see your styled page
Breakdown:
- The body tag is declaring which font will be used for the text. The browser will display from left to right in the list, depending on which one the visitor of the page has on the computer.
- The ?<h1>? tag is set to be blue using the most basic way of styling the color of the text; by just writing the name of the color. The CSS that achieves this is ?color:blue;?.
- The ?<h1>? is also centered. This achieved by using the property text-align and setting its value to center as shown here ?text-align:center;?
- The ?<h2>? tag is set to red using rgb. This provides more selection of colors than is provided for the color changing used in the h1 tag. It is wrote like this ?color:rgb(255,0,0);?
- H2 is also aligned to the left of the page using the text-align property, by setting its value to left.
- ?<h3>? is using the hexadecimal color system and is the most common color system used on the web. It is wrote like this ?color:#669900;?
- ?<h3>? is also aligned using the justify value for the property text-align. Wrote like this text-align:justify;.
- <h4> introduces how to decorate the text using the property text-decoration. h4 is underlined and this is how it is writing text-decoration:underline;.
- H4 is also the intro for the property text-transform. h4 is all in uppercase.
- H5 and H6 just show other values for the property text-decoration and text-transform.
- The paragraph tag has an indent and also shadowing. this was achieved by using text-indent:50px; for the indent, 50px is the distance it is indented and 2px and 2px is the size of the shadow effect applied to the paragraph, the hexadecimal just chooses which colour the shadow will be. text-shadow: 2px 2px #ff0000;.
Play around with the code. You learn more from your mistakes, espically when it comes to CSS. Its all about just trying stuff out.
"In life, there are no mistakes, only lessons."
? Vic Johnson, Day by Day with James Allen