What does 100% mean in CSS? (2020)
wattenberger.com
wattenberger.com
background-position
Unlike all other cases, here `background-position-x: [25]%` means:> "Place image so its inner left [quarter] boundary matches inner left [quarter] boundary of its container."
So 0% = align left edges, 100% = align right edges, 50% = align centers (= center) etc. Super intuitive on the first sight, but I was quite surprised how thought-out it was underneath.
https://developer.mozilla.org/en-US/docs/Web/CSS/background-...
It’s common for people to wish that percentages worked this way with other properties, because in some cases that’d be useful, but they don’t. (Example: margin-based centring, `width: 20em; margin-left: calc(50% - 10em);`.)
left: x%;
top: y%;
transform: translate(-x%, -y%);Most of the time, an element's containing block is its parent, but not always.
For more on this: https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_...
Examples: with flexbox and/or tables, 100% behaves differently. What about 100% vs 100vh/vw? This would be very nice to show here as well. As others note, background-position or background-size. Collapsing margins might be interesting, but probably not the best fit for this example. How other types of `display` interact with the 100% (e.g. `inline` just ignores it). As it's just very briefly touched, but how box-sizing affects the height/width. SVGs viewBox, canvas, hiding navbars on mobile, body's default width/height, the trick of `width: 100%; max-width: 1200px; margin: 0 auto;`, etc. there's such a vast amount of topics relating/breaking the concept of 100% in CSS that it's mind-numbing.
However, does this not make it much easier to understand if you are clear and understand the CSS Box Model[1] very well. I remember visualizing it in my head in kinda 3D model of the CSS Box, whenever something obvious do not work while placing objects on the page/layout for the browser. Once you know where your bearing and browser quarks, either you position your mark or find another way inside the Box.
I'm hoping they haven't change the CSS Box Model.
inset:0rem;
margin: auto;
it will center vertical and horizontal it without having to translate itcourtesy of Kevin Powell - https://www.youtube.com/watch?v=HOM47v73yG8 ( who I've found to be an amazing resource for learning all kinds of CSS info )
That's because `inset: 0rem` is equivalent to `top: 0rem; right: 0rem; bottom: 0rem; left: 0rem;` - which is a well know technique to force 100% height.
left: 50%;
margin-left: -205px;
border: 0px;
width: 410px;
Because when sized to 400px it always clips 10 pixels of the canvas. box-sizing: border-box;
width: 400px;
might be the fix for that scenarioThe brainpower wasted on web layout is immense.
Blame it on folks outside of dev? I would ban the word “modern” if possible. Everyone thinks that it’s obvious but no one seems to be able to define it once and for all, lol
Though it sure would be nice if people were exploring other approaches to reflowable layout without tying themselves to CSS and modern browsers’ codebases.
https://developer.mozilla.org/en-US/docs/Web/CSS/scrollbar-g...
Not something you can actually use though.
https://css-tricks.com/aspect-ratio-boxes/
[1] Recently `aspect-ratio` got into evergreen browsers: https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-rati...
But otherwise you’re right, it doesn’t really come up.
I always trip up on height/width being a % of the parent, and just default to using vh instead.
Edit - Ah! - it's clear with the 'height' slider dragged up: it's one of the dots from the background (except it isn't in the background) grid.
(R: 93, G: 200, B: 195) That's more green than blue. When you consider that humans have stronger green color preceptors it's definitely green.
"Definitely" green is a difficult assertion to make universal. It's true that it has slightly more green than blue in the sRGB space, but the blue/green boundary is not as culturally universal as you may expect
However, I'd also highlight the "padding" area in the box, so the "#Padding" example became more self-explanatory.