|
Web Design ![]() This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Lecture /
Introduction to Cascading Styles SheetsA Web Design Lecture by Steven Choy Overview: Three different ways to apply CSS to HTML - CSS Selectors, Properties, and Values - How to use color - How to manipulate text - How to space things out - How to make a border around an element - Class and ID Selectors - Grouping and Nesting
Three different ways to apply CSS to HTML
They are put straight into the HTML tags using the style attribute.
Examples
<h1 style="color:#f00; font-size:22px;">Some Heading</h1>
<p style="margin-top:30px;">Some Text</p>
They are put inside the head tags using the style tag.
Example
<style type="text/css">
p { margin-top:30px; }
h1 { color: #f00; font-size:22px; }
</style>
They are put in an external file that can be linked to in the HTML using the link tag. The link to the CSS file should be put inside the head tag of the HTML.
Example
<link rel="stylesheet" type="text/css" href="style.css" />
Note: there are other ways of linking external style sheets, but the above way should be suffice for most people.
1) What are the pros and cons of these three ways of applying CSS?
2) Which way should we used when we make Web pages?
"The cascade in CSS or cascading style sheets is the ability to have multiple styles from different sources merge together into one definitive style. CSS defines the cascade in several ways, but the simplest is to remember that style properties that come later are more likely to be applied than those that come earlier."
CSS Selectors, Properties, and Values
body {
font-size: 12px;
color: #000;
}
How to use color?
17 color names: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, orange, purple, red, silver, teal, white, and yellow.
Example of rgb value: rgb(0,0,255) = blue color, rgb(255,0,0) = red color
Example of hex code: #0000FF = blue color, #FF0000 = red color
The hex color code is six digits in length.
The three-digit version of the hex color code is a compressed version of the six-digit.
Examples: #00f = #0000ff, #96c = #9966cc
Example:
h1 {
color: #0033CC;
background-color: #EEEEEE;
}
How to manipulate text?
How to space things out?
Properties:
margin-top
margin-right
margin-bottom
margin-left
Properties
padding-top
padding-right
padding-bottom
padding-left
How to make a border around an element?
The value can be solid, dotted, dashed, etc
Example
h2 {
border-style: solid;
border-width: 1px;
border-color: red;
}
CSS Shorthand Properties
border-width: 1px 5px 10px 20px;
border: 1px #F00 solid;
margin: 10px 20px 20px 10px;
padding: 20px 10px 20px 10px;
Background Images
the location of the background image (relative to the CSS location of HTML location).
specify how the background image repeats itself, which can be
repeat, repeat-y, repeat-x, no-repeat
it can be top, center, bottom, left, right or any sensible combination, such as above.
Class and ID Selectors
Format
.Class_Name {
property_1:value;
property_2:value;
...
}
Example
.xyz {
color:#ccf;
background-color: #ccc;
padding: 20px;
}
Some examples of applying this style to the HTML
<h2 class="xyz">This is heading 2</h2>
<p class="xyz">This is a paragraph</p>
<div class="xyz">. . . </div>
"The difference between an ID and a class is that an ID can be used to identify one element, whereas a class can be used to identify more than one."
"The difference between ID and class is that an ID selector can be called only once in a document, while a class selector can be called multiple times in a document. The second difference is that ID can be called by Javascript's getElementByID function."
Grouping
Format
h2, h3, .thisClass, .anotherClass {
property:value;
}
Nesting Example
#top {
background-color: #ccc;
padding: 1em
}
#top h1 {
color: #ff0;
}
#top p {
color: red;
font-weight: bold;
}
<div id="top">
<h1>Chocolate curry</h1>
<p>This is my recipe for making curry purely with chocolate</p>
<p>Mmm mm mmmmm</p>
</div>
CSS Display Property
CSS Position Property
The default positioning for all elements is position:static, which means the element is not positioned and occurs where it normally would in the document.
If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document.
When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go.
The absolute element can be placed anywhere on the page using top, right, bottom and left.
The element is positioned relative to the browser window
CSS Float Property
References
Thanks for ReadingIf you would rather like to have this lecture note in printed format, please click the print action link in the top right corner. If you find any problem in this lecture note, please feel free to tell Steven via steven [at] findaway.hk. |