css foundations, pt 2

64
22-3375 Web Design I // Columbia College Chicago CSS Foundations, pt 2

Upload: shawn-calvert

Post on 27-Jan-2015

105 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: CSS Foundations, pt 2

22-3375 Web Design I // Columbia College Chicago

CSS Foundations, pt 2

Page 2: CSS Foundations, pt 2

Review

Three different approaches to applying your css to the html

How the browser interprets the “cascade” of style rules

How to write CSS rules

How to add class and id selectors to your html for more styling control

What is css and how it’s relationship to your html files

Understand basic css properties for text styling

Page 3: CSS Foundations, pt 2

How to add css to your document

Page 4: CSS Foundations, pt 2

External (linked)

Internal <head>

There are 2 basic ways to add styles to your html page:

Page 5: CSS Foundations, pt 2

<head> <link rel="stylesheet" type="text/css" href="stylesheet.css"></head>

Except in special circumstances, best practice is to keep all your styles in a separate css document.

Linked stylesheets can be applied across multiple documents and sites.

External

Page 6: CSS Foundations, pt 2

<head> <style> p {color: red} </style></head>

You can add styles directly into your html page using the “style” element in the <head> of your document.

Embedded styles apply ONLY to that html document.

Internal (embedded)

Page 7: CSS Foundations, pt 2

<p style=”color: red”>Red Text</tag>

You can add styles directly into your html markup using the “style” attribute on opening tag of your element. This is called an “inline” style.

Inline styles apply ONLY to that specific instance of the element.

Internal (inline)

Page 8: CSS Foundations, pt 2

Best Practice

In most cases you should use the external method of adding styles to your html page.

Writing the css in the <head> of your document is acceptable if you definitely only want to apply the styles to a single page.

However, adding your styles “inline” — inside a html starting tag, e.g. <p style=”font-size: 14px”> — should be avoided.

Page 9: CSS Foundations, pt 2

Class Exercise

!

!

download class tutorial files

create new css file in ‘review’ folder

link to the css file from review.html file

Page 10: CSS Foundations, pt 2

CSS Selectors

Page 11: CSS Foundations, pt 2

p Type (element)

# ID

. Class

Page 12: CSS Foundations, pt 2

body {declaration} p {declaration} h1 {declaration} ul {declaration}

The simplest selector is the type selector, which targets an html element by name.

Type (element) Selectors

Page 13: CSS Foundations, pt 2

The essential selector types (elements)

PrimaryStructure html body

BodyElements p br h1 – h6

ul ol a img div

FormattingElements em i strong b q blockquote span

Page 14: CSS Foundations, pt 2

div elements

Div elements allow you to group a set of elements together in one block-level element. !<div id=”head”> <a bunch of elements></> </div>

Page 15: CSS Foundations, pt 2

span elements

span elements are the inline equivalent of divs. !<p>Here is some text <span class=”red-text”> with a span</span> added.</p> Spans are the equivalent of ‘character’ styles in InDesign.

Page 16: CSS Foundations, pt 2

type selectors vs IDs & Classes

Type selectors use the tag names that are built into HTML.

IDs and classes use names that you define, and are added to html elements to make them more specific.

Page 17: CSS Foundations, pt 2

CSS #logo {declaration}"

HTML <img id=”logo” src=”” alt=””>

An ID is an html attribute that is added to your html markup. You reference that ID in your css with a hash.

ID Selectors

Page 18: CSS Foundations, pt 2

CSS .ingredients {declaration}"

HTML <ul class=”ingredients”>

A class is an html attribute that is added to your html markup. You reference that ID in your css with a period.

Class Selectors

Page 19: CSS Foundations, pt 2

IDs vs Classes

The most important difference between IDs and classes is that there can be only one ID on a page, but multiple classes.

An ID is more specific than a class.

An element can have both an ID and multiple classes.

Page 20: CSS Foundations, pt 2

IDs vs Classes

ID: #344-34-4344 Class: Male Class: Student

ID: #123-54-9877 Class: Female Class: Student

Page 21: CSS Foundations, pt 2

Descendant Selectors

CSS #sidebar .author {declaration}"

HTML

<div id=”sidebar”> <p class=”author”></p> </div>

A space between two selectors indicates a descendant selector. In the example above, the style is targeted to an element with the class “author” inside the id “sidebar”.

Page 22: CSS Foundations, pt 2

Multiple classes

CSS .ingredients.time {declaration}"

HTML

<div class=”ingredients time”> <h1></h1> </div>

Elements can have multiple classes, giving you more control. The are written in the CSS in the exact order they appear in the html, with no spaces.

Page 23: CSS Foundations, pt 2

Combining IDs and classes

CSS #wrapper.ingredients.time {declaration}"

HTML

<div id=”wrapper” class=”ingredients time”> <h1></h1> </div>

Elements can have both ids and classes. While an element can have only one id, it can have multiple classes.

Page 24: CSS Foundations, pt 2

Class Exercise

!

Copy the following values from another site:

Page 25: CSS Foundations, pt 2

The Box Model:Defining the dimensions

Page 26: CSS Foundations, pt 2

There are five basic properties for defining your “boxes”:

display

width and height

padding

margins

border

Page 27: CSS Foundations, pt 2

CSS Box Model

All HTML elements can be considered as boxes or blocks. In CSS, the term "box model" is used when talking about design and layout.

The CSS box model is essentially a box that wraps around HTML elements, and it consists of: margins, borders, padding, and the actual content.

!

Inspect Element 3d view in Firefox

Page 28: CSS Foundations, pt 2

Total dimensions

The actual width or height of any object is the total of the content width, padding, borders, and margins.

.size-me { width: 200px; padding: 10px; border: 2px; margin: 20px; }"

The actual width: 254px

content: 200pxpadding-left: 10pxpadding-right: 10pxborder-right: 2pxborder-left: 2pxmargin-right: 20pxmargin-left: 20px

!

Page 29: CSS Foundations, pt 2

Display property

By default, certain elements in your html have a display property of inline or block.

Block elements takes up the full width available, and forces a line above and below. Examples include <p>, <div>, <ul>, <blockquote> and all headers.

<div></div>

<ul></ul>

<p></p>

Page 30: CSS Foundations, pt 2

Display property

Inline elements can be inserted within block elements or other inline elements and do no create additional space or line breaks. Examples include <img>, <em>, <strong>, <a>, and <span>.

<p></p>

<p>

<p></p>

<a></a> </p>

Page 31: CSS Foundations, pt 2

Display property

There are four values that you can give to the display property:

display: block;

display: inline;

display: inline-block;

display: none

!

Inline-block is a special value that allows an element to stay inline with its containing element, but allows you to control it’s dimensions as if it were a block element.

display: none removes the element.

Page 32: CSS Foundations, pt 2

Display property

For example, by default, a <li> element is set to display: block. If we change that value in our css to:

li {display: inline-block;}

Then this: Changes to this:

<ul>

<ul>

<li>

<li>

</li>

</li>

<ul>

<ul>

<li> </li> <li> </li>

Page 33: CSS Foundations, pt 2

Class Exercise

!

1 Inline and Block

Page 34: CSS Foundations, pt 2

Class Exercise

!

2 Borders and Margins

Page 35: CSS Foundations, pt 2

Defining dimension: width and height

You can set the width and height of any element that has a display property of block or inline-block.

As with type, you can set dimension in pixels (px), ems, or percentage (%). Setting any width with pixels is referred to as fixed-width, and setting width in ems or percentages is referred to as flexible-width.

.size-me { width: 200px; height: 200px; }

!

!

Page 36: CSS Foundations, pt 2

Defining dimension: min- and max-

Instead of absolutely defining the width and height of your elements, you can set minimum and maximum widths and heights.

This allows you to resize elements based on the browser or their parent elements. The idea of creating flexible content that adjusts to the user’s browser/device is at the core of what is referred to as responsive design.

.size-me { min-width: 200px; max-width: 100px; }"

Page 37: CSS Foundations, pt 2

Defining dimension: min-width and max-width

!

!

!

Page 38: CSS Foundations, pt 2

Defining dimension: min-width and max-width

!

!

!

Page 39: CSS Foundations, pt 2

Defining padding & margins

You can set the padding and margin properties of any element in css.

Margin and padding dimensions can be also pixels (px), ems, or percentage (%).

.space-me { padding: 10px; margin-bottom: 20px;}

!

!

!

Page 40: CSS Foundations, pt 2

Defining padding & margins

While you can add padding and margins to inline elements, not all of your properties will apply (vertical spacing, see below).

!

!

!

Page 41: CSS Foundations, pt 2

Collapsing margins

When two or more margins collapse, the resulting margin width is the maximum of the collapsing margins' widths.

In the example below, box1 has a taller margin than box2, so only that margin will be used (not added together). !!

Page 42: CSS Foundations, pt 2

CSS Shorthand

1

2

3

4

When setting sizes for padding, margins and borders, instead of this: .box { padding-top: 10px; padding-right: 15px; padding-bottom: 10px; padding-left: 15px; }

Use this: .box { padding: 10px 15px 10px 15px; }"

The values start at the top and go around the element clockwise. !

!

Page 43: CSS Foundations, pt 2

CSS Shorthand

1

2

1

2

If your top/bottom and left/right values are the same, you can shorten the declaration even more: .box { padding: 10px 15px; }"

Where the first value is for the top and bottom, and the second is for left and right. !

!

Page 44: CSS Foundations, pt 2

Defining borders

You can create a border around any element with css; you can create the same border on each side, or customize the color, width and border style of each side.

!

There are 3 properties to define with each border:

border-width: 1px;

border-style: solid; (dotted, dashed, etc)

border color: #666666;

!

!

!

Page 45: CSS Foundations, pt 2

CSS Shorthand

Borders can also be shortened, from this: .box { border-width: 1px; border-style: solid; border-color: #888888 }"

to this: .box { border: 1px solid #888888; }"

Note that there is only a single space between each value, and they should follow the order shown above.

!

!

Page 46: CSS Foundations, pt 2

Defining borders

h3 { border-bottom: 1px solid #444444;}

!

!

Page 47: CSS Foundations, pt 2

Class Exercise

!

3 Centered, Fixed-Width Page

Page 48: CSS Foundations, pt 2

The Box Model:Positioning

Page 49: CSS Foundations, pt 2

There are four basic declarations you can use to position your boxes:

float: left (or right)

position: relative

position: fixed

position: absolute

Page 50: CSS Foundations, pt 2

position: static

Static positioning is the default – this is referred to as the “normal flow”.

block boxes are positioned on a page one after the other (in the order they're written in the HTML). They start in the upper left of the containing box and stack from top to bottom. The distance between each box is defined by the margins with top and bottom margins collapsing into one another.

!

!

!

<div id=”header”></div>

<ul id=”sidebar-nav”></ul>

<div id=”content”></div>

Page 51: CSS Foundations, pt 2
Page 52: CSS Foundations, pt 2
Page 53: CSS Foundations, pt 2

float

A floated element takes the element out of the normal flow and moves it to the far right or left of the containing element. Existing content will then flow around it. Floated elements should always have at least a width property.

.photo { float: left; max-width: 150px; }"

!

!

Page 54: CSS Foundations, pt 2

!

.photo { float: left; max-width: 150px; }"

!

!

4 Float

Page 55: CSS Foundations, pt 2

Class Exercise

!

5 Positioning

Page 56: CSS Foundations, pt 2

Positioning Properties

There are four values that you can give to the position property:

! static Elements renders in order, as they appear in the document flow. This is default.

relative The element is positioned relative to its static position, so “left:20” adds 20 pixels to the element’s LEFT position.

absolute The element is positioned relative to its first positioned (not static) ancestor element (usually, the first parent with position:relative).

fixed The element is positioned relative to the browser window.

Page 57: CSS Foundations, pt 2

boxes exercise

.box { "" width: 300px;"" height: 300px;"" border: 1px solid #333;"" margin: 10px;"" display: inline-block;}" "

.relative { "" position: relative;"" top: 50px;"" left: -50px;}"

.fixed { "" position: fixed;"" bottom: 0;"" left: 0;}"

!

!

Page 58: CSS Foundations, pt 2

boxes exercise

.box.absolute { "" position: relative;}"

.box.absolute p { "" position: absolute;"" top: 0;"" right: 0;"" background: #444;"" padding: 10px;"" margin: 0;"" color: #fff;}"

!

!

!

Page 59: CSS Foundations, pt 2

relative

A relative positioned element moves the element out of it’s position in the normal flow, and allows you to move it “relative” to that location. The space taken by the original location is retained in the flow.

.date { position: relative; top: 40px; left: -105px; }"

!

!

Page 60: CSS Foundations, pt 2

relative exercise

!

!

!

.date { position: relative; top: 40px; left: -105px; }"

!

!

Page 61: CSS Foundations, pt 2

fixed

A fixed element is taken complete out of the document flow, and is positioned relative to the browser window.

.create-change { position: fixed; bottom: 40px; right: 0; }"

!

!

Page 62: CSS Foundations, pt 2

fixed exercise

!

!

!

.create-change { position: fixed; bottom: 40px; right: 0; }"

!

!

Page 63: CSS Foundations, pt 2

absolute

The element is positioned relative to its first positioned (not static) ancestor element. The element and its spacing are completely taken out of the flow. In the example below, the “.post” parent element is given a position value of relative, which lets us set it’s child element “.icon” within it.

.post { position: relative; }"

.icon { position: absolute; left: -60px; right: 0; }"

!

!

Page 64: CSS Foundations, pt 2

absolute exercise

!

!

!

.post { position: relative; }"

.icon { position: absolute; left: -60px; right: 0; }"

!

!