CMPT 165
Introduction to the Internet
and the World Wide Web
By Hassan S. Shavarani
Unit3: Stylesheets
Topics
- Styles
- CSS Basics
- CSS Properties
- CSS Selectors
- Colors in CSS
- Styling Pages with CSS
- Browser Compatibility
- Separating Content and Appearance
- CSS Fonts [optional content]
- Interactive Color Mixer [optional content]
Styles
CSS (Cascading Style Sheets) language will give us a way to express information about the appearance of different parts of our content
CSS information (style sheets) will also be edited with a text editor, and saved in ".css" files
Why Separate Style Information?
- CSS will allow us to give all of our site's appearance information in one place
- even if we have hundreds of HTML files, they can all refer to one CSS file
- if we want to make a change in the appearance of a site, it requires only a single change
- we can also have separate people working on different parts of the site
CSS Basics - First Problem
Let's say we want to use CSS to change the headings on the page
we want the <h2> elements to be
centred (instead of the default left-justified)
and in an italic font
CSS Basics - First Solution
make a new file call it firststyle.css
h2 {
text-align: center;
font-style: italic;
}
In this example:
- h2 {...} block is a CSS rule
- "h2" in the CSS is a type selector
- two lines in the curly braces are CSS declarations each of which defines a property and a value
<link> tag
Let's link the generated CSS file to the HTML codes
<head>
<meta charset="UTF-8" />
<title>Page with a style sheet</title>
<link rel="stylesheet" href="firststyle.css" />
</head>
Lets apply the CSS [*]
* images from http://www.cs.sfu.ca/CourseCentral/165/common/study-guide/figures/withstyle[1/2].png
border properties
- used to control the border around an element (width of the line, type of line, and colour of line, respectively)
- used to set the border attributes of an element including border-width, border-style, and border-color
-
look at the border property demos at
border properties
border properties can be used separately or as a shorthand property border to combine these into one line (giving the three values in any order)
figure {
border-style: solid;
border-width: 3px;
border-color: red;
}
figure {
border: solid red 3px;
}
line-height
- sets the amount of space used for lines, such as in text
- on block-level elements, it specifies the minimum height of line boxes within the element
- on non-replaced inline elements, it specifies the height that is used to calculate line box height
- look at the demo at https://developer.mozilla.org/en-US/docs/Web/CSS/line-height
font-family
- specifies a prioritized list of one or more font family names and/or generic family names for the selected elements
- the list of fonts are tried in-order until the browser finds one available on the user's system
- there are six generic font families and your list must end with one of them since it's guaranteed to work
- look at the demo at https://developer.mozilla.org/en-US/docs/Web/CSS/font-family
CSS Box Model
<h2>Element Contents</h2> Example
h2 {
padding: 1em;
border: medium dashed black;
background-color: grey;
}
* image from http://www.cs.sfu.ca/CourseCentral/165/common/study-guide/figures/boxmodel.svg
CSS Units
em
- The current font size: if the current text is 12 point then this will be 12 points. Another unit, an
ex
is half the text size.
px
- One screen pixel (dot) on the display. (Note: for some very high resolution devices, real “pixels” are very small, so this length is adjusted to be close to the size of a pixel on a traditional display.)
mm
- A millimetre. There are also units for centimetre, inch, etc. (Note: this is the browsers best guess, but might be innacurate depending on the scaling of the display/projector/phone/etc. For example if you display your screen on a projector, the “millimetre” suddenly becomes much larger.)
HTML Example Code
CSS Example Code
body {
font-family: "Helvetica", sans-serif;
}
h1 {
text-align: center;
font-weight: bold;
background-color: silver;
color: teal;
padding: 0.25em;
}
h2 {
border: medium dotted teal;
font-weight: normal;
padding: 0.1em;
}
Example Code Result
* image from http://www.cs.sfu.ca/CourseCentral/165/common/study-guide/figures/css-prop-result.png
* try the result at css-prop-page.html