can a background image be negatively positioned to the bottom or right, or only a portion of a picture repeated?

◇◆丶佛笑我妖孽 提交于 2019-12-05 02:56:58

For your first question, no you cannot.

For your second question, no you cannot.

To "negatively positioning a background picture to the right or bottom", you can use percentages less than 0%. e.g.:

background-position: -11% -7%;

...positions the image cropped on the bottom right if the image is of similar size to the element. You might need more negative percentages if they aren't the same size.

To "negatively positioning a background picture to the left or top", you can use percentages greater than 100%. e.g.:

background-position: 105% 110%;

...positions the image cropped on the top and left assuming again that they are of similar sizes. You might need larger percentages if they aren't the same size.

Finding the exact percentages you need is not very intuitive, however. CSS uses percentages a little differently with background-position. The value is the percentage along both the image and element (aka viewport) where they are the same. This is why 0% is equal to left, 50% is equal to center, and 100% is equal to right. Outside of this range it is even less intuitive as "less than zero" matches a point before the top/left edge on both the image and the element (effectively shifting the image away from the top/left), and "greater than 100%" matches a point greater than the far bottom/right edge which has the effect of moving the image away from the bottom/right.

These equation helps to determine where you want the image to sit. Simple algebra will get you the variable you want to know (e.g., solve for percentage).

effectiveLeft = (elementWidth - imageWidth) * percentageLeft;
effectiveTop = (elementHeight - imageHeight) * percentageTop;

As for your second question (repeating part of an image), I don't believe this is possible unless you were to repeat multiple cropping elements, which is different than what you were wanting.

charles marshall

If you use percentage based background positions you can. So you would want something like:

background-position: 190% 0;
Ricardo Escobar Montecinos

They've already answer that you can't use negative values, but maybe you could try:

Give the element background-position:right bottom and the image for the background should have only the part you want to show.

Tyler Treat

The background-position property has a few different values:

left top
left center
left bottom
right top
right center
right bottom
center top
center center
center bottom

You can also specify X and Y positions for it. For more information on it, take a look at this link:

https://developer.mozilla.org/docs/CSS/background-position

As for your second question, that's not possible using simple CSS.

For negative right bottom position to work right, this DIV must necessarily be property background-repeat: no-repeat;

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!