Last active
September 15, 2018 23:21
-
-
Save ThatGuySam/a1a943e4f3226d5e16abab460ab3446a to your computer and use it in GitHub Desktop.
Crops toward focal point instead of center of image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cropping towards a focal point | |
/* | |
This controls where the image is cropped toward as | |
the design flows around it responsively. | |
This would be the same as a focal point in photography. | |
Original Idea: https://designshack.net/articles/mobile/focal-point-intelligent-cropping-of-responsive-images/ | |
*/ | |
.crops-toward-focal-point { | |
&, | |
img#{&}, | |
& img { | |
object-position: var(--focal-left, 50%) var(--focal-top, 50%); | |
// Optional CSS to get working quickly, but you might want to | |
// move this stuff to other classes so this helper isn't so | |
// opinionated | |
// height: 100%; | |
// width: 100%; | |
// object-fit: cover; | |
background-position: var(--focal-left, 50%) var(--focal-top, 50%); | |
} | |
} | |
.focal-top { | |
@for $i from 0 through 10 { | |
$percent: $i * 10; | |
&-#{$percent} { | |
// 1% just converts it to percent | |
// ex: 40 * 1% = 40% | |
--focal-top: #{$percent * 1%}; | |
} | |
} | |
} | |
.focal-left { | |
@for $i from 0 through 10 { | |
$percent: $i * 10; | |
&-#{$percent} { | |
// 1% just converts it to percent | |
// ex: 40 * 1% = 40% | |
--focal-left: #{$percent * 1%}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment