{"id":5225,"date":"2013-04-29T07:00:36","date_gmt":"2013-04-29T07:00:36","guid":{"rendered":"http:\/\/www.adviceinteractivegroup.com\/?p=5225"},"modified":"2013-04-29T07:00:36","modified_gmt":"2013-04-29T07:00:36","slug":"going-off-canvas-for-mobile-is-easier-then-you-think","status":"publish","type":"post","link":"https:\/\/al.rocksdmdev.com\/?p=5225","title":{"rendered":"Going Off-Canvas for Mobile is Easier Than You Think"},"content":{"rendered":"<p>Disclaimer: This tutorial assumes you have intermediate knowledge of HTML5 and CSS3 and beginner knowledge of jQuery\/Javascript.<\/p>\n<p>In this tutorial we will be creating a basic design with a content area and a sidebar then using CSS3 for media queries to move the sidebar off-canvas for mobile. <a href=\"http:\/\/www.adviceinteractivegroup.com\/demo\/off-canvas\/\" target=\"_blank\" rel=\"noopener\">View the demo<\/a> source or download the files to follow along.<\/p>\n<h2>The HTML5<\/h2>\n<p>I won&#8217;t go into much detail here since we will be doing most of the work with CSS3. The demo has some basic containers &#8211; #header, #content, #sidebar, and #footer &#8211; as well as a button with an id of &#8220;toggle-sidebar&#8221;. There is also some dummy information you might see in the wild. The sidebar has buttons for navigate to different categories.<\/p>\n<h2>The Javascript<\/h2>\n<p>Most of the work is done in CSS. The jQuery simply toggles the class &#8220;sidebar-active&#8221; on the body when the #toggle-sidebar button is clicked<\/p>\n<h2>The CSS<\/h2>\n<h4>Desktop (Default)<\/h4>\n<p>Nothing fancy here. With padding the content is 80% wide (floated left) and the sidebar is 20% wide (floated right). The CSS3 transitions are used for animation later on. If you aren&#8217;t familiar with CSS3 transitions <a href=\"http:\/\/www.css3.info\/preview\/css3-transitions\/\" target=\"_blank\" rel=\"noopener\">css3.info<\/a> has a good explaination. The #toggle-sidebar button has a value of &#8220;display:none;&#8221; since we don&#8217;t want it visible for desktops. Lastly, the #content and #sidebar divs are inside a larger #wrap div that has &#8220;overflow:hidden;&#8221;. This is to prevent scroll bars that come with the default overflow auto.<\/p>\n<h4>Tablet<\/h4>\n<p>We will be using a media query with min-width of 501px and max-width of 1024px for tablet styles. This means our desktop view will show for screens larger then 1024px or browsers that don&#8217;t support media queries &#8211; like older versions of Internet Explorer. If you need some more info on CSS3 media queries there is a really good article on <a href=\"https:\/\/css-tricks.com\/css-media-queries\/\">css-tricks.com<\/a>.<\/p>\n<p>Inside our tablet media query we have a few important items.<\/p>\n<ul>\n<li>A lot of the default css is still in tact for the #content and #sidebar &#8211; like floats and CSS3 transitions<\/li>\n<li>Our #toggle-sidebar button has &#8220;display:inline-block;&#8221; so that it is visible on tablet<\/li>\n<li>The #content (with padding) has 100% width.<\/li>\n<li>The #sidebar (with padding) has a width of 70%<\/li>\n<li>The #sidebar has a right margin of 100%. This is what takes it off-canvas, rather than just breaking below the #content.<\/li>\n<li>.sidebar-active #content has a left margin of -70%. When the sidebar is active the content will move off-canvas using negative left-margin. The 70% is to match the #sidebar width. This will leave part of the content visible on tablets for a nice effect.<\/li>\n<li>.sidebar-active #sidebar has a right margin of 0 when the sidebar is active removing the negative right-margin will bring it back into view.<\/li>\n<\/ul>\n<h4>Mobile<\/h4>\n<p>We will be using a media query with max-width of 500px for mobile styles since the tablet styles start at 501px.<\/p>\n<p>Inside our mobile media query we are doing almost the same thing as tablet.<\/p>\n<ul>\n<li>Our #toggle-sidebar button is visible<\/li>\n<li>The #content and #sidebar (with padding) have 100% width since there isn&#8217;t a lot of room to work with on mobile.<\/li>\n<li>.sidebar-active #content has a left margin of -100% to match the sidebar width.<\/li>\n<li>.sidebar-active #sidebar has a right margin of 0, just like the tablet design<\/li>\n<\/ul>\n<h4>Extras<\/h4>\n<p>There is some button styling with CSS3 and a few other things in the demo that weren&#8217;t covered in this tutorial since they aren&#8217;t required for the functionality. If you haven&#8217;t looked at all the new elements CSS3 has to offer I highly suggest checking them out<\/p>\n<h2>Conclusion<\/h2>\n<p>We were able to easily create an off-canvas sidebar for tablet\/mobile with a minimal amount of code. There are CSS grid frameworks and other responsive packages out there &#8211; which are great for more complex projects &#8211; but hopefully you now have a better understanding of how off-canvas works and can use it in your next project.<\/p>\n<p>Having trouble or have a question? Leave a comment and let me know<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Disclaimer: This tutorial assumes you have intermediate knowledge of HTML5 and CSS3 and beginner knowledge of jQuery\/Javascript. In this tutorial [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[17],"tags":[],"class_list":["post-5225","post","type-post","status-publish","format-standard","hentry","category-web-development-design"],"_links":{"self":[{"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/posts\/5225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5225"}],"version-history":[{"count":0,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=\/wp\/v2\/posts\/5225\/revisions"}],"wp:attachment":[{"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/al.rocksdmdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}