_objects.ratio.scss 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* ==========================================================================
  2. #RATIO
  3. ========================================================================== */
  4. // A list of aspect 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-ratios: (
  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-ratios: (
  18. "2\\:1" : (2:1),
  19. "4\\:3" : (4:3),
  20. "16\\:9" : (16:9)
  21. ) !default;
  22. /**
  23. * Create ratio-bound content blocks, to keep media (e.g. images, videos) in
  24. * their correct aspect ratios.
  25. *
  26. * http://alistapart.com/article/creating-intrinsic-ratios-for-video
  27. *
  28. * 1. Default is a 1:1 ratio (i.e. a perfect square).
  29. */
  30. .o-ratio {
  31. position: relative;
  32. display: block;
  33. &:before {
  34. content: "";
  35. display: block;
  36. width: 100%;
  37. padding-bottom: 100%; /* [1] */
  38. }
  39. }
  40. .o-ratio__content,
  41. .o-ratio > iframe,
  42. .o-ratio > embed,
  43. .o-ratio > object {
  44. position: absolute;
  45. top: 0;
  46. bottom: 0;
  47. left: 0;
  48. height: 100%;
  49. width: 100%;
  50. }
  51. /* Ratio variants.
  52. ========================================================================== */
  53. /**
  54. * Generate a series of ratio classes to be used like so:
  55. *
  56. * <div class="o-ratio o-ratio--golden-ratio">
  57. *
  58. */
  59. @each $ratio-name, $ratio-value in $inuit-ratios {
  60. @each $antecedent, $consequent in $ratio-value {
  61. @if (type-of($antecedent) != number) {
  62. @error "`#{$antecedent}` needs to be a number.";
  63. }
  64. @if (type-of($consequent) != number) {
  65. @error "`#{$consequent}` needs to be a number.";
  66. }
  67. .o-ratio--#{$ratio-name}:before {
  68. padding-bottom: ($consequent/$antecedent) * 100%;
  69. }
  70. }
  71. }
  72. /* Contain modifier.
  73. ========================================================================== */
  74. /**
  75. * Only works with image content.
  76. * Contains the image to the boundaries, without cropping or stretching it.
  77. */
  78. .o-ratio--img-contain {
  79. > .o-ratio__content:before {
  80. height: auto;
  81. margin: auto;
  82. max-height: 100%;
  83. max-width: 100%;
  84. width: auto;
  85. }
  86. }