{"id":6709,"date":"2025-08-08T14:18:08","date_gmt":"2025-08-08T19:18:08","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=6709"},"modified":"2025-08-14T23:14:38","modified_gmt":"2025-08-15T04:14:38","slug":"the-one-liner-for-max-width-centering-and-margins","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/the-one-liner-for-max-width-centering-and-margins\/","title":{"rendered":"The one-liner for max-width, centering, and margins."},"content":{"rendered":"\n<p>To horizontally center an element and limit it&#8217;s width, this is easily the most common approach:<\/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\"><span class=\"hljs-selector-class\">.page-wrap<\/span> {\n  <span class=\"hljs-attribute\">max-width<\/span>: <span class=\"hljs-number\">800px<\/span>;\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span> auto;\n}<\/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>That could still touch the edges of a parent container though, so if need to enforce some spacing, we&#8217;d probably do that on a parent.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">body<\/span> {\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">1rem<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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>There is no real problem with that, but we can smash it all into a one-liner now if you&#8217;re feeling it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.page-wrap<\/span> {\n  <span class=\"hljs-attribute\">margin-inline<\/span>: <span class=\"hljs-built_in\">max<\/span>(<span class=\"hljs-number\">1.5rem<\/span>, ((<span class=\"hljs-number\">100%<\/span> - <span class=\"hljs-number\">800px<\/span>) \/ <span class=\"hljs-number\">2<\/span>));\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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 <code>1.5rem<\/code> is the space on the outside (which could be <code>0<\/code>), and the <code>500px<\/code> is the <code>max-width<\/code> (or rather <code>max-inline-size<\/code> as we&#8217;re using logical properties here). <\/p>\n","protected":false},"excerpt":{"rendered":"<p>To horizontally center an element and limit it&#8217;s width, this is easily the most common approach: That could still touch the edges of a parent container though, so if need to enforce some spacing, we&#8217;d probably do that on a parent. There is no real problem with that, but we can smash it all into [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6710,"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":[29],"tags":[7,347],"class_list":["post-6709","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-beat","tag-css","tag-logical-properties"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/08\/pexels-photo-987648.jpeg?fit=1880%2C1259&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/6709","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=6709"}],"version-history":[{"count":2,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/6709\/revisions"}],"predecessor-version":[{"id":6755,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/6709\/revisions\/6755"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/6710"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=6709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=6709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=6709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}