How does CSS computation for background percentages work? [duplicate]
Asked Answered
A

3

9

I'm currently playing around with CSS gradients and position, I kind of manage to do what I want but with lucky guesses but I'd like to understand how this actually all works.

For instance, here's the French flag (merci):

.serge_testDraw {
    width: 10rem;
    height: 10rem;
    border: 0.2rem solid black;
    background-image:
        linear-gradient(to right, blue 0%, blue 100%)
        , linear-gradient(to right, white 0%, white 100%)
        , linear-gradient(to right, red 0%, red 100%)
    ;
    background-size: calc(100% / 3) 100%;
    background-repeat: no-repeat;
    background-position: 0%, 50%, 100%;
}

How come the background position is 0%, 50, 100% instead of 0, 33.33%(a third), 66.66% (two third)?

Plus, I'm clueless how to position backgrounds whose size is 100%.

Aviva answered 3/9, 2014 at 15:28 Comment(2)
what's interesting is the middle background-position can be any value, it doesn't seem to matter, but it has to be present jsfiddle.net/#&togetherjs=74v86sn02JMetzgar
It looks as though, since you have three background images defined, the background-position property sets the initial position for each image, which applies to the left edge of each "image", in your case a red, a blue and white section 33% in lengthMetzgar
R
11

A percentage value for background-position does not position the origin of a background image with respect to the background positioning area. It positions the entire image. This means the dimensions of the image itself are also taken into account (after resizing it with background-size).

A background position of 100% 100% is analogous to right bottom, positioning an image such that the bottom right corner of the image touches the bottom right corner of the background area. Similarly, 50% 50% is analogous to center center, placing the midpoint of an image on the midpoint of the background area.

Imagine sliding a rectangular tile around the interior of a rectangular frame; moving it all the way to the right (i.e. 100%) means having its right edge touch the right side of the frame (since you can't slide it through the frame), and moving it to the bottom means having its bottom edge touch the bottom of the frame.

Generally speaking, for any background-position: x y where the two values are percentages, the x point of the image is aligned with the x point of the background area, and the y point of the image is aligned with the y point of the background area.

CSS2.1's description is painful to read so I'll quote CSS3 Backgrounds and Borders instead, where there's even a graphical example:

For example, with a value pair of ‘0% 0%’, the upper left corner of the image is aligned with the upper left corner of, usually, the box's padding edge. A value pair of ‘100% 100%’ places the lower right corner of the image in the lower right corner of the area. With a value pair of ‘75% 50%’, the point 75% across and 50% down the image is to be placed at the point 75% across and 50% down the area.

But if you're like me, and you're terrible at visualizing this in real time, then just think of a sliding puzzle instead.

Note that none of this applies to absolute values; absolute values do position the origin of the image. However, the reference point can be changed if you anchor the absolute value to the right and/or bottom sides, for example right 10px bottom 10px positions a background image 10 pixels away from the bottom right corner of the background area.

If you want to position a background image whose size is 100% of the background area, you won't be able to use a percentage, since you can't move a tile that fits its frame perfectly (which incidentally would make for either the most boring puzzle or the perfect prank). This applies whether the intrinsic dimensions of the background image match the dimensions of the element, or you explicitly set background-size: 100%. So, to position the image, you will need to use use an absolute value instead (forgoing the sliding-puzzle analogy altogether).

Rembrandt answered 3/9, 2014 at 16:14 Comment(0)
G
1

Like Woodrow said because of your background-size rule who gonna apply for each background, each background gonna take a third of your flag so 0% it's starting point for the first background.

Second one gonna take a third again but to be more easy to understand think 50% it's center more than 50% this is why he is in the middle and take a third of container. You can use center instead of 50% if you want.

Here's mozilla's doc about background-position

Grunter answered 3/9, 2014 at 15:43 Comment(0)
S
0

The background-position property sets the position of the center of the background. You've specified that all backgrounds have a width of calc(100%/3) and a height of 100%, then you've specified the x-position for the center of each of the three backgrounds (the y-position defaults to 50%).

Scrope answered 3/9, 2014 at 15:36 Comment(4)
how can 0% represent the center, is it not the edge?Metzgar
so let's say the whole flag is 600px wide. you've got three backgrounds: blue, white, and red. next, you set the size of each background to be one-third of the total width (200px). now you set the center of the blue background to be 0% from the left of the entire flag. so the 200px wide blue sliver is actually hanging halfway off the edge of the flag... only the 100px on the right are showing.Scrope
"so the 200px wide blue sliver is actually hanging halfway off the edge of the flag... only the 100px on the right are showing." No, if that were true, then every background image in existence that's left-aligned would be clipped.Rembrandt
thank you, i was mistaken. awesome well-sourced answer, btw.Scrope

© 2022 - 2024 — McMap. All rights reserved.