{"id":1109,"date":"2023-03-28T16:04:50","date_gmt":"2023-03-28T15:04:50","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=1109"},"modified":"2024-03-20T15:15:14","modified_gmt":"2024-03-20T15:15:14","slug":"responsive-css","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/authoring-a-website\/week-5\/responsive-css\/","title":{"rendered":"Responsive CSS"},"content":{"rendered":"\n<p>Last week you learned how to go through the stages of a project and saw an example from the initial client brief through to the delivered and tested website. <\/p>\n\n\n\n<p>While the client never mentioned that the website should work on a mobile phone in portrait mode it is an unspoken requirement. A website should be usable on any common web browsing device. More and more people use their phones to access websites so this should be kept in mind when you are designing one.<\/p>\n\n\n\n<p>With CSS you can use the @media rule that could be used to only apply certain CSS rules when a screen was above or below a certain width. You could also use it to detect the orientation of a phone so that you would know if it was being used in portrait or landscape mode.<\/p>\n\n\n\n<p>We will use these rules to make the garage website more responsive. But first we need to design what the site will look like at different screen widths. Will we just have the two different designs, one wide for desktops, one narrow for phones. Or will we have three or more that alter the layout as the screen gets wider.<\/p>\n\n\n\n<p>For this project lets keep it simple with just two designs. Pick a width, on screens more narrow than that the narrow content CSS loads, for those at or above that width the wide content CSS loads.<\/p>\n\n\n\n<p>Now look at your existing site and think about which parts would be hard to read or use on a portrait mobile device. A full width block of text or image is fine. They can still take up the full width although the text will now spread over more rows. Where you have side by side columns it is worth converting them into full width containers that stack vertically. This applies to menus too. If there are enough items in the menu that it might be difficult to select the right link when they are crushed together horizontally then it is worth converting to a vertical list.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"436\" height=\"722\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134156\/mobilehome.png\" alt=\"Example mobile home page\" class=\"wp-image-1117\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134156\/mobilehome.png 436w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134156\/mobilehome-181x300.png 181w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><figcaption class=\"wp-element-caption\">Example mobile home page<\/figcaption><\/figure><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"438\" height=\"723\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134215\/mobileservices.png\" alt=\"Example mobile services page\" class=\"wp-image-1118\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134215\/mobileservices.png 438w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134215\/mobileservices-182x300.png 182w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><figcaption class=\"wp-element-caption\">Example mobile services page<\/figcaption><\/figure><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"436\" height=\"723\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134233\/mobilecarlist.png\" alt=\"Example mobile car list page\" class=\"wp-image-1119\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134233\/mobilecarlist.png 436w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134233\/mobilecarlist-181x300.png 181w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><figcaption class=\"wp-element-caption\">Example mobile car list page<\/figcaption><\/figure><\/div><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"436\" height=\"723\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134300\/mobilecardetails.png\" alt=\"Example mobile car details page\" class=\"wp-image-1120\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134300\/mobilecardetails.png 436w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134300\/mobilecardetails-181x300.png 181w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><figcaption class=\"wp-element-caption\">Example mobile car details page<\/figcaption><\/figure><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"437\" height=\"727\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134315\/mobilecontact.png\" alt=\"Example mobile contact page\" class=\"wp-image-1121\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134315\/mobilecontact.png 437w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/30134315\/mobilecontact-180x300.png 180w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><figcaption class=\"wp-element-caption\">Example mobile contact page<\/figcaption><\/figure><\/div><\/div>\n<\/div>\n\n\n\n<p>You don&#8217;t need a new wireframe for the printed details page as that still comes out of a printer rather than displayed on a screen.<\/p>\n\n\n\n<p>If the container on the wide layout was using flex then you can just change the flex direction from rows to columns for the narrow layout and adjust the child elements to take up the full width of the flex container. <\/p>\n\n\n\n<p>If the container on the wide layout was using grid then you can change the number of columns in the grid to 1 for the narrow layout.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n  &lt;title&gt;Mobile Test&lt;\/title&gt;\n  &lt;style&gt;\n#grid {\n  background-color: cyan;\n  display: grid;\n  grid-template-columns: auto auto auto;\n}\n\n#flex {\n  background-color: pink;\n  display: flex;\n  flex-wrap: wrap;\n  flex-direction: row;\n}\n#flex div {\n  flex-basis: 33.33%;\n}\n@media screen and (max-width: 768px) {\n  #grid {\n    grid-template-columns: auto;\n  }\n\n  #flex {\n    flex-direction: column;\n  }\n  #flex div {\n    flex-basis: 100%;\n  }\n}\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;div id=\"grid\"&gt;\n    &lt;div&gt;1&lt;\/div&gt;\n    &lt;div&gt;2&lt;\/div&gt;\n    &lt;div&gt;3&lt;\/div&gt;\n  &lt;\/div&gt;\n  &lt;hr&gt;\n  &lt;div id=\"flex\"&gt;\n    &lt;div&gt;1&lt;\/div&gt;\n    &lt;div&gt;2&lt;\/div&gt;\n    &lt;div&gt;3&lt;\/div&gt;\n  &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>In the above example both the grid and flex containers have 3 child elements. On screens that are no more than 768px wide then the child divs are stacked vertically. Once the screen is at least 769px wide then the child divs are placed beside each other in a horizontal row.<\/p>\n\n\n\n<p>The @media rule is used in media queries to apply different styles for different media types\/devices. This allows for more responsive designs to be tailored to different formats.<\/p>\n\n\n\n<p>Media queries can be used to check many things, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>width and height of the viewport<\/li>\n\n\n\n<li>width and height of the device<\/li>\n\n\n\n<li>orientation (is the tablet\/phone in landscape or portrait mode?)<\/li>\n\n\n\n<li>resolution<\/li>\n<\/ul>\n\n\n\n<p>You can combine or exclude options with the&nbsp;<strong>and<\/strong>&nbsp;or&nbsp;<strong>not<\/strong>&nbsp;keywords.<\/p>\n\n\n\n<p>You can also apply styles based on media types. These types are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>all \u2013 The default. Used for all media type devices<\/li>\n\n\n\n<li>print \u2013 Used for printers<\/li>\n\n\n\n<li>screen \u2013 Used for computer screens, tables, smart phones etc<\/li>\n\n\n\n<li>speech \u2013 Used for screen readers<\/li>\n<\/ul>\n\n\n\n<p>The most common feature choices are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>height \u2013 The viewport height<\/li>\n\n\n\n<li>max-height \u2013 The maximum height of the display area<\/li>\n\n\n\n<li>min-height \u2013 The minimum height of the display area<\/li>\n\n\n\n<li>width \u2013 The viewport width<\/li>\n\n\n\n<li>max-width \u2013 The maximum width of the display area<\/li>\n\n\n\n<li>min-width \u2013 The minimum width of the display area<\/li>\n\n\n\n<li>orientation \u2013 The orientation of the viewport (landscape or portrait mode)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<p>Use the print type to change the styles for clearer printing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@media screen {\n  body {\n    color: greeen;\n  }\n}\n\n@media print {\n  body {\n    colour: black;\n  }\n}<\/code><\/pre>\n\n\n\n<p>Use width checks to make responsive designs: On screens that are 992px wide or less, go from four columns to two columns<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.column {\n  width: 25%;\n}\n@media screen and (max-width: 992px) {\n  .column {\n    width: 50%;\n  }\n}<\/code><\/pre>\n\n\n\n<p>On screens that are 600px wide or less, make the columns stack on top of each other instead of next to each other<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@media screen and (max-width: 600px) {\n  .column {\n    width: 100%;\n  }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<p>Now go and make mobile friendly designs for your websites and then alter the CSS to allow for the site to respond to changes in viewport width.<\/p>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/authoring-a-website\/\" data-type=\"page\" data-id=\"52\">End of the week<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week you learned how to go through the stages of a project and saw an example from the initial client brief through to the delivered and tested website. While the client never mentioned that the website should work on a mobile phone in portrait mode it is an unspoken requirement. A website should be&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/authoring-a-website\/week-5\/responsive-css\/\">Continue reading <span class=\"screen-reader-text\">Responsive CSS<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":62,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1109","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/1109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=1109"}],"version-history":[{"count":11,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/1109\/revisions"}],"predecessor-version":[{"id":1370,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/1109\/revisions\/1370"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/62"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=1109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}