intro to css

71
Intro to CSS

Upload: amazing-rando

Post on 27-Jan-2015

111 views

Category:

Technology


3 download

DESCRIPTION

Slides from my Intro to CSS class. More info at http://randyoest.com.

TRANSCRIPT

Page 1: Intro to CSS

Intro to

CSS

Page 2: Intro to CSS

Randy Oest Manager of Digital Design Williams Randall Marketing

!

@amazingrando

Page 3: Intro to CSS

Why Learn CSS?

1. Running a blog on a CMS and want to tweak things

2. Working with a developer and want to understand how to talk with them

3. Web designer looking to understand how things get built

Page 4: Intro to CSS

What We’ll Learn

1. What is CSS

2. How to speak CSS

3. How to apply it

4. Where to find out more info

Page 5: Intro to CSS

If HTML is the Skeleton of Web pages…

Page 6: Intro to CSS

CSS is the muscle and skin.

Page 7: Intro to CSS

CSS Needs HTML

CSS is used to style elements, giving them form.

Page 8: Intro to CSS

HTML Elements

<p>Paragraph …</p>  

<div  class=“nav”>…</div>  

<h1  id=“title”>Headline</h1>  

Page 9: Intro to CSS

CSS Needs HTML

Selectors are used to target styles.

Page 10: Intro to CSS

Examples of Selectors

<p>Paragraph …</p>  

p  {  …  }  

Page 11: Intro to CSS

Examples of Selectors

<div  class=“nav”>…</div>  

.nav  {  …  }  

Page 12: Intro to CSS

Classes

Class names always start with a period. .class

Page 13: Intro to CSS

IDs

IDs always start with a hash. There can only be one on a page.

#id

Page 14: Intro to CSS

Think of classes as your name and nicknames, e.g. Randy, Amazing Rando, Hey You…

Think of IDs as your unique social security or credit card numbers.

Page 15: Intro to CSS

Multiple Selectors

<h1  id=“title”>Headline</h1>  

h1#title  {  …  }  

No spaces!

Page 16: Intro to CSS

Specificity

When there is a style conflict, IDs trump classes, and classes trump HTML elements

Page 17: Intro to CSS

Nested Elements

<div  id=“feature”>  

  <img  src=“…”  />  

  Some Text …

</div>

Page 18: Intro to CSS

Nested Elements

<div  id=“feature”>  

  <img  src=“…”  />

#feature  img  {  …  }

Now there is a space.

Page 19: Intro to CSS

.class  {     color:  #fff;     padding:  10px;  }

Properties & Values

Property Value

Page 20: Intro to CSS

PX: Pixels

%: Percentage

EM: Relative unit, equal to the current font size

Common Size Units

Page 21: Intro to CSS

EMs, being relative units, can get complicated quickly when you nest them.

Warning about EMs

Page 22: Intro to CSS

1. External File

2. Internal, using the <style> tag

3. Inline styles

Getting CSS into the HTML

Page 23: Intro to CSS

Put this inside the <head>

<link  rel="stylesheet"  href="file.css">

External File

Page 24: Intro to CSS

Put this inside the <head>

<style  type="text/css">     …  your  styles  …  </style>

Internal

Page 25: Intro to CSS

<h1  style=“color:#fff;”>    Headline  </h1>

Inline HTML

Page 26: Intro to CSS

<interlude>

Page 27: Intro to CSS

Obligatory Baby Pictures

Page 28: Intro to CSS
Page 29: Intro to CSS
Page 30: Intro to CSS
Page 31: Intro to CSS

</interlude>

Page 32: Intro to CSS

Box Model

Page 33: Intro to CSS

Elements of the Box

Page 34: Intro to CSS

An element’s size is equal to:

Width (or Height) + Padding + Border + Margin = Total Size

The Box Model is Tricky

Page 35: Intro to CSS

.box  {    width:  200px;    padding:  10px;    border:  5px;    margin:  15px;  }

Page 36: Intro to CSS

So if we plug in values:

Width (200px) + Padding (10px * 2 sides)+ Border (5px * 2 sides) + Margin (15 px * 2 sides) = Total Size (260px)

The Box Model is Tricky

Page 37: Intro to CSS

There is a Better Way

Page 38: Intro to CSS

Use a Different

Box Model

Page 39: Intro to CSS

*,  *:before,  *:after  {  -­‐webkit-­‐box-­‐sizing:  border-­‐box;  -­‐moz-­‐box-­‐sizing:  border-­‐box;  box-­‐sizing:  border-­‐box;  }

Magic Bullet

Page 40: Intro to CSS

selector  {  -­‐webkit-­‐box-­‐sizing:  border-­‐box;  -­‐moz-­‐box-­‐sizing:  border-­‐box;  box-­‐sizing:  border-­‐box;  }

Fixing A Single Item

Page 41: Intro to CSS

An element’s size is equal to the width size.

Border Box is NOT Tricky

Page 42: Intro to CSS

Content-Box (Default)

Border-Box (Good!)

Page 43: Intro to CSS

padding-­‐top:  10px;  padding-­‐right:  5px;  padding-­‐bottom:  10px;  padding-­‐left:  5px;  

OR padding:  10px  5px  10px  5px;

Shorthand

Page 44: Intro to CSS

Starts at NOON and goes clockwise. padding:  top  right  bottom  left;

Shorthand

Page 45: Intro to CSS

padding:  top+bottom  right+left;  padding:  10px  5px;

Even More Shorthand

Page 46: Intro to CSS

The Display Property

Page 47: Intro to CSS

Block: <h1>,  <p>,  <div>  

Inline: <a>,  <span>,  <b>,  <strong>

The Display Property

Page 48: Intro to CSS

a  {    display:  block;  }  

The Display Property

Page 49: Intro to CSS

There are many more exotic display types, such as inline-­‐block, none, etc.

The Display Property

Page 50: Intro to CSS

Flow

Page 51: Intro to CSS

Floats

Page 52: Intro to CSS

img  {    float:  left;  }  

The Display Property

Page 53: Intro to CSS

Positioning can change an element in the flow.

Page 54: Intro to CSS

Positioning

Parent

Element

position:  static

Page 55: Intro to CSS

img  {    position:  static;  }  

Positioning

This is the default, no need to write it

Page 56: Intro to CSS

Positioning

Parent

Element

position:  fixed

Fixed in relation to the browser.

Page 57: Intro to CSS

img  {    position:  fixed;    top:  0px;      left:  0px;  }  

Positioning

Page 58: Intro to CSS

top:  0px;    right:  0px;  bottom:  0px;  left:  0px;  

Positioning

Page 59: Intro to CSS

Positioning

Parent

Element

position:  relative

Page 60: Intro to CSS

img  {    position:  relative;    top:  -­‐10px;      left:  -­‐200px;  }  

Positioning

Page 61: Intro to CSS

Positioning

Parent

Element

position:  absolute

position:  static

Page 62: Intro to CSS

img  {    position:  absolute;    top:  0px;      left:  0px;  }  

Positioning

Page 63: Intro to CSS

Absolute positioning looks for the parent with a position other than static.

Page 64: Intro to CSS

Positioning

Page 65: Intro to CSS

Positioning

Parent

Element

position:  absolute

position:  relative

Page 66: Intro to CSS

img  {    position:  absolute;    top:  0px;      left:  0px;  }  

Positioning

Page 67: Intro to CSS

A Quick Note About Mobile

Page 68: Intro to CSS

Most CSS that you write for mobile is just CSS.

Page 69: Intro to CSS

Percentages and relative units, like EMs are better than pixels, which are static width.

Page 70: Intro to CSS

@media  all  and  (max-­‐width:  699px){       …  styles  }

Media Query

Page 71: Intro to CSS

Further Learning

RandyOest.com/CSS