COMSM0085

Overview of Software Tools

Software Tools: Part 2

(COMS10012 / COMSM0085)

Last week (Week 6): HTTP & HTML

This week (Week 7): CSS

Next week (Week 8): JS


CSS

Cascading Style Sheets (we’ll get to the ‘cascading’).

A set of rules that define how HTML elements are displayed.

In most cases, rules refer to the setting of visual properties for a HTML document element (more technically: to the DOM objects derived from the HTML).


CSS composition

CSS entries follow fairly simple syntax:

selector {
    property : value;
}

CSS implementation

Your CSS rules should be placed in a stylesheet document e.g., mystyle.css.

The browser can then be informed to use a particular stylesheet by a reference in the HTML document.

<html>
 <head>
    <link rel="stylesheet" href="mystyle.css"/>
 </head>
 <body>
  ...

You can also just instruct your browser to apply a custom stylesheet to HTML documents by default (‘user’ or ‘custom’ styles). But as a web designer you assume your website visitors want to see the webpage the way you intended it to be viewed.


CSS simple example

p {
 color : red;
}

For all p elements, set the color (font colour) to red.


Selectors

Wide range of selection capabilities.

Simplest selector: name of a single tag (e.g, p,a,div). Applies to all elements of that kind.

Next-simplest selector is a list of tags:

p, div, main {
    color : red;
}

Selectors: Class

Syntax for selecting only elements with particular class properties:

 p.important {
    color: red;
 }
 <p class='important'>This is red</p>
 <p>This is not</p>

Selectors: Class

Can also apply to class regardless of element type:

 .important {
    color: red;
 }
 <p class='important'>This is red</p>
 <p>This is not</p>
 <span class='important'>But this is</span>

Selectors: ID

If you wanted to apply style to a particular document element:

 p#uniquebox {
    color: red;
 }
 <p id='uniquebox'>This is red</p>
 <p class='uniquebox'>This is not</p>
 <p>This certainly is not</p>

Same as class – #uniquebox by itself would apply to?


Selectors: Attribute

Can generalise to select elements by any attribute.

 p[name=tim] {
    color:red;
 }
 div[border=none] {
    color: blue;
 }

p[class='important'] would be the same as p.important.

Can also do some fancy partial matching, e.g., img[title~='flower'] selects all images where the title attribute contains the string ‘flower’.


Selectors: Positional

<div class="container">
 <p>direct child</p>
 <div>
  <p>descendant</p>
 </div>
</div>
<p>para one</p>
<p>para two</p>

Selectors: Positional

Just to add complexity: all the rules can be combined.

 div.important > p, h1#main, [title=nowred] ~ span {
    color: red;
 }

Worth looking at a reference guide


Cascading?

Which rule applies?

<p class='important'>If you want to pass this unit then...</p>
p {
    font-size: 12pt;
}

p.important {
    color: red;
}

Values

Lots of different properties that can be set, which require different values – you will need to explore the MDN documentation to get to grips with all of the options.

However, some common elements relate to colour and element layout.

Color values

As well as color, you can set background-color and elements like border-color.

red and #FF0000 are identical. But #FF0001 or #FF1111 will still look ‘red’.


Layout

When laying out elements on a page, a common issue relates to dealing with space ‘around’ an element (or between elements).

Each page element can be thought of as a ‘box’ with several layers:

It’s common to get confused between padding and margin (making the border visible helps).

Developer tools give a good visual demonstration of the values (see video for this week).


Layout values

Both margin and padding can be specified for individual sides, or collectively in clockwise order.

 margin-top: 10px;
 margin-right: 20px;
 margin-bottom: 10px;
 margin-left: 5px;

 margin: 10px 20px 10px 5px;

Units of measurement

There are many different ways to specify measurement in CSS.

‘Absolute’ units try to produce a specific real size:

‘Relative’ units produce dimensions relative to either the viewport or some reference element of the page.

Very easy to get muddled about units.


Design is hard

This unit is trying to teach you some fundamental understanding of CSS.

CSS can be hard to debug and understand – technical issues.

But successfully designing styles for real websites can also be hard in a non-technical sense. There are key principles (links to fundamentals of ergonomics, audience expectations, etc.) but fundamentally a lot comes down to questions of taste, style, fashion – web design is an art.

Some concepts you may find handy:


Exercises this week

  1. Reading MDN documentation.
  2. Applying basic CSS to a HTML document.
  3. Getting very frustrated about pink lines.
  4. Using an existing CSS framework.
  5. Reading even more MDN documentation.
  6. Using a grid layout.
  7. Creating a responsive layout.