{"id":903,"date":"2015-03-09T14:58:56","date_gmt":"2015-03-09T13:58:56","guid":{"rendered":"http:\/\/blogs.igalia.com\/jfernandez\/?p=903"},"modified":"2015-06-02T10:46:33","modified_gmt":"2015-06-02T08:46:33","slug":"content-distribution-in-css-grid-layout","status":"publish","type":"post","link":"https:\/\/blogs.igalia.com\/jfernandez\/2015\/03\/09\/content-distribution-in-css-grid-layout\/","title":{"rendered":"Content Distribution in CSS Grid Layout"},"content":{"rendered":"<p>It&#8217;s been a while since <a href=\"http:\/\/www.igalia.com\" target=\"_blank\">Igalia<\/a> and <a href=\"http:\/\/www.bloomberg.com\/company\/\" target=\"_blank\">Bloomberg<\/a> started to implement the <a href=\"http:\/\/dev.w3.org\/csswg\/css-align\/\" target=\"_blank\">Box Alignment<\/a> specification for the <a href=\"http:\/\/dev.w3.org\/csswg\/css-grid-1\/\" target=\"_blank\">CSS Grid Layout<\/a> model. Some weeks ago we accomplished an important milestone of our roadmap landing in Blink trunk the last patches implementing the Content Distribution properties: <strong>align-content<\/strong> and <strong>justify-content<\/strong>.<\/p>\n<p>Quoting the CSS Box Alignment document, the content distribution properties are defined as follows:<\/p>\n<blockquote><p>Aligns the contents of the box as a whole along the box\u2019s inline\/row\/main axis.<br \/>\nThe <a href=\"http:\/\/dev.w3.org\/csswg\/css-align\/#alignment-container\" target=\"_blank\">alignment container<\/a> is the grid container\u2019s content box. The <a href=\"http:\/\/dev.w3.org\/csswg\/css-align\/#alignment-subject\" target=\"_blank\">alignment subjects<\/a> are the grid tracks.<\/p><\/blockquote>\n<p>The CSS syntax of these recently added properties gives an idea of how powerful and flexible they are for grid layout definitions, allowing every possible alignment values combination:<\/p>\n<blockquote><p>auto | &lt;<a href=\"http:\/\/dev.w3.org\/csswg\/css-align\/#typedef-baseline-position\" target=\"_blank\">baseline-position<\/a>&gt; | &lt;<a href=\"http:\/\/dev.w3.org\/csswg\/css-align\/#typedef-content-distribution\" target=\"_blank\">content-distribution<\/a>&gt; || [ &lt;<a href=\"http:\/\/dev.w3.org\/csswg\/css-align\/#typedef-overflow-position\" target=\"_blank\">overflow-position<\/a>&gt;? &amp;&amp; &lt;<a href=\"http:\/\/dev.w3.org\/csswg\/css-align\/#typedef-content-position\" target=\"_blank\">content-position<\/a>&gt; ]<\/p><\/blockquote>\n<p>It&#8217;s worth mentioning that <a href=\"http:\/\/dev.w3.org\/csswg\/css-align\/#baseline-align-content\" target=\"_blank\">Baseline Alignment<\/a> is still not implemented, as well as the <em>&lt;content-distribution&gt;<\/em> values for Distribution Alignment. However, in the latter case I&#8217;ve got already a quite promising draft implementation which, eventually, has been very useful to activate a <a href=\"http:\/\/lists.w3.org\/Archives\/Public\/www-style\/2014Nov\/0070.html\" target=\"_blank\">discussion<\/a> inside the W3C community to allow these alignment values for grid. In previous versions of the specification it was stated that all <em>&lt;content-distribution&gt;<\/em> values should use their <em>&lt;content-position&gt;<\/em> fallback values for grid containers. I&#8217;m glad that such decision was finally made, because I think that<em> &lt;content-distribution&gt;<\/em> values are really useful for defining fancier grid layouts. I&#8217;ll talk about this soon in a new post, as I consider it deserves a detailed description with the proper examples.<\/p>\n<p>Last, but not least, as it happens with Self Alignment it allows using <strong>overflow keywords<\/strong> to define how we want to handle grid&#8217;s content overflow. It works in the same way for Content Distribution as we&#8217;ll see later with some examples.<\/p>\n<h3>Aligning the grid<\/h3>\n<p>When there is available space in the grid container block, it&#8217;s always useful to have a way to control how we want to use such space and how we want our grid to behave on it. It might happen that container&#8217;s size changes (fullscreen mode) or we could have to deal with a content sized grid modifying its content&#8217;s size. There are quite many possibilities, so I&#8217;ll leave this issue for user\/designer&#8217;s imagination and I&#8217;ll focus on very simple examples to illustrate the concept.<\/p>\n<p>For now, let&#8217;s consider this case to understand what you can do with the different <em>&lt;content-alignment&gt;<\/em> values in a grid layout.<\/p>\n<pre lang=\"javascript\">.grid {\r\n    grid: 50px 50px \/ 100px 100px;\r\n    position: relative;\r\n    width: 200px;\r\n    height: 300px;\r\n}\r\n.fixedSize {\r\n    width: 20px;\r\n    height: 40px;\r\n}\r\n<div class=\"grid\">\r\n   <div class=\"fixedSize\" style=\"grid-column: 1; grid-row: 1; background: violet;\"><\/div>\r\n   <div style=\"grid-column: 1; grid-row: 2; background: yellow;\"><\/div>\r\n   <div style=\"grid-column: 2; grid-row: 1; background: green;\"><\/div>\r\n   <div class=\"fixedSize\" style=\"grid-column: 2; grid-row: 2; background: red;\"><\/div>\r\n<\/div>\r\n<\/pre>\n<p>We are defining a <em>2&#215;2 grid<\/em> with <em>50&#215;100 pixels<\/em> cells where we are going to place <em>4 items<\/em>, one in each cell. Notice that items at (1,1) and (2,2) have a <strong>fixed size<\/strong> of <em>20&#215;40 pixels<\/em>, while the other 2 are <em>auto-sized<\/em> so they will be <strong>stretched<\/strong> to fill their corresponding grid cell (if you don&#8217;t know why, a reading of previous <a href=\"http:\/\/blogs.igalia.com\/jfernandez\/2015\/01\/12\/box-alignment-and-grid-layout-ii\/\">post<\/a> might help). Also, bear in mind that both <em>align-content<\/em> and <em>justify-content<\/em> properties have <em>start<\/em> as the initial value for grids.<\/p>\n<p><a href=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2015\/03\/ContentAlignment.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-926\" src=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2015\/03\/ContentAlignment.gif\" alt=\"ContentAlignment\" width=\"376\" height=\"494\" \/><\/a><\/p>\n<h3>Controlling the grid overflow<\/h3>\n<p>When grid content&#8217;s size exceeds its container dimensions there is the risk of <strong>data loss<\/strong>. Some examples of this scenario are <em>center<\/em> or <em>end<\/em> alignment from the viewport&#8217;s edges; all the content overflowing the viewport&#8217;s area can&#8217;t be reached, hence we lose such data. In order to prevent this issue Box Alignment specification defines the <strong>safe overflow<\/strong> mode, which basically forces a <em>start<\/em> alignment value for the property handling the dimension where the overflow is detected.<\/p>\n<p>Using the same <em>CSS<\/em> and <em>HTML<\/em> code in the example above, we can easily define cases where this data loss issue (red colored arrows) is clearly noticeable just modifying the height or width to cause top or left overflow respectively.<\/p>\n<p><a href=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2015\/03\/Content-Alignment-Overflow1.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-984\" src=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2015\/03\/Content-Alignment-Overflow1.gif\" alt=\"Content-Alignment-Overflow1\" width=\"465\" height=\"531\" \/><\/a><\/p>\n<p>There are other situations where Content Alignment and Overflow interact in a different way, using margins, padding or\/and borders and even combining different writing-modes and flow directions. The effect of the alignment values varies considerably depending on those factors but I think you have now a clear idea of how to use these new properties in a grid layout.<\/p>\n<h3>Current status and next steps<\/h3>\n<p>With the grid support for the <em>align-content<\/em> and <em>justify-content<\/em> CSS properties in Blink we&#8217;ve got most of the Box Alignment specification covered. As it was commented before, just Base Alignment is still pending to be implemented in Chromium browsers. I have to admit that there are also some bugs and wrong behavior using certain CSS combinations, specially regarding orthogonal flows, but we are working on it right now and I hope to integrate the patches soon in trunk.<\/p>\n<p>For the time being, let&#8217;s consider the following table as the <strong>current implementation status<\/strong> of the Box Alignment specification for the Grid Layout model in WebKit (Safari\/Epiphany) and Blink (Chrome\/Chromium\/Opera) based browsers:<\/p>\n<p><a href=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2015\/03\/align-grid-support-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-955\" src=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2015\/03\/align-grid-support-1.png\" alt=\"align-grid-support-1\" width=\"666\" height=\"389\" srcset=\"https:\/\/blogs.igalia.com\/jfernandez\/files\/2015\/03\/align-grid-support-1.png 666w, https:\/\/blogs.igalia.com\/jfernandez\/files\/2015\/03\/align-grid-support-1-300x175.png 300w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/a><\/p>\n<p>The <strong>lack of progress<\/strong> in the implementation of the Box Alignment specification in the WebKit web engine is disappointing. I&#8217;ve been stuck for quite a lot of time trying to upgrade the CSS properties to the last version of the spec, mainly due design and performance issues. I&#8217;ll discuss with the WebKit hackers the best approach to solve this issue so I can put the Grid Layout implementation at the same level than in Blink web engine.<\/p>\n<p>Igalia and Bloomberg will continue working on the implementation of the CSS Grid Layout specification and among my short\/mid term challenges are completing the Box Alignment support. These goals include the following tasks:<\/p>\n<ul>\n<li>Fixing bugs and completing the orthogonal flows support.<\/li>\n<li>Implementing the Base Alignment features<\/li>\n<li>Completing the Content Distribution Alignment with the &lt;content-distribution&gt; values<\/li>\n<li>Implementing the Box Alignment spec in WebKit<\/li>\n<\/ul>\n<div id=\"attachment_1489\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-1489\" alt=\"Igalia &amp; Bloomberg logos\" src=\"http:\/\/blogs.igalia.com\/mrego\/files\/2014\/03\/igalia-bloomberg.png\" width=\"400\" \/><\/p>\n<p class=\"wp-caption-text\"><strong>Igalia<\/strong> and <strong>Bloomberg<\/strong> working to build a better web platform<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been a while since Igalia and Bloomberg started to implement the Box Alignment specification for the CSS Grid Layout model. Some weeks ago we accomplished an important milestone of our roadmap landing in Blink trunk the last patches implementing the Content Distribution properties: align-content and justify-content. Quoting the CSS Box Alignment document, the content &hellip; <a href=\"https:\/\/blogs.igalia.com\/jfernandez\/2015\/03\/09\/content-distribution-in-css-grid-layout\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Content Distribution in CSS Grid Layout<\/span><\/a><\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,20,12,21],"tags":[],"class_list":["post-903","post","type-post","status-publish","format-standard","hentry","category-blink","category-css-grid-layout","category-webkit","category-webkit-igalia"],"_links":{"self":[{"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/posts\/903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/comments?post=903"}],"version-history":[{"count":75,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/posts\/903\/revisions"}],"predecessor-version":[{"id":1099,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/posts\/903\/revisions\/1099"}],"wp:attachment":[{"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/media?parent=903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/categories?post=903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/tags?post=903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}