Skip to content

Instantly share code, notes, and snippets.

@jfreyre
Created January 30, 2019 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfreyre/46baf813d109d675923155838e850d7c to your computer and use it in GitHub Desktop.
Save jfreyre/46baf813d109d675923155838e850d7c to your computer and use it in GitHub Desktop.
aXBBeV
<div class="container">
woot
</div>
$breakpoints: ( xs: 480px, sm: 768px, md:992px, lg: 1200px );
@mixin respond-above($breakpoint) {
// If the breakpoint exists in the map.
@if map-has-key($breakpoints, $breakpoint) {
// Get the breakpoint value.
$breakpoint-value: map-get($breakpoints, $breakpoint);
// Write the media query.
@media (min-width: $breakpoint-value) {
@content;
}
// If the breakpoint doesn't exist in the map.
}
@else {
// Log a warning.
@warn 'Invalid breakpoint: #{$breakpoint}.';
}
}
@mixin respond-below($breakpoint) {
// If the breakpoint exists in the map.
@if map-has-key($breakpoints, $breakpoint) {
// Get the breakpoint value.
$breakpoint-value: map-get($breakpoints, $breakpoint);
// Write the media query.
@media (max-width: ($breakpoint-value - 1)) {
@content;
}
// If the breakpoint doesn't exist in the map.
}
@else {
// Log a warning.
@warn 'Invalid breakpoint: #{$breakpoint}.';
}
}
@mixin respond-between($lower, $upper) {
// If both the lower and upper breakpoints exist in the map.
@if map-has-key($breakpoints, $lower) and map-has-key($breakpoints, $upper) {
// Get the lower and upper breakpoints.
$lower-breakpoint: map-get($breakpoints, $lower);
$upper-breakpoint: map-get($breakpoints, $upper);
// Write the media query.
@media (min-width: $lower-breakpoint) and (max-width: ($upper-breakpoint - 1)) {
@content;
}
// If one or both of the breakpoints don't exist.
}
@else {
// If lower breakpoint is invalid.
@if (map-has-key($breakpoints, $lower) == false) {
// Log a warning.
@warn 'Your lower breakpoint was invalid: #{$lower}.';
}
// If upper breakpoint is invalid.
@if (map-has-key($breakpoints, $upper) == false) {
// Log a warning.
@warn 'Your upper breakpoint was invalid: #{$upper}.';
}
}
}
.container {
text-align: center;
min-height: 50vw;
@include respond-below(xs) {
background: red;
}
@include respond-between(xs,md) {
background: blue;
}
@include respond-above(md) {
background: yellow;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment