CMPT 165

Introduction to the Internet
and the World Wide Web

By Hassan S. Shavarani

Unit3: Stylesheets

Topics

  1. Styles
  2. CSS Basics
  3. CSS Properties
  4. CSS Selectors
  5. Colors in CSS
  6. Styling Pages with CSS
  7. Browser Compatibility
  8. Separating Content and Appearance
  9. CSS Fonts [optional content]
  10. 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 [*]

Page without style * images from http://www.cs.sfu.ca/CourseCentral/165/common/study-guide/figures/withstyle[1/2].png Page with style

CSS Basic Rule Syntax


selectorlist {
	property: value;
	[more property:value; pairs]
}
... where selectorlist is:
	selector[:pseudo-class]
		[::pseudo-element]
		[, more selectorlists]
					
* from https://developer.mozilla.org/en-US/docs/Web/CSS/Reference

text-align

font-style

font-weight

color

background-color

border properties

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;
}
CSS Box Model * 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

HTML Example

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

HTML+CSS Example * 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
Any Questions?