_objects.crop.scss 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ==========================================================================
  2. #CROP
  3. ========================================================================== */
  4. // A list of cropping ratios that get generated as modifier classes.
  5. // You should predefine it with only the ratios and names your project needs.
  6. //
  7. // The map keys are the strings used in the generated class names, and they can
  8. // follow any convention, as long as they are properly escaped strings. i.e.:
  9. //
  10. // $inuit-crops: (
  11. // "2\\:1" : (2:1),
  12. // "4-by-3" : (4:3),
  13. // "full-hd" : (16:9),
  14. // "card-image" : (2:3),
  15. // "golden-ratio" : (1.618:1) -> non-integers are okay
  16. // ) !default;
  17. $inuit-crops: (
  18. "2\\:1" : (2:1),
  19. "4\\:3" : (4:3),
  20. "16\\:9" : (16:9)
  21. ) !default;
  22. /**
  23. * Provide a cropping container in order to display media (usually images)
  24. * cropped to certain ratios.
  25. *
  26. * 1. Set up a positioning context in which the image can sit.
  27. * 2. This is the crucial part: where the cropping happens.
  28. */
  29. .o-crop {
  30. position: relative; /* [1] */
  31. display: block;
  32. overflow: hidden; /* [2] */
  33. }
  34. /**
  35. * Apply this class to the content (usually `img`) that needs cropping.
  36. *
  37. * 1. Image’s default positioning is top-left in the cropping box.
  38. * 2. Make sure the media doesn’t stop itself too soon.
  39. */
  40. .o-crop__content {
  41. position: absolute;
  42. top: 0; /* [1] */
  43. left: 0; /* [1] */
  44. max-width: none; /* [2] */
  45. }
  46. /**
  47. * We can position the media in different locations within the cropping area.
  48. */
  49. .o-crop__content--left-top {
  50. left: 0;
  51. }
  52. .o-crop__content--left-center {
  53. top: 50%;
  54. transform: translateY(-50%);
  55. }
  56. .o-crop__content--left-bottom {
  57. top: auto;
  58. bottom: 0;
  59. }
  60. .o-crop__content--right-top {
  61. right: 0;
  62. left: auto;
  63. }
  64. .o-crop__content--right-center {
  65. top: 50%;
  66. right: 0;
  67. left: auto;
  68. transform: translateY(-50%);
  69. }
  70. .o-crop__content--right-bottom {
  71. top: auto;
  72. right: 0;
  73. bottom: 0;
  74. left: auto;
  75. }
  76. .o-crop__content--center-top {
  77. left: 50%;
  78. transform: translateX(-50%);
  79. }
  80. .o-crop__content--center,
  81. .o-crop__content--center-center {
  82. top: 50%;
  83. left: 50%;
  84. transform: translate(-50%, -50%);
  85. }
  86. .o-crop__content--center-bottom {
  87. top: auto;
  88. bottom: 0;
  89. left: 50%;
  90. transform: translateX(-50%);
  91. }
  92. /* Crop-ratio variants
  93. ========================================================================== */
  94. /**
  95. * Generate a series of crop classes to be used like so:
  96. *
  97. * <div class="o-crop o-crop--golden-ratio">
  98. *
  99. */
  100. @each $crop-name, $crop-value in $inuit-crops {
  101. @each $antecedent, $consequent in $crop-value {
  102. @if (type-of($antecedent) != number) {
  103. @error "`#{$antecedent}` needs to be a number.";
  104. }
  105. @if (type-of($consequent) != number) {
  106. @error "`#{$consequent}` needs to be a number.";
  107. }
  108. .o-crop--#{$crop-name} {
  109. padding-bottom: ($consequent/$antecedent) * 100%;
  110. }
  111. }
  112. }
  113. /* Fill modifier
  114. ========================================================================== */
  115. /**
  116. * Content stretches to fill it's container while maintaining aspect-ratio.
  117. */
  118. .o-crop--fill {
  119. > .o-crop__content {
  120. min-height: 100%;
  121. min-width: 100%;
  122. }
  123. }