{"id":966,"date":"2012-08-22T18:31:36","date_gmt":"2012-08-22T18:31:36","guid":{"rendered":"http:\/\/a.parsons.edu\/~dejongo\/12-fall\/?p=966"},"modified":"2025-02-11T15:38:00","modified_gmt":"2025-02-11T15:38:00","slug":"04-responsive-design","status":"publish","type":"post","link":"http:\/\/b.parsons.edu\/~dejongo\/04-responsive-design\/","title":{"rendered":"04 Responsive Design"},"content":{"rendered":"<p><a href=\"https:\/\/www.smashingmagazine.com\/2011\/01\/guidelines-for-responsive-web-design\/\">Responsive design<\/a> started with the May 25, 2010 article <a href=\"http:\/\/www.alistapart.com\/articles\/responsive-web-design\">Responsive Web Design<\/a> by Ethan Marcotte discussing <a href=\"03-media-queries\/\">media queries<\/a>. Flexible layouts and responsive images are two additional components necessary for responsive web design to function effectively. Prior to the advent of responsive design, flexible layouts were achieved using em units.<\/p>\n<h2>EM as a basis for Web Design<\/h2>\n<\/p>\n<p>An <a href=\"http:\/\/en.wikipedia.org\/wiki\/Em_(typography)\">em<\/a> is a unit of measurement in typography, equal to the currently specified point size. It\u2019s believed to be derived from the width of the capital \u201cM\u201d in a specific typeface. Today, it represents the height of a font. This unit remains consistent across all fonts at a given point size. For instance, <code>1em<\/code> in a 16-point typeface corresponds to 16 pixels, which is the default font size for browsers.<\/p>\n<p>CSS allows one to measure everything in <code>em<\/code> units and some consider it a <a href=\"http:\/\/www.w3.org\/WAI\/GL\/css2em.htm\">best practice<\/a> to do so. (In case you are wondering, Bert Bos is the original author of CSS) This practice originated at a time when browsers only enlarged the fonts when zooming the page in or out, leaving all other elements the same size. To make the whole web page scale, the other elements had to be built with ems.<br \/>\nYou define the reference size of the text on the page in the body tag by setting the size of the <code>em<\/code>. When zooming in or out at the time when browsers only enlarged fonts, all elements specified in ems would change proportionally with the font, preserving the layout.<\/p>\n<h2>Flexible Layouts using EM<\/h2>\n<\/p>\n<p>To calculate a size in ems: <strong> divide the desired size by the reference em or rem<\/strong> \u2014 960px \/ 16px = 60em.\n<\/p>\n<p>Because percentages are often used to resize elements including fonts, it is possible to accidentally add a percentage on top of a percentage. When the font size of both a child and its parent are set at 80%, the child ends up being 80% of 80% of the <code>em<\/code>. This can lead to unexpected results. To avoid that, CSS3 introduced a <code>rem<\/code>, which stands for reference em. Unlike the <code>em<\/code>, the <code>rem<\/code> is <a href=\"06-text-formatting\/#em-font-size-rem\">not affected by inheritance<\/a> and reflects the percentage of the font as specified in the body tag.\n<\/p>\n<p>I don\u2019t recommend using the <code>em<\/code> as a relative measurement for anything other than specifying font size. The need for using it to create responsive or scalable websites has vanished, and I don\u2019t see any advantage to using the <code>em<\/code>, despite some people still advocating for its use.<\/p>\n<h2>Flexible Layouts using %<\/h2>\n<\/p>\n<p>Flexible websites resize automatically with the size of the window or the device, which is known as the viewport. To design elements effectively, use relative measurement units such as percentages. This ensures that all elements with relative sizes change proportionally when the viewport changes.<br \/>\nFiguring out the percentage isn\u2019t hard: <strong style=\u201ccolor: red;\u201d>target divided by context equals the result<\/strong>, where the target is the parent element and the context is the child element(s). This formula is used to calculate the correct percentages and is illustrated below in the creation of a fluid layout. The following example begins with a pixel-based layout, similar to a Photoshop comp, and transforms it into a percentage-based layout without using any absolute values.<\/p>\n<figure>\n<img decoding=\"async\" src=\"http:\/\/b.parsons.edu\/~dejongo\/12-fall\/stuff\/08-fluid-layout.gif\" alt=\"column diagram from Marcotte's Responsive Web Design book\"\/><figcaption><a href=\"http:\/\/b.parsons.edu\/~dejongo\/12-fall\/stuff\/ROBOT.html\">visit the example<\/a><\/figcaption><\/figure>\n<\/p>\n<p>The conventional standard width for web pages used to be 960px. That number can be evenly divided in many columns: by 2 (480), 3 (320), 4 (240), 5 (192), 6 (160), 8 (120), 10 (96), and 12 (80). This makes it easy to create columns and facilitate design.\n<\/p>\n<p>A 960 pixel wrapper div can be divided up into a grid of 12 columns, each measuring 68 pixels across and separated by regular 12px-wide gutters. Taken together, those columns and gutters are a total width of 960 pixels.\n<\/p>\n<p>If the main article were 6 grid columns and the side column were three grid columns with a column between them, that would fit in a container 900 pixel wide, leaving around a half column on either side.  Break this into pixel width and the main column width is 566p pixels while the side column is 331 pixels wide.\n<\/p>\n<p>To give the wrapper div a little padding on either side, instead of 960pixels, make it 90%. That is 90% of the viewport. That width is then used to define all other widths.\n<\/p>\n<p>Figuring out the percentage of a 900px container in a 960px parent, plug it into the formula <strong style=\"color: red;\">target  \u00f7 context = result<\/strong>, dividing 900 by 960 gives us 0.9375. Since 100% is equal to 1, .9375 is equal to 93.75%. That makes the container that holds the two columns 93.75% of the page size.\n<\/p>\n<p>The two columns fit inside the 900 pixel container. The width of the column is divided by 900. The large column is 62.8888889% and the small column is 36.7777778%. Don&#8217;t round the numbers off, as the browser uses the information to make the layout more accurate.\n<\/p>\n<p>All of the padding and margins need to be translated to percentages using the formula <strong style=\"color: red;\">target  \u00f7 context = result<\/strong>, dividing the child into its parent to get the right percentage.\n<\/p>\n<p>The layout will be problematic when the window becomes too small or too large. Use <code>min-width<\/code> and <code>max-width<\/code> to keep it from doing that. This is where media queries come in handy. <\/p>\n<h2>Flexible Images<\/h2>\n<\/p>\n<p>The img element by convention is a child of a figure element. Set the image width to 100% <code>img {width: 100%}<\/code>. The set the figure width to a percentage of the parent element to control the size of the image. <code>figure {width: 50%}<\/code>\n<\/p>\n<p>Background images can be sized in percentages, or they can be tiled to fill the background, or cropped.<\/p>\n<h2>Utopia  (2024)<\/h2>\n<\/p>\n<p> <a href=\"https:\/\/www.smashingmagazine.com\/2021\/04\/designing-developing-fluid-type-space-scales\/\">Responsive design<\/a> has been updated using <a href=\"https:\/\/utopia.fyi\">Utopia<\/a>, a comprehensive support system that simplifies the creation of fluid responsive designs. Utopia employs CSS frameworks to seamlessly achieve fluid design for  <a href=\"https:\/\/utopia.fyi\/type\/calculator\"> fonts <\/a>and <a href=\"https:\/\/utopia.fyi\/space\/calculator\/\"> spacing<\/a> across the range of smartphones and large screens. Unlike @media queries, Utopia eliminates the need for jumps or surprises, and it scales better than relying solely on viewport width. Additionally, Utopia offers tools for designing a <a href=\"https:\/\/utopia.fyi\/grid\/calculator\">layout grid<\/a>. While these tools may seem <a href=\"https:\/\/utopia.fyi\/blog\/designing-a-utopian-layout-grid\">complex at first<\/a>, they\u2019re worth checking out in a week or two as you begin coding and developing your website. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Responsive design started with the May 25, 2010 article Responsive Web Design by Ethan Marcotte discussing media queries. Flexible layouts and responsive images are two additional components necessary for responsive web design to function effectively. Prior to the advent of &hellip; <a href=\"http:\/\/b.parsons.edu\/~dejongo\/04-responsive-design\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts\/966"}],"collection":[{"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/comments?post=966"}],"version-history":[{"count":39,"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts\/966\/revisions"}],"predecessor-version":[{"id":6388,"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts\/966\/revisions\/6388"}],"wp:attachment":[{"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/media?parent=966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/categories?post=966"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/tags?post=966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}