10/30/2018

Exercise 21

Viewport Meta Tag

CSS Media Queries

Media queries allow us to apply different CSS rules based on environment variables.

They can be though of as if statements in CSS.

Types of Media Queries

  • all - All media types
  • print
  • screen - for color devices

Media Query Tests

  • width - (min & max)
  • height - (min & max)
  • orientation - (portrait or landscape)
  • aspect-ratio
  • resolution - dpi

Applying Media Queries

Including via style sheet


        
        
        
        

Applying Media Queries

Include via CSS


@media screen{
    body {
        color: red;
    }
}
@media print{
    body {
        color: black;
    }
}
        

Applying Media Queries

Include via CSS


@media screen and (min-width:320px) and (max-width:600px){
    /*rules apply to screen widths 320px to 600px;*/
    p{
        color:red;
    }
    div{
        float:right;
    }
}
@media screen and (max-width:320px){
    /*rules apply to screen widths below 320px*/
}
        

Questions?

Responsive Layout Types

There are two primary ways to handle responsive layouts.

Desktop First

Mobile First

Desktop First

Initial popular way to handle responsive.

  • Traditional method in the past to implement a responsive website.
  • Better for complex websites.
  • Must dumb down features for mobile users.
  • Challenge to adapt
  • Heavy load on mobile users

Desktop First CSS Example


    /* Global Default Style */
    body{
        font-size:16px;
    }
    @media screen and (max-width:960px) {
        /* Large Screen Style */
        body{
            font-size:14px;
        }
    }        

           
    @media screen and (max-width:620px) {
        /* Medium Screen Style */
        body{
            font-size:12px;
        }
    }
     @media screen and (max-width:480px) {
        /* Small Screen Style */
       body{
            font-size:10px;
        }
    }
            

Mobile First

Takes the simplistic approach of targeting the smallest screens first.

  • Focus on content over functionality.
  • Can be challenging for clients due to prioritizing content.
  • Has become the preferred approach for most websites.
  • Expected to drive even more simplistic desktop sites in the future..

Responsive Showcase

Mobile First CSS


    /* Global Default Style */
    body{
        font-size:16px;
    }

    @media screen and (min-width:480px) {
        /* Small Screen Style */
       body{
            font-size:14px;
        }
    }
            
    @media screen and (min-width:620px) {
        /* Medium Screen Style */
        body{
            font-size:12px;
        }
    }
            
    @media screen and (min-width:960px) {
        /* Large Screen Style */
        body{
            font-size:10px;
        }
    }        

Font Libraries

You don't have to settle with the default fonts that are on your machine for web pages. Adding a custom font to your website is very easy process to do.

First checkout what you can already use.

CssFontStack.com

Step 1

Find a font you have permission to use your website and include the files in your project.

Step 2 - Declare a font-face in your css


@font-face {
    font-family: my-font-name;
    src: url(path/to/font.eof),
        url(path/to/font.woff),
        url(path/to/font.woff2)

}
    

Step 3 - Done

You can use your font immediately after declaring the font-family in your CSS


        .css{
            /*Make sure you include a fallback */
            font-family:my-font-name, Arial, sans-serif;
        }
        

Custom Font Drawbacks

There is always a cost doing something fancy on the web. Custom fonts are the same way.

  • Slows down page load
  • Flash of default font type or no font at all.
  • Inconsistent experiences in different browsers.

Self Hosting Alternatives

These are some alternatives to using a self hosted font type. These are traditionally more optimized for the web world.

Google Font Installation


    
        
    .my-div{
        font-family: 'Roboto', sans-serif;
    }

Icon Font Libraries

Icon Font Libraries are an incredible way to speed up your development time and spend less time cutting out small images.

What is an Icon Font Library

It takes a set of Scalable Vector Graphic icons and compresses them into a font file for you to use.

  • Chevron
  • Bars
  • Facebook
  • Cog
  • Flag

Advantages

  • Fewer web requests
  • Easy to use
  • Scalable
  • CSS manipulation

Disadvantages

  • 1 heavy web request
  • Many icons, but may only need 4 or 5
  • Icons may not match branding

Popular Icon Libraries

Make your own

IcoMoon

Using FontAwesome


       
    

        
        
        
        
        
        
     
    
More Examples

Exercise 21

>