{"id":313,"date":"2018-02-15T14:57:04","date_gmt":"2018-02-15T14:57:04","guid":{"rendered":"http:\/\/www.simon-li.com\/design-and-code\/?p=313"},"modified":"2021-12-21T16:49:09","modified_gmt":"2021-12-21T16:49:09","slug":"an-elegant-way-to-break-the-bootstrap-container","status":"publish","type":"post","link":"http:\/\/www.simon-li.com\/design-and-code\/an-elegant-way-to-break-the-bootstrap-container\/","title":{"rendered":"An Elegant Way to Break the Bootstrap Container"},"content":{"rendered":"<p>Carousels are a popular UI element. Unlike other elements that are often constrained by the main view, they often occupy the entire width of a page (the viewport), as can be seen from the following examples.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-314\" src=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_1.jpg\" alt=\"\" width=\"648\" height=\"328\" srcset=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_1.jpg 648w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_1-300x152.jpg 300w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><br \/>\n<!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-315\" src=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_2.jpg\" alt=\"\" width=\"360\" height=\"589\" srcset=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_2.jpg 360w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_2-183x300.jpg 183w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/p>\n<figure id=\"attachment_316\" aria-describedby=\"caption-attachment-316\" style=\"width: 577px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-316\" src=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_3-577x1024.jpg\" alt=\"\" width=\"577\" height=\"1024\" srcset=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_3-577x1024.jpg 577w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_3-169x300.jpg 169w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/rail_3.jpg 640w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><figcaption id=\"caption-attachment-316\" class=\"wp-caption-text\">Examples of carousels occupying the entire width of a page.<\/figcaption><\/figure>\n<p>Bootstrap is a popular responsive front-end framework for designing websites. Its 12-column grid system, which consists of <a href=\"https:\/\/getbootstrap.com\/docs\/4.0\/layout\/overview\/#containers\" target=\"_blank\" rel=\"noopener noreferrer\">containers<\/a>, rows and columns, provides a convenient structure for creating responsive webpages and has been widely used among web developers. Containers in Bootstrap center the contents of a page and add padding to both sides, thereby creating the main view of the page. However, it also prevents any content inside from extending beyond the padding. We want to put carousels inside the container for better organization and code structure, just like other UI elements. At the same time, we also want it to use up all the width of a page. In other words, is it possible for the carousel to break the container?<\/p>\n<p>There is actually an elegant method to do this, with the use of the CSS unit <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/length#vw\" target=\"_blank\" rel=\"noopener noreferrer\"><em>vw<\/em><\/a> and the function <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/calc\" target=\"_blank\" rel=\"noopener noreferrer\"><em>calc()<\/em><\/a>, and it works across all breakpoints for all devices. Play around with the following codepen <a href=\"https:\/\/codepen.io\/simonli\/pen\/qppeER\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p><a href=\"https:\/\/codepen.io\/simonli\/pen\/qppeER\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-317\" src=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-15-at-10.37.52-PM.png\" alt=\"\" width=\"1310\" height=\"744\" srcset=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-15-at-10.37.52-PM.png 1310w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-15-at-10.37.52-PM-300x170.png 300w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-15-at-10.37.52-PM-768x436.png 768w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-15-at-10.37.52-PM-1024x582.png 1024w\" sizes=\"auto, (max-width: 1310px) 100vw, 1310px\" \/><\/a><\/p>\n<p>The red boxes can be seen as the regular contents placed inside the container, and the green box is breaking the container and extends to both the left and right sides of the page.<\/p>\n<p>Note that in the above code, the <em>div<\/em> tag with the break-out class has a width of <em>100vw<\/em>. Since <em>vw<\/em> represents 1% of the viewport width, <em>100vw<\/em> is the entire width of the viewport, which is the width of the browser window. Additionally, we need to move the <em>div<\/em> tag to the left, that\u2019s why we set its <em>position<\/em> property to <em>relative<\/em> to make it move relative to its parent, which is the container. We also set its <em>left<\/em> property to <em>calc(-1 * (100vw &#8211; 100%) \/ 2)<\/em>, where <em>100%<\/em> is the width of the container, to make sure that there\u2019s no gap between the carousel and the viewport.<\/p>\n<p>This approaches works for any responsive breakpoints. With a few lines of CSS, we are breaking the bootstrap container in an clean and elegant way.<\/p>\n<p>Finally, <em>vw<\/em> and <em>calc()<\/em> are widely supported by browsers nowadays, as is shown in <a href=\"https:\/\/caniuse.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Can I use<\/a>, so it\u2019s pretty safe to use them without worrying about browser support.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-318\" src=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-11.png\" alt=\"\" width=\"1296\" height=\"1264\" srcset=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-11.png 1296w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-11-300x293.png 300w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-11-768x749.png 768w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-11-1024x999.png 1024w\" sizes=\"auto, (max-width: 1296px) 100vw, 1296px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-319\" src=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-47.png\" alt=\"\" width=\"1296\" height=\"1280\" srcset=\"http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-47.png 1296w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-47-300x296.png 300w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-47-768x759.png 768w, http:\/\/www.simon-li.com\/design-and-code\/wp-content\/uploads\/2018\/02\/screenshot-caniuse.com-2018.02.15-21-37-47-1024x1011.png 1024w\" sizes=\"auto, (max-width: 1296px) 100vw, 1296px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Carousels are a popular UI element. Unlike other elements that are often constrained by the main view, they often occupy the entire width of a page (the viewport), as can be seen from the following examples.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[50,52,76],"class_list":["post-313","post","type-post","status-publish","format-standard","hentry","category-coding","tag-bootstrap","tag-css","tag-front-end"],"_links":{"self":[{"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":21,"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"predecessor-version":[{"id":967,"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/posts\/313\/revisions\/967"}],"wp:attachment":[{"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.simon-li.com\/design-and-code\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}