!!! If anyone can answer this now, I will wait for the bounty period to end, and up it to 150 before awarding it. (Scouts honour!) !!!
I have looked around but can’t find an answer to this question:
I am getting event cover images from fb api, and also storing the offset_x and offset_y values. Then I place the images as css background-images like this:
CSS
.event-image {
margin-bottom: 30px;
width: auto;
width: 500px;
height: 178px;
max-width: 100%;
min-width: 300px;
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
outline: none;
}
The height/width is based on the exact ratio used by facebook: 2,8:1
HTML
<div class="event-image" style="background-image: url([url of img]); background-position: [offset_x]% [offset_y]%;" >
(I do have some internal logic that only adds the background-position property if there is one set in the fb api.)
The problem is that this only works 90% of the time. Roughly 10 % of the images are slightly miss-aligned. usually only by a few percentage points 🙁
Here is an example.
.event-image {
margin-bottom: 30px;
width: auto;
width: 500px;
height: 178px;
max-width: 100%;
min-width: 300px;
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
outline: none;
}
<div class="event-image" style="background-image: url(https://scontent.xx.fbcdn.net/t31.0-0/p180x540/14566476_1295215523823549_1334771895810946224_o.jpg); background-position: 0% 50%; "> </div>
<!-- Offsets are taken directly from API -->
Now here is the actual event
You can see that actually the offset would be perfect at 46% – so why is fb giving 50%?
The most info I can find is on pixel calculations, but considering I am using percentages, this wasn’t useful.
EDIT
New issue implementing elfan’s solution:
Here is an event on fb where the image has offset_x in the actual fb page of -7px – but according the api, the offset_x = 50%
{
"cover": {
"offset_x": 50,
"offset_y": 0,
"source": "https://scontent.xx.fbcdn.net/t31.0-8/s720x720/14681104_1307094859330152_7540791723420117074_o.jpg",
"id": "1307094859330152"
},
"id": "544220282434185"
}
So, using the calculation 500px (width of my image) * offset_x % = 250px
What am I doing wrong 🙂
I have also noticed that there are some events which have crazy offsets, like 1800 for example. According to fb docs, it should be between 0-100.
2
Answers
I have seen this method being used in regards to offsets from FB.
There is a problem with the interpretation.
The value
50
from fb api apparently refers to the offset in percentage as when using it intop
, which means percentage of the containing block’s height (spec here). And that is different to when using it inbackground-position
(spec here). There is also an article here that visually shows the difference.If you want to use
background-position
, the solution is to usepx
.Alternatively, you can use
top
, either as%
orpx
.I have made the following code to compare your code, fb code, and what the fix should be (for all alternatives):
Additional explanation about why 46% looks correct in your original code:
background-position: 0%
is the same astop: 0px
background-position: 100%
is the same astop: -197px
background-position: 50%
is the same astop: -98.5px
(197 x 50%)background-position: 46%
is the same astop: -90.62px
(197 x 46%), while the correct one istop: -89px
, so that looks close enough.Where does
197
come from?The box size is 500 x 178px. The actual image size is 800 x 600px. The rendered/scaled image size due to
background-size: cover
is 500 x 375px.The image height is 375-178 = 197px larger than the box height. Remember that when using
background-position: 100%
, the bottom edge of the image will meet the bottom edge of the box, which meanstop: -197px
.