_breakpoints.less 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Breakpoint viewport sizes and media queries.
  2. //
  3. // Breakpoints are defined as a map of (name: minimum width), order from small to large:
  4. //
  5. // (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px)
  6. //
  7. // The map defined in the `@grid-breakpoints` global variable is used as the `@breakpoints` argument by default.
  8. @plugin "../plugins/breakpoints";
  9. // Name of the next breakpoint, or null for the last breakpoint.
  10. //
  11. // >> breakpoint-next(sm)
  12. // md
  13. // >> breakpoint-next(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  14. // md
  15. // >> breakpoint-next(sm, @breakpoint-names: (xs sm md lg xl))
  16. // md
  17. //@function breakpoint-next($name, $breakpoints: $grid-breakpoints, $breakpoint-names: map-keys($breakpoints)) {
  18. // $n: index($breakpoint-names, $name);
  19. // @return if($n < length($breakpoint-names), nth($breakpoint-names, $n + 1), null);
  20. //}
  21. // Minimum breakpoint width. Null for the smallest (first) breakpoint.
  22. //
  23. // >> breakpoint-min(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  24. // 576px
  25. //@function breakpoint-min($name, $breakpoints: $grid-breakpoints) {
  26. // $min: map-get($breakpoints, $name);
  27. // @return if($min != 0, $min, null);
  28. //}
  29. // Maximum breakpoint width. Null for the largest (last) breakpoint.
  30. // The maximum value is calculated as the minimum of the next one less 0.02px
  31. // to work around the limitations of `min-` and `max-` prefixes and viewports with fractional widths.
  32. // See https://www.w3.org/TR/mediaqueries-4/#mq-min-max
  33. // Uses 0.02px rather than 0.01px to work around a current rounding bug in Safari.
  34. // See https://bugs.webkit.org/show_bug.cgi?id=178261
  35. //
  36. // >> breakpoint-max(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  37. // 767.98px
  38. //@function breakpoint-max($name, $breakpoints: $grid-breakpoints) {
  39. // $next: breakpoint-next($name, $breakpoints);
  40. // @return if($next, breakpoint-min($next, $breakpoints) - 0.01px, null);
  41. //}
  42. // Returns a blank string if smallest breakpoint, otherwise returns the name with a dash in front.
  43. // Useful for making responsive utilities.
  44. //
  45. // >> breakpoint-infix(xs, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  46. // "" (Returns a blank string)
  47. // >> breakpoint-infix(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  48. // "-sm"
  49. //@function breakpoint-infix($name, $breakpoints: $grid-breakpoints) {
  50. // @return if(breakpoint-min($name, $breakpoints) == null, "", "-#{$name}");
  51. //}
  52. // Media of at least the minimum breakpoint width. No query for the smallest breakpoint.
  53. // Makes the @content apply to the given breakpoint and wider.
  54. //@mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
  55. // $min: breakpoint-min($name, $breakpoints);
  56. // @if $min {
  57. // @media (min-width: $min) {
  58. // @content;
  59. // }
  60. // } @else {
  61. // @content;
  62. // }
  63. //}
  64. #media-breakpoint-up(@name, @content, @breakpoints: @grid-breakpoints) {
  65. @min: breakpoint-min(@name, @breakpoints);
  66. & when not (@min = ~"") {
  67. @media (min-width: @min) {
  68. @content();
  69. }
  70. }
  71. & when (@min = ~"") {
  72. @content();
  73. }
  74. }
  75. // Media of at most the maximum breakpoint width. No query for the largest breakpoint.
  76. // Makes the @content apply to the given breakpoint and narrower.
  77. //@mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
  78. // $max: breakpoint-max($name, $breakpoints);
  79. // @if $max {
  80. // @media (max-width: $max) {
  81. // @content;
  82. // }
  83. // } @else {
  84. // @content;
  85. // }
  86. //}
  87. #media-breakpoint-down(@name, @content, @breakpoints: @grid-breakpoints) {
  88. @max: breakpoint-max(@name, @breakpoints);
  89. & when not (@max = ~"") {
  90. @media (max-width: @max) {
  91. @content();
  92. }
  93. }
  94. & when (@max = ~"") {
  95. @content();
  96. }
  97. }
  98. // Media that spans multiple breakpoint widths.
  99. // Makes the @content apply between the min and max breakpoints
  100. //@mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
  101. // $min: breakpoint-min($lower, $breakpoints);
  102. // $max: breakpoint-max($upper, $breakpoints);
  103. //
  104. // @if $min != null and $max != null {
  105. // @media (min-width: $min) and (max-width: $max) {
  106. // @content;
  107. // }
  108. // } @else if $max == null {
  109. // @include media-breakpoint-up($lower) {
  110. // @content;
  111. // }
  112. // } @else if $min == null {
  113. // @include media-breakpoint-down($upper) {
  114. // @content;
  115. // }
  116. // }
  117. //}
  118. #media-breakpoint-between(@lower, @upper, @content, @breakpoints: @grid-breakpoints) {
  119. @min: breakpoint-min(@lower, @breakpoints);
  120. @max: breakpoint-max(@upper, @breakpoints);
  121. & when not (@min = ~"") and not (@max = ~"") {
  122. @media (min-width: @min) and (max-width: @max) {
  123. @content();
  124. }
  125. }
  126. & when not (@min = ~"") and (@max = ~"") {
  127. #media-breakpoint-up(@lower, @content, @breakpoints);
  128. }
  129. & when (@min = ~"") and not (@max = ~"") {
  130. #media-breakpoint-down(@upper, @content, @breakpoints);
  131. }
  132. }
  133. // Media between the breakpoint's minimum and maximum widths.
  134. // No minimum for the smallest breakpoint, and no maximum for the largest one.
  135. // Makes the @content apply only to the given breakpoint, not viewports any wider or narrower.
  136. //@mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints) {
  137. // $min: breakpoint-min($name, $breakpoints);
  138. // $max: breakpoint-max($name, $breakpoints);
  139. //
  140. // @if $min != null and $max != null {
  141. // @media (min-width: $min) and (max-width: $max) {
  142. // @content;
  143. // }
  144. // } @else if $max == null {
  145. // @include media-breakpoint-up($name) {
  146. // @content;
  147. // }
  148. // } @else if $min == null {
  149. // @include media-breakpoint-down($name) {
  150. // @content;
  151. // }
  152. // }
  153. //}
  154. #media-breakpoint-only(@name, @content, @breakpoints: @grid-breakpoints) {
  155. @min: breakpoint-min(@name, @breakpoints);
  156. @max: breakpoint-max(@name, @breakpoints);
  157. & when not (@min = ~"") and not (@max = ~"") {
  158. @media (min-width: @min) and (max-width: @max) {
  159. @content();
  160. }
  161. }
  162. & when not (@min = ~"") and (@max = ~"") {
  163. #media-breakpoint-up(@name, @content, @breakpoints);
  164. }
  165. & when (@min = ~"") and not (@max = ~"") {
  166. #media-breakpoint-down(@name, @content, @breakpoints);
  167. }
  168. }