Friday 4 October 2013

Responsive Web Design - Part2


Flexible Layouts

Responsive web design is broken down into three main components, including flexible layouts, media queries, and flexible media. The first part, flexible layouts, is the practice of building the layout of a website with a flexible grid, capable of dynamically resizing to any width. Flexible grids are built using relative length units, most commonly percentages or em units. These relative lengths are then used to declare common grid property values such as width,margin, or padding.

Relative Viewport Lengths

CSS3 introduced some new relative length units, specifically related to the viewport size of the browser or device. These new units include vwvhvmin, and vmax. Overall support for these new units isn’t great, but it is growing. In time they look to play a large roll in building responsive websites.
  • vwViewports width
  • vhViewports height
  • vminMinimum of the viewport’s height and width
  • vmaxMaximum of the viewport’s height and width
Flexible layouts do not advocate the use of fixed measurement units, such as pixels or inches. Reason being, the viewport height and width continually change from device to device. Website layouts need to adapt to this change and fixed values have too many constraints. Fortunately, Ethan pointed out an easy formula to help identify the proportions of a flexible layout using relative values.
The formula is based around taking the target width of an element and dividing it by the width of it’s parent element. The result is the relative width of the target element.
  1. target ÷ context = result

Flexible Grid

Let’s see how this formula works inside of a two column layout. Below we have a parent division with the class ofcontainer wrapping both the section and aside elements. The goal is to have have the section on the left and the aside on the right, with equal margins between the two. Normally the markup and styles for this layout would look a bit like the following.
HTML
  1. <div class="container">
  2. <section>...</section>
  3. <aside>...</aside>
  4. </div>
CSS
  1. .container {
  2. width: 660px;
  3. }
  4. section {
  5. float: left;
  6. margin: 10px;
  7. width: 420px;
  8. }
  9. aside {
  10. float: right;
  11. margin: 10px;
  12. width: 200px;
  13. }

Fixed Grid Demo

SECTION
ASIDE
Using the flexible grid formula we can take all of the fixed units of length and turn them into relative units. In this example we’ll use percentages but em units would work equally as well. Notice, no matter how wide the parentcontainer becomes, the section and aside margins and widths scale proportionally.
  1. .container {
  2. max-width: 660px;
  3. }
  4. section {
  5. float: left;
  6. margin: 1.51515151%; /* 10px ÷ 660px = .01515151 */
  7. width: 63.63636363%; /* 420px ÷ 660px = .63636363 */
  8. }
  9. aside {
  10. float: right;
  11. margin: 1.51515151%; /* 10px ÷ 660px = .01515151 */
  12. width: 30.30303030%; /* 200px ÷ 660px = .30303030 */
  13. }

Flexible Grid Demo

100% WIDE CONTAINER
SECTION
ASIDE
75% WIDE CONTAINER
SECTION
ASIDE
50% WIDE CONTAINER
SECTION
ASIDE


Taking the flexible layout concept, and formula, and reapplying it to all parts of a grid will create a completely dynamic website, scaling to every viewport size. For even more control within a flexible layout, you can also leverage the min-widthmax-widthmin-height, and max-height properties, as done with the parent container above.
The flexible layout approach alone isn’t enough. At times the width of a browser viewport may be so small that even scaling the the layout proportionally will create columns that are too small to effectively display content. Specifically, when the layout gets too small, or too large, text may become illegible and the layout may begin to break. In this event, media queries can be used to help build a better experience.