Friday, October 4, 2013

Viewport meta tag explained with examples

First task when designing web applications for mobile is to set viewport meta tag to tell mobile browser how to initially display your web page.

Very useful readings are A tale of two viewports — part oneA tale of two viewports — part twodevicePixelRatioMore about devicePixelRatio, Using the viewport meta tag to control layout on mobile browsers (MDN) and Configuring the Viewport for iOS which explain how to set and use this tag correctly. To completely understand effect of this tag I decided to test few different variations and compare results.


Measured properties:


screen.width/height - contain the total width and height of the user's screen and are measured in device pixels; they never change. Nexus 4 display resolution is 768 x 1280 but as we can see screen.width x height are 384 x 592.


window.innerWidth/Height - tells how much space the user currently has available for CSS layout and is measured in CSS pixels.

On mobile devices visual viewport is measured by window.innerWidth/Height. Obviously the measurements change when the user zooms out or in.

document.documentElement.clientWidth/Height - dimensions of the viewport, regardless of the dimensions of the <html> element (viewport is one level higher than <html> element). There's a formal difference between the two property pairs: document.documentElement.clientWidth/Height doesn’t include the scrollbar.

On mobile devices those properties contain the layout viewport’s dimensions. How wide is default layout viewport? That differs per browser. Safari iPhone uses 980px, Opera 850px, Android WebKit 800px, and IE 974px.

document. documentElement. offsetWidth/Height - tells dimensions of the <html> element itself, measured in CSS pixels.

On mobile devices those properties have the same meaning, total size of <html> element.

Examples


Testing device used was Nexus 4 with Chrome 30. Screen resolution of nexus 4 is 768x1280. Its higher than iPhone 4s (640x960) and even higher than iPhone 5x (640x1136).


no viewport tag:
screen 384 / 592
inner 980 / 1325
client 980 / 1324
offset 980 / 3208
dpr 2




<meta name='viewport' content='initial-scale=1.0, user-scalable=no'>
screen 384 / 592
inner 980 / 1325
client 384 / 519
offset 384 / 2830
dpr 2





<meta name="viewport" content="width=device-width">
screen 384 / 592
inner 384 / 519
client 384 / 519
offset 384 / 2830
dpr 2




<meta name="viewport" content="initial-scale=1.0">
screen 384 / 592
inner 598 / 311
client 384 / 519
offset 384 / 2830
dpr 2
landscape:
screen 598 / 384
inner 598 / 311
client 598 / 311
offset 598 / 2117
dpr 2




<meta name="viewport" content="initial-scale=2.0">
screen 384 / 592
inner 192 / 260
client 192 / 259
offset 192 / 7703
dpr 2




<meta name="viewport" content="width=590">
screen 384 / 592
inner 590 / 798
client 590 / 797
offset 590 / 2617
dpr 2





Problems:
position: fixed. A fixed element, as we know, is positioned relative to the viewport. But relative to which viewport...layout or visual?

Wednesday, October 2, 2013

Interesting resources (October 2013)

List of links to interesting resources

Google I/O 2013 - Enchant, Simplify, Amaze: Android's Design Principles
http://www.youtube.com/watch?v=s0HIP8EdlnE&list=WL4856A6E16934A8D0

Android Design Principles
http://developer.android.com/design/get-started/principles.html

Chrome DevTools
https://developers.google.com/chrome-developer-tools/

Google I/O 2013 - Point, Click, Tap, Touch - Building Multi-Device Web Interfaces
http://www.youtube.com/watch?v=DujfpXOKUp8

Git Reference (all read except Branching and Merging and Inspection and Comparison)
http://gitref.org/basic/

A More Awesome Web: Features You've Always Wanted - Google I/O 2013
http://www.youtube.com/watch?v=N_wTBKMuJis

Chrome DevTools Revolutions 2013
http://www.html5rocks.com/en/tutorials/developertools/revolutions2013/

Remote Debugging on Android
https://developers.google.com/chrome-developer-tools/docs/remote-debugging

Thursday, September 26, 2013

LazyJS, loading javascript asynchronously with execution order

By reading book High Performance JavaScript I discovered very useful library LazyLoad created by Ryan Grove of Yahoo! Search. LazyLoad is capable of asynchronously loading javascript and css files and executing them in correct order. In development of our projects we require only loading JavaScript files asynchronously and that's why we created modified version called LazyJS which is significantly smaller (1232 Bytes minified js and 2331 Bytes minified js with copyright notice) and require less memory allocation. When inlined into page and gzipped with other content its almost unnoticeable.
LazyJS minified code on GitHub

Example of use:
LazyJS.js(["/s/jquery-2.0.3.min.js","/s/jquery.Jcrop.min.js","/s/js.js"], null);


UPDATE:
Recently I discovered another option which is described in following blog post
Loading your javascript files asynchronously with script inspired by google analytics. It is worth to give it a try.

Saturday, June 8, 2013

Take page speed to extreme

Aside of optimizing your web application according to following articles, I will try to write list of few other hints which you can use to save bytes in html responses.


1. To define charset use: <meta charset='utf-8'> instead of <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

2. When you are using html5 doctype, you should omit type attribute in link tag for stylesheet: <link rel='stylesheet' href='style.css'> (instead of <link rel='stylesheet' type='text/css' href='style.css'>).

3. In case of using webfonts for icons on your page use: <i class='i-comment'></i> instead <span class='i-comment'></span>

4. In case of dynamically serving content (eg. images) implement server handler to consume path instead of parameters:
www.yourdomain.com/i?i=someimageidentifier
www.yourdomein.com/i/someimageidentifier

5. Use overflow: auto/hidden to clear floats (as explained on QuirksMode) instead using additional markup with clear property set.

6. Use relative path in stylesheet for urls (eg. bg.png) instead of absolute paths (/style/bg.png). Relative paths are always shorter and they allows you to test without using local server.

Not to do:

One of the things not to do is to use p tag instead of div. The reason is explained on Sitepoints html tag reference

Some developers perceive similarities between the p and the div elements, seeing them as being interchangeable, but this isn't the case. The p element offers more semantic information (“this is a paragraph of text, a small collection of thoughts that are grouped together; the next paragraph outlines some different thoughts”), while the div element can be used to group almost any elements together. Indeed, it can contain almost any other element, unlike p, which can only contain inline elements.

Also have in mind those limitations on placing only inline elements into following tags:

Heading elements h1..h6 may contain any text content, but they can't include any block-level elements: only inline or phrase elements can be included.

blockquote element is used to mark up one or more paragraphs, which may themselves contain other inline elements (for example, strong, em or a elements).


This article is written by developers of itumb.com.

Sunday, June 2, 2013

CSS reset, how complex it should be? (Eric Meyer’s Reset explained)

One of the first things to consider when creating web page is to include CSS reset to override default user agent's style rules. According to css reset</>, most popular reset at current time is Eric Meyer’s “Reset CSS” 2.0. With good references and experience with first version of Eric Meyer’s reset I decided to stick with it. (Eric Mayer is author of CSS: The Definitive Guide and other popular CSS books, you can find his blog here.)

To improve performance (lower download speed and complexity of style sheet) I ended up with slightly (ok maybe a little bit more) modified version which was created by applying following rules.

1. Remove all unnecessary tag selectors from first part.

In my case first part was changed from original

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}

to very shortened list of tags (your version should vary according to tags used in your project):

html, body, div, span, h1, h2, p, a, img, ul, li, form, label { margin: 0; padding: 0; border: 0;}

You can see, that I also removed three css properties and I will try to explain why.
I was searching for the explanation of font-size: 100%; font: inherit; and found few lines about them here:
Added font: inherit to the declaration block of the first rule. There are still older versions of IE that don’t understand inherit, but support is now widespread enough that I feel this can go in.  I left font-size: 100% as a sop to older browsers, and override it with the next declaration in those browsers that understand.
It did not tell me the reason why they are necessary but at least indicated font-size: 100% is there to support older browsers (not specified which ones). I searched more and found CSS: 100% font size - 100% of what? which explains it is used to indicate that font size from browser preferences should be used. Also there is note that next declaration override it and if you are not paranoid about older browsers you can also remove it without worry.

Purpose of font: inherit; is explained in What is the purpose of using {font:inherit;}?. Simply you can tell yourself why it is there when font properties are inherited by default. They are, but user agent's style sheet have tag selectors for eg. h1..h6 or a tags and those take precedence because they are defined closer than what you specify for any parent element. To reset font properties you need to specify that it take inherited value. It is questionable to include this property because it will simply erase all basic font styles, sizes, colors etc. I don't think it is necessary, next image illustrate what is the difference (left side shows font: inherit property in place):


Main reason which lead me to remove this property was extensive hierarchy showed in chrome developer tools.

When property font: inherit (and font-size: 100%) ware in place:


When property font: inherit (and font-size: 100%) ware removed:


For now I also removed vertical-align: baseline property. My tests shows there was no change when this property was in place. Also there are many indications that it caused problems with image alignment and alignment in table cells.

2. Depending on your usage of specific HTML5 tags you should decide if next section is necessary for you.

For now I don't use HTML5 tags to semantically divide content because tags does not include many scenarios for which web applications are created and I think it was not a step in the right direction and semantics should be described completely another way than is presented in HTML5.

In my case I removed following part completely:

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
display: block;
}

3. Next is rule for body element.

body {
line-height: 1;
}

After reading a little bit more about it (eg. jQuery/CSS: line-height of “normal” == ?px) it clearly explains that this fix is necessary because default value of line-height property is "normal" which is inconsistent in different browsers and font faces. As mentioned previously I also include default font face, font size and color in this declaration and they should be changed according to your project's needs:

body {
font-family: Arial, sans-serif;
font-size: 12px;
color: #2A2A2A;
line-height: 1;
}

4. Rules at the end specify defaults for lists (ordered, unordered), blockquote, q, table and should be used when necessary.

/* optional */
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}

Applying all those rules for my project produced simple and easy to understand code:

html, body, div, span, h1, h2, p, a, img, ul, li, form, label {
margin: 0;
padding: 0;
border: 0;
}

body {
font: 12px/1 Arial, sans-serif;
}

ul {
list-style: none;
}


This article is written by developers of itumb.com.