{"id":615,"date":"2019-10-22T14:27:58","date_gmt":"2019-10-22T18:27:58","guid":{"rendered":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/?p=615"},"modified":"2021-01-04T14:10:30","modified_gmt":"2021-01-04T19:10:30","slug":"last-of-type-but-only-if-it-meets-other-conditions-as-well","status":"publish","type":"post","link":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/2019\/last-of-type-but-only-if-it-meets-other-conditions-as-well\/","title":{"rendered":":last-of-type but only if it meets other conditions as well"},"content":{"rendered":"\n<p><code class=\"css\">.gallery-item:nth-of-type(3n+2):last-of-type<\/code><\/p>\n\n\n\n<p>I was working on a gallery with rows of 3 items per row for which the markup contained &lt;br&gt; after every third .gallery-item and the CSS used floats. It didn&#8217;t look how I wanted it to and needed to change it anyway, so I decided to also get rid of the floats and break-tags and use <code class=\"css\">display: flex<\/code> instead.<br><\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Grid would have been ever nicer because it would give more control, but also, for now, requires more lines of CSS to make it work in more browsers, so I opted for flexbox with space-between.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignright\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"225\" src=\"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-content\/uploads\/2019\/10\/flex-grid-300x225.gif\" alt=\"Illustration of a grid of 5 dark boxes on a yellow background divided over two rows. The top row has three boxes and the bottom one has two boxes. The right box on the bottom row is right-aligned, leaving a gap in the middle. A purple arrow points from the middle of that last box to the center of that row.\" class=\"wp-image-624\" srcset=\"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-content\/uploads\/2019\/10\/flex-grid-300x225.gif 300w, https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-content\/uploads\/2019\/10\/flex-grid-768x576.gif 768w, https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-content\/uploads\/2019\/10\/flex-grid-320x240.gif 320w, https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-content\/uploads\/2019\/10\/flex-grid-640x480.gif 640w, https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-content\/uploads\/2019\/10\/flex-grid-360x270.gif 360w, https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-content\/uploads\/2019\/10\/flex-grid-720x540.gif 720w, https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-content\/uploads\/2019\/10\/flex-grid-800x600.gif 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n\n<p>The markup isn&#8217;t mine but from WordPress, so I can&#8217;t\/don&#8217;t want to change that, but I can disable the break-tags with CSS: <code class=\"css\">#gallery-2 br { display: none; }<\/code>.<\/p>\n\n\n\n<p>What I like about space-between is that it makes the child-elements line up flush with the outer edges of the parent element. What I don&#8217;t like about it is that if the last row has less items on it, especially if it&#8217;s two instead of the number on the previous rows, you&#8217;ll get a big gap in the middle. When it&#8217;s 3 instead of 5, I don&#8217;t mind, or 4 instead of 5, but 2 instead of 4 or instead of 3, I think looks like you didn&#8217;t know what you were doing.<\/p>\n\n\n\n<p>So I wanted to add some CSS rules to the last .gallery-item (last-child would have targeted the last &lt;br&gt;) but only if it was the second .gallery-item of a row.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">#gallery-2 {\n    display: flex;\n    justify-content: space-between;\n    flex-wrap: wrap; <span style=\"color: #777;\">\/* or it'll put everything onto the same row *\/<\/span>\n    position: relative;\n}\n#gallery-2 br {\n    display: none; <span style=\"color: #777;\">\/* or display:flex will take them into account *\/<\/span>\n}\n#gallery-2 .gallery-item {\n    float: none; <span style=\"color: #777;\">\/* overriding the original float:left *\/<\/span>\n    width: 300px; <span style=\"color: #777;\">\/* overriding the original width:33% *\/<\/span>\n}\n@media only screen and (min-width: 1024px){\n<span style=\"color: #777;\">\/* on smaller screens the rows will have 2 or 1 item per row *\/<\/span>\n    #gallery-2 .gallery-item:nth-of-type(3n+2):last-of-type {\n    <span style=\"color: #777;\">\/* 2nd item on a row of three but only if it's the last of its kind *\/<\/span>\n        position: absolute;\n        bottom: 0;\n        left: calc(50% - 150px);\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<div class=\"_3bJ2H CHExY\">\n<div class=\"_1l8RX _1ByhS\">Photo by <a href=\"https:\/\/unsplash.com\/@curtismacnewton?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">Curtis MacNewton<\/a> on <a href=\"https:\/\/unsplash.com\/s\/photos\/collection?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">Unsplash<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>.gallery-item:nth-of-type(3n+2):last-of-type I was working on a gallery with rows of 3 items per row for which the markup contained &lt;br&gt; after every third .gallery-item and the CSS used floats. It didn&#8217;t look how I wanted it to and needed to change it anyway, so I decided to also get rid of the floats and break-tags &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\":last-of-type but only if it meets other conditions as well\" class=\"read-more button\" href=\"https:\/\/www.blindemanwebsites.com\/today-i-learned\/2019\/last-of-type-but-only-if-it-meets-other-conditions-as-well\/#more-615\" aria-label=\"Read more about :last-of-type but only if it meets other conditions as well\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[17,75,18,41],"class_list":["post-615","post","type-post","status-publish","format-standard","hentry","category-new-things","tag-css","tag-flexbox","tag-pseudo-elements","tag-wordpress","infinite-scroll-item"],"_links":{"self":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/615","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=615"}],"version-history":[{"count":1,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":1134,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/posts\/615\/revisions\/1134"}],"wp:attachment":[{"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blindemanwebsites.com\/today-i-learned\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}