{"id":1413,"date":"2020-08-14T16:45:10","date_gmt":"2020-08-14T16:45:10","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=1413"},"modified":"2020-08-14T16:45:10","modified_gmt":"2020-08-14T16:45:10","slug":"what-does-100-mean-in-css-2","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=1413","title":{"rendered":"What does 100% mean in CSS?"},"content":{"rendered":"\n<p>One of the CSS units I use most is the wonderful&nbsp;<strong>%<\/strong>&nbsp;\u2014 so handy for positioning elements on the page.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Unfortunately, the rules aren\u2019t exactly straightforward. One question I\u2019m always asking myself is:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Percent of&nbsp;<em>what<\/em>?<\/p><\/blockquote>\n\n\n\n<p>Hopefully this guide can help clear things up.<a href=\"https:\/\/wattenberger.com\/blog\/css-percents#gist\">Just give me the gist!<\/a><a href=\"https:\/\/wattenberger.com\/blog\/css-percents#width,height\">#<\/a><\/p>\n\n\n\n<h2>The basics: width &amp; height<\/h2>\n\n\n\n<p>In these examples, the purple box is our lovely&nbsp;self&nbsp;element \u2014 this is the element we\u2019re trying to position using CSS properties. The surrounding blue box is the&nbsp;parent&nbsp;element.It\u2019s important to note that the&nbsp;self&nbsp;element is&nbsp;<code>absolutely<\/code>&nbsp;positioned, to take it out of the normal flow and prevent moving things as we\u2019re poking around.<\/p>\n\n\n\n<p>Let\u2019s start with our most basic, and most straightforward, example:&nbsp;<code>width<\/code>&nbsp;and&nbsp;<code>height<\/code>. Move the sliders around to get a feel for how the width and height of our&nbsp;self&nbsp;element changes with different percentage values.height50% (100px)width50% (300px)200px600px<code>.self {<\/code><code> position: absolute;<\/code><code> height: 50%;<\/code><code> width: 50%;<\/code><code>}<\/code><\/p>\n\n\n\n<p>We can see that our&nbsp;element\u2019s&nbsp;<code>width<\/code>&nbsp;and&nbsp;<code>height<\/code>&nbsp;are based on our&nbsp;parent\u2019s&nbsp;<code>width<\/code>&nbsp;and&nbsp;<code>height<\/code>&nbsp;(respectively).<a href=\"https:\/\/wattenberger.com\/blog\/css-percents#top,left\">#<\/a><\/p>\n\n\n\n<h2>top &amp; left<\/h2>\n\n\n\n<p>Great! Seems pretty straightforward \u2014 let\u2019s move on to&nbsp;<code>left<\/code>&nbsp;and&nbsp;<code>top<\/code>:height50% (100px)width50% (300px)top0% (0px)left50% (300px)200px600px<code>.self {<\/code><code> position: absolute;<\/code><code> height: 50%;<\/code><code> width: 50%;<\/code><code> top: 0%;<\/code><code> left: 50%;<\/code><code>}<\/code><\/p>\n\n\n\n<p>These values are also based on our&nbsp;parent\u2019s&nbsp;<code>width<\/code>&nbsp;and&nbsp;<code>height<\/code>. If an element has a&nbsp;<code>left<\/code>&nbsp;value of&nbsp;<code>50%<\/code>, its left side will sit halfway across its parent component.<a href=\"https:\/\/wattenberger.com\/blog\/css-percents#margins\">#<\/a><\/p>\n\n\n\n<h2>margins<\/h2>\n\n\n\n<p>What about&nbsp;<code>margin<\/code>s?height50% (100px)width50% (300px)margin top0% (0px)margin left50% (300px)200px600px<code>.self {<\/code><code> position: absolute;<\/code><code> height: 50%;<\/code><code> width: 50%;<\/code><code> margin-top: 0%;<\/code><code> margin-left: 50%;<\/code><code>}<\/code><\/p>\n\n\n\n<p><code>margin<\/code>s work similarly to our last example \u2014 they are based on the size of their&nbsp;parent. However, there is one weird thing here that is important to note:<\/p>\n\n\n\n<p><strong><code>margin-top<\/code>&nbsp;is based on our&nbsp;parent\u2019s&nbsp;<em>width<\/em>, not height<\/strong>&nbsp;(and the same goes for&nbsp;<code>margin-bottom<\/code>). In other words, all&nbsp;<code>margin<\/code>s are a percent of their&nbsp;parent&#8217;s width.<a href=\"https:\/\/wattenberger.com\/blog\/css-percents#padding\">#<\/a><\/p>\n\n\n\n<h2>padding<\/h2>\n\n\n\n<p>And what about&nbsp;<code>padding<\/code>? Should be the same as&nbsp;<code>margin<\/code>, right?height50% (100px)width50% (300px)padding top0% (0px)padding left50% (300px)200px600px<code>.self {<\/code><code> position: absolute;<\/code><code> height: 50%;<\/code><code> width: 50%;<\/code><code> padding-top: 0%;<\/code><code> padding-left: 50%;<\/code><code>}<\/code><\/p>\n\n\n\n<p>And it is! For the most part.<\/p>\n\n\n\n<p>Something interesting you might notice here is that&nbsp;<code>padding-left<\/code>&nbsp;(for example) won\u2019t change the&nbsp;<code>width<\/code>&nbsp;of our&nbsp;self element, unless the&nbsp;<code>padding-left<\/code>&nbsp;value is&nbsp;<em>greater<\/em>&nbsp;than our&nbsp;self element\u2019s&nbsp;<code>width<\/code>. This is because I use the&nbsp;<code>border-box<\/code>&nbsp;<code>box-sizing<\/code>&nbsp;model. Unfamiliar, or need a recap?&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Learn\/CSS\/Building_blocks\/The_box_model\" target=\"_blank\" rel=\"noreferrer noopener\">Read more on MDN.<\/a><a href=\"https:\/\/wattenberger.com\/blog\/css-percents#transform-translate\">#<\/a><\/p>\n\n\n\n<h2>transform: translate<\/h2>\n\n\n\n<p>Okay! Here is where things get a bit&#8230; weird.<\/p>\n\n\n\n<p>Let\u2019s take a look at&nbsp;<code>transform: translate<\/code>:height50% (100px)width50% (300px)translate top0% (0px)translate left50% (150px)200px600px<code>.self {<\/code><code> position: absolute;<\/code><code> height: 50%;<\/code><code> width: 50%;<\/code><code> translate-top: 0%;<\/code><code> translate-left: 50%;<\/code><code> transform: translate(50%, 0%);<\/code><code>}<\/code><\/p>\n\n\n\n<p>The box moves a lot more slowly this time, right? That\u2019s because&nbsp;<strong><code>transform: translate<\/code>&nbsp;percentage values are based on our&nbsp;<code>self element\u2019s<\/code>&nbsp;<code>width<\/code>&nbsp;and&nbsp;<code>height<\/code><\/strong>.<a href=\"https:\/\/wattenberger.com\/blog\/css-percents#gist\">#<\/a><\/p>\n\n\n\n<h2>TLDR<\/h2>\n\n\n\n<p>Let\u2019s recap what we\u2019ve learned:heightparent\u2019s heightwidthparent\u2019s widthtopparent\u2019s heightleftparent\u2019s widthmargin-topparent\u2019s widthmargin-leftparent\u2019s widthpadding-topparent\u2019s widthpadding-leftparent\u2019s widthtranslate-topself\u2019s heighttranslate-leftself\u2019s width<\/p>\n\n\n\n<p>Now let\u2019s put what we\u2019ve learned to the test!<a href=\"https:\/\/wattenberger.com\/blog\/css-percents#exercise\">#<\/a><\/p>\n\n\n\n<h2>Centering elements<\/h2>\n\n\n\n<p>How could we center an element inside its parent, no matter its own dimensions?<\/p>\n\n\n\n<p>Try centering our&nbsp;self element&nbsp;inside of the&nbsp;parent. Make sure it doesn\u2019t move around when you tweak its&nbsp;<code>width<\/code>&nbsp;and&nbsp;<code>height<\/code>.height50% (100px)width50% (300px)top0% (0px)left50% (300px)translate top0% (0px)translate left50% (150px)200px600px<code>.self {<\/code><code> position: absolute;<\/code><code> height: 50%;<\/code><code> width: 50%;<\/code><code> top: 0%;<\/code><code> left: 50%;<\/code><code> translate-top: 0%;<\/code><code> translate-left: 50%;<\/code><code> transform: translate(50%, 0%);<\/code><code>}<\/code>Show me!<\/p>\n\n\n\n<p>See how this works? We\u2019re moving our&nbsp;self&nbsp;element:<\/p>\n\n\n\n<ul><li><em>down<\/em>&nbsp;by 50% of our&nbsp;parent\u2019s&nbsp;height using&nbsp;<code>top: 50%<\/code><\/li><li><em>right<\/em>&nbsp;by 50% of our&nbsp;parent\u2019s&nbsp;width using&nbsp;<code>left: 50%<\/code><\/li><li><em>up<\/em>&nbsp;by 50% of our&nbsp;self element\u2019s&nbsp;height using&nbsp;<code>transform: translate (top): -50%<\/code><\/li><li><em>left<\/em>&nbsp;by 50% of our&nbsp;self element\u2019s&nbsp;width using&nbsp;<code>transform: translate (left): -50%<\/code><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>One of the CSS units I use most is the wonderful&nbsp;%&nbsp;\u2014 so handy for positioning elements on the page.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[105],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1413"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1413"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1413\/revisions"}],"predecessor-version":[{"id":1414,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1413\/revisions\/1414"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}