{"id":1105,"date":"2020-11-06T17:31:16","date_gmt":"2020-11-06T22:31:16","guid":{"rendered":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/?p=1105"},"modified":"2020-11-06T17:34:32","modified_gmt":"2020-11-06T22:34:32","slug":"css-columns-across-browsers","status":"publish","type":"post","link":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/2020\/css-columns-across-browsers\/","title":{"rendered":"CSS Columns Across Browsers"},"content":{"rendered":"\n<p><strong>What:<\/strong> When using CSS columns, the content doesn&#8217;t always line up at the top of each new column. The more columns you have, the more likely you are to run into this problem, especially if you&#8217;re using <code>column-fill: balance<\/code>. And the problem seems to happen in Chrome-based browsers, in Safari and in browsers on iOS.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>The two problems I&#8217;ve been having is to get all of the content to actually align at the top and to truly &#8220;balance&#8221;, meaning that it doesn&#8217;t leave the last column(s) empty when there is more content to fill it with. <\/p>\n\n\n\n<p>I&#8217;ve been using columns for no-CSS masonry photo galleries. Not what they are intended for but it works and it means I don&#8217;t have to load any JavaScript (on a page that is already loading a lot of photos) to create the masonry effect.<\/p>\n\n\n\n<p><strong>How:<\/strong> The CSS I&#8217;ve ended up with is below, with comments for clarification. Not all of it is perhaps needed, but that would need more testing than I currently had time for. Amendments welcome.<\/p>\n\n\n\n<pre class=\"wp-block-code\" title=\"The CSS\"><code lang=\"css\" class=\"language-css line-numbers\">.gallery {\n    column-count: 4; \/* Can be any number *\/\n    column-gap: 0.5rem;\n    column-fill: balance; \/* This is the default though, so unnecessary *\/\n    \/* The below is only needed if you're using an &lt;ul> *\/\n    margin: 0;\n    list-style: none;\n}\n.gallery-item {\n    display: block; \/* This helps with alignment in Chrome *\/\n    \/* The below helps with alignment in Safari and other browsers on iOS *\/\n    page-break-inside: avoid;\n    break-inside: avoid;\n    -webkit-column-break-inside: avoid;\n    \/* I used to also add here a \n    margin-bottom: 0.5rem; \n    to create some space between the images but since that's not \n    \"inside\", I now use: *\/\n    padding-bottom: 0.5rem;\n}<\/code><\/pre>\n\n\n\n<p>Your markup can be one of the below:<\/p>\n\n\n\n<pre class=\"wp-block-code\" title=\"HTML example 1\"><code lang=\"markup\" class=\"language-markup\">&lt;ul class=\"gallery\">\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-1.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-2.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-3.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-4.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-5.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-6.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-7.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-8.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-9.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-10.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-11.jpg\" \/>&lt;\/li>\n    &lt;li class=\"gallery-item\">&lt;img src=\"img\/photo-12.jpg\" \/>&lt;\/li>\n&lt;\/ul><\/code><\/pre>\n\n\n\n<p>Or:<\/p>\n\n\n\n<pre class=\"wp-block-code\" title=\"HTML example 2\"><code lang=\"markup\" class=\"language-markup\">&lt;div class=\"gallery\">\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-1.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-2.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-3.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-4.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-5.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-6.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-7.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-8.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-9.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-10.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-11.jpg\" \/>&lt;\/figure>\n    &lt;figure class=\"gallery-item\">&lt;img src=\"img\/photo-12.jpg\" \/>&lt;\/figure>\n&lt;\/div><\/code><\/pre>\n\n\n\n<p>If you&#8217;re using columns for text content, this may also be interesting: <a href=\"https:\/\/www.blindemanwebsites.com\/today-i-learned\/2020\/always-include-widows-and-orphans\/\" data-type=\"post\" data-id=\"868\">Always include Widows &amp; Orphans<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What: When using CSS columns, the content doesn&#8217;t always line up at the top of each new column. The more columns you have, the more likely you are to run into this problem, especially if you&#8217;re using column-fill: balance. And the problem seems to happen in Chrome-based browsers, in Safari and in browsers on iOS.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[19,17,188,129],"class_list":["post-1105","post","type-post","status-publish","format-standard","hentry","category-new-things","tag-browser-support","tag-css","tag-gallery","tag-multic-column","infinite-scroll-item"],"_links":{"self":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/1105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/comments?post=1105"}],"version-history":[{"count":3,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/1105\/revisions"}],"predecessor-version":[{"id":1121,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/1105\/revisions\/1121"}],"wp:attachment":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/media?parent=1105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/categories?post=1105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/tags?post=1105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}