{"id":1258,"date":"2024-03-14T18:48:35","date_gmt":"2024-03-15T00:48:35","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=1258"},"modified":"2024-03-14T18:48:35","modified_gmt":"2024-03-15T00:48:35","slug":"what-is-safe-alignment-in-css","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/what-is-safe-alignment-in-css\/","title":{"rendered":"What is safe alignment in CSS?"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.stefanjudis.com\/today-i-learned\/safe-unsafe-alignment-in-css-flexbox\/\">Stefan Judis covered this recently<\/a>, and like Stefan, I first heard of the <code>safe<\/code> keyword in CSS from Rachel Andrews at an AEA conference years ago. It&#8217;s used in addition to other alignment keywords in layout like this for example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-code-table\"><span class='shcb-loc'><span><span class=\"hljs-selector-class\">.flex<\/span> {\n<\/span><\/span><span class='shcb-loc'><span>  <span class=\"hljs-attribute\">display<\/span>: flex;\n<\/span><\/span><mark class='shcb-loc'><span>  <span class=\"hljs-attribute\">align-items<\/span>: safe center;\n<\/span><\/mark><span class='shcb-loc'><span>}\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The extra value <code>safe<\/code> here is essentially saying <em>&#8220;don&#8217;t let the alignment force a situation where the content is positioned off the screen where a user can&#8217;t scroll to it&#8221;<\/em>. Content that is pushed off the screen in a way a user can&#8217;t scroll to it (think off the top or left of a browser window, or any element with scrolling overflow) is called <strong>data loss<\/strong>. I love the term data loss in CSS because that&#8217;s exactly what it is. For sighted users who only access websites with their vision, if they can&#8217;t see content, it ain&#8217;t there. <\/p>\n\n\n\n<p><a href=\"https:\/\/codepen.io\/chriscoyier\/pen\/rNbeyXj?editors=1100\">Here&#8217;s my own demo for explaining this.<\/a><\/p>\n\n\n\n<p>I&#8217;ll post a video here below. <em>Without<\/em> using the <code>safe<\/code> keyword, and using <code>align-items: center;<\/code> in a flexbox context, you can see how if there isn&#8217;t enough room in an element, it&#8217;s possible for elements to go up over the top (block start) of the element in such a way it&#8217;s impossible to scroll to. You can scroll <em>down<\/em> just not <em>up.<\/em><\/p>\n\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player\" style=\"\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='250' src='https:\/\/videopress.com\/embed\/wYvWJ1FP?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script><\/div>\n\t\t\t\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n<p>Then if you <em>add<\/em> the <code>safe<\/code> keyword, you can see the the layout <em>won&#8217;t let the items go up into that inaccessible\/unscrollable<\/em>. That way you can always scroll down to get the content, the content never goes up <\/p>\n\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player\" style=\"\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='250' src='https:\/\/videopress.com\/embed\/4PqouYBx?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script><\/div>\n\t\t\t\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n<p>See how the elements stick to the top as the size of the parent changes rather than go above it? <code>safe<\/code>! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stefan Judis covered this recently, and like Stefan, I first heard of the safe keyword in CSS from Rachel Andrews at an AEA conference years ago. It&#8217;s used in addition to other alignment keywords in layout like this for example: The extra value safe here is essentially saying &#8220;don&#8217;t let the alignment force a situation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[7,53],"class_list":["post-1258","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-css","tag-layout"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/03\/safe-thumb.jpg?fit=1000%2C500&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=1258"}],"version-history":[{"count":2,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1258\/revisions"}],"predecessor-version":[{"id":1265,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1258\/revisions\/1265"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/1263"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=1258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=1258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=1258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}