Introduction to CSS

CSS3 Logo

Philip Schmökel

Exercises

Cascading Style Sheets


<selector1>, <selector2> {
  <property1> : <value1>;
  <property2> : <value2>;
  <property3> : <value3>
}
 
<selector1> {
  <property4> : <value4>
}
 
/* Css comment */
/* Example */
p, div {
  background: red;
  font-weight: bold;
  color: blue
}
 
p {
  font-style: italic
}

Selectors

Selector Selected element(s)
p Paragraphs
.class1 Elements with class1
#content Element with identifier content
p.class1 Paragaphs with class class1
p .class1 Elements with class class1 having paragraph ancestor
.class1 .class2 Elements with class class2 having ancestor with class class1
p.class1.class2 Paragraphs with both class1 and class2 classes
Selector Selected element(s)
* All elements
div > p All <p> elements where the direct parent is a <div> element
div + p All <p> elements that are placed immediately after <div> elements
p:first-child
p:nth-child(3)
p:last-child
Selects every <p> element that is the first/third/last child of its parent
input[type="text"] Selects all <input type="text">

. . . W3C Selectors

Selector's priority

Source Ids Classes Elements

Source 2 - Inline
1 - <style></style> tag
0 - External file
Ids Number of identifiers in selector
Classes Number of classes in selector
Elements Number of elements in selector

block vs inline

  • display: block
  • displayed under each other
  • size defined by width, height, margin, padding
  • example: <div>, <section>, <p>
  • display: inline
  • displayed next to each other
  • size defined only by content size and margin-left, margin-right, padding-left, padding-right
  • example: <strong>, <i>, <a>, <span>

block vs inline

Example

<div>
    <span>1</span>
    <span>2</span><span>3</span>
</div>
<div>
    <span>4</span>
    <span>5</span><span>6</span>
</div>
<div>
    <span>7</span>
    <span>8</span><span>9</span>
</div>
1 23
4 56
7 89

Positioning

position: static

  • default value
  • positioned according to the normal flow

position: relative

  • element is positioned relative to its normal position
  • position can be adjusted with top, bottom, left, right properties
  • even if moved consumes space in container

Positioning

position: absolute

  • element is positioned relative to its first positioned (not static) ancestor element
  • position can be adjusted with top, bottom, left, right properties
  • often used together with relative positioning

position: fixed

  • element is positioned relative to the browser window
  • position can be adjusted with top, bottom, left, right properties

Positioning Example 1

body {
      position: relative
    }
    .container {
      top: 50px; left: 50px; background: green
    }
    .child {
      top: 50px; right: 50px; background: red
    }

Positioning Example 2


header
center
footer
.content {
    height: 200px;
    position: relative;
    margin-left: 100px;
    background: green;
}
nav {
    width: 100px;
    background: blue;
    position: absolute;
    top: 0;
    left: -100px;
    height: 100%;
}

Box Model

CSS Boxmodel

real-size = margin + border + padding + size
you can change what width and height refer to:
box-sizing: content-box|border-box|inherit;

Float property

  • elements are floated horizontally
  • elements after the floating element will flow around it
  • example for placing images in text:

Float and Clear

Exercise 1: Selectors

Write CSS and change

to

HTML-file: <Repository>/css/exercises/exercise1.html

Exercise 2: Selectors

Write HTML and create

HTML-file: <Repository>/css/exercises/exercise2.html

less and sass

... to make CSS more maintainable.

  • CSS pre-processor

  • variables, functions, mixins and more

$buttonColor: #2ecc71;
$buttonDark: darken($buttonColor, 10%);
$buttonDarker: darken($buttonDark, 10%);

button {
  background: $buttonColor;
  border-radius: 3px;
  box-shadow: 0px 5px 0px $buttonDark;
  &:hover {
    background: $buttonDark;
    box-shadow: 0px 5px 0px $buttonDarker;
  }
}

http://lesscss.org/ http://sass-lang.com/

... compiles to

button {
  background: #2ecc71;
  border-radius: 3px;
  box-shadow: 0px 5px 0px #25a25a;
}
button:hover {
  background: #25a25a;
  box-shadow: 0px 5px 0px #1b7943;
}
    

how to Layout our application?

  • table
  • positioned layout
  • float
  • block/inline(-block)
  • flex layout

Flexbox Structure

  • Container = flex container
  • Immediate children = flex items

Its all about grouping and aligning container, its flexible items and the space in between

Flex features

Responsive

Nestable

Items have equal height by default

Easy responsive layout

Flex container
properties

Flex Container

Flex Playground

display

Defines the flex container


    .container {
        display: flex; /* or inline-flex */
    }
                        

flex-direction

Direction of flex item appearance


    .container {
        flex-direction: row | row-reverse | column | column-reverse;
    }
                                

flex-wrap

Allow items to wrap inside container


    .container{
      flex-wrap: nowrap | wrap | wrap-reverse;
    }
                                

flex-flow

Usage of flex-direction and flex-wrap in one line


    .container {
        flex-flow: <‘flex-direction’> || <‘flex-wrap’>
    }
                                

justify-content

Defines alignment of items and usage of free space between them inside the container along the main axis


    .container {
      justify-content: flex-start | flex-end | center | space-between | space-around;
    }
                                

align-items

Defines alignment of container items along cross axis


    .container {
      align-items: flex-start | flex-end | center | baseline | stretch;
    }
                                

align-content

Aligns container lines (item rows) in the cross axis, affecting space between lines


    .container {
      align-content: flex-start | flex-end | center | space-between | space-around | stretch;
    }
                                

Flex items
properties

Flex Items

Flex Playground

order

Defines concrete order of special item inside a container


    .item {
      order: integer;
    }
                                

align-self

Overrides the cross axis alignment (align-items) for the individual item


    .item {
      align-self: auto | flex-start | flex-end | center | baseline | stretch;
    }
                                
flex-grow

Defines an abilty to grow in proportion to free space


    .item {
      flex-grow: number; /* default 0 */
    }
                                

Flex Grow Flex Grow
flex-shrink

Defines an abilty to shrink, if no space available


      flex-shrink: number; /* default 1 */
                                
flex-basis

Defines the size of the item


      flex-basis: length | auto; /* default auto */
                                

flex

Usage of flex-grow, flex-shrink, flex-basis in one line


    .item {
      flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
    }
                                

Can I use it?

Widely accepted: Can I use..Flexbox?

Flex Support

.. but not without Flexbugs

Exercise 3: Flex

Write Css code using Flex Model and reproduce.
Use fixed widths for sidebars.
(for small devices (width < 600px) rearrange sidebars to appear above/below content with full width):

HTML-file: <Repository>/css/exercises/exercise3.html