Media Queries
JavaScriptCSS media queries allow us to adjust the display and orientation of content at different screen sizes.
Default Media Queries
Foundation for Sites has three core breakpoints:
- Small: any screen.
- Medium: any screen 640 pixels or wider.
- Large: any screen 1024 pixels or wider.
Many components can be modified at different screen sizes using special breakpoint classes. The grid is the most obvious example. In the code below, the left-hand column is six columns wide on small screens, hence .small-6
. On medium-sized screens, the class .medium-4
overrides the small style, changing the column to be four wide.
<div class="row">
<div class="small-6 medium-4 columns"></div>
<div class="small-6 medium-8 columns"></div>
</div>
If you're using the CSS version of Foundation, use these media queries to imitate the three core breakpoints:
/* Small only */
@media screen and (max-width: 39.9375em) {}
/* Medium and up */
@media screen and (min-width: 40em) {}
/* Medium only */
@media screen and (min-width: 40em) and (max-width: 63.9375em) {}
/* Large and up */
@media screen and (min-width: 64em) {}
/* Large only */
@media screen and (min-width: 64em) and (max-width: 74.9375em) {}
Upgrading from Foundation 5
In Foundation 5, breakpoints were accessed using a series of Sass variables named $small-up
, $small-only
, $medium-only
, and so on. In Foundation 6, this method of writing media queries has been replaced with a dedicated breakpoint mixin, described below. The legacy variables will be removed in Foundation 6.3.
To upgrade your existing media queries, replace rulesets like this:
@media #{$medium-only} {
}
With this:
@include breakpoint(medium only) {
}
Changing the Breakpoints
If you're using the Sass version of Foundation, the default breakpoints can be changed. The names of the breakpoints, and their widths, are stored in a $breakpoints
variable in the settings file.
$breakpoints: (
small: 0px,
medium: 640px,
large: 1024px,
xlarge: 1200px,
xxlarge: 1440px,
);
Even though the above values are in pixels, they're converted to ems at the end for use in media queries.
Changing the widths of any of the breakpoints is as easy as changing the pixel values in this map. Note that here there are two extra breakpoints: xlarge
and xxlarge
. We don't use these for any components, and also don't output any CSS classes that use them by default.
You can change that by modifying the $breakpoint-classes
variable in your settings file. This is a list of breakpoint names. Adding or removing names from the list will change the CSS class output. It looks like this by default:
$breakpoint-classes: (small medium large);
For example, to get .xlarge
classes in your CSS, for use in the grid, Menu, and more, just add it to the end of the list:
$breakpoint-classes: (small medium large xlarge);
Sass
The Breakpoint Mixin
Our breakpoint()
mixin makes it easy to write media queries. You can use the named breakpoints, or a custom pixel, rem, or em value.
To use the mixin, call it with @include
, and then include the CSS content you want inside the curly braces.
.element {
// Only affects medium screens and larger
@include breakpoint(medium) {
// All CSS in here goes inside the media query
}
}
The behavior of the media query can be changed by adding the keyword down
or only
after the breakpoint value, separated by a space.
.element {
// Only affects medium screens and smaller
@include breakpoint(medium down) { }
// Only affects medium screens, not small or large
@include breakpoint(medium only) { }
}
It's also possible to pass in custom values. You can enter a pixel, rem, or em value—all values are converted to em at the end.
.element {
// Converted to 20em
@include breakpoint(320px) { }
// Unitless values are assumed to be pixels
@include breakpoint(320) { }
// Converted to 40em
@include breakpoint(40rem) { }
}
Lastly, there are three special media queries that are not width-based: portrait
, landscape
, and retina
. Using these keywords with the breakpoint()
mixin will output a media query for device orientation or pixel density, rather than screen width.
.element {
@include breakpoint(landscape) {
// CSS for landscape-oriented devices only
}
@include breakpoint(retina) {
// CSS for high-resolution displays only
}
}
Breakpoint Function
The functionality of the breakpoint()
mixin comes from an internal function, also called breakpoint()
. If you want to write your own media queries, you can use the breakpoint()
function to access the logic of the mixin directly.
@media screen and #{breakpoint(medium)} {
// Medium and up styles
}
This can be used to combine multiple media queries together.
@media screen and #{breakpoint(medium)} and #{breakpoint(xlarge down)} {
// Medium to extra large styles
}
JavaScript
Working with Media Queries
The Foundation JavaScript includes a set of helper functions for working with media queries. They're all on the Foundation.MediaQuery
object.
Get the name of the current breakpoint with MediaQuery.current
.
Foundation.MediaQuery.current // => 'small', 'medium', etc.
To see if the screen is currently a certain breakpoint or larger, use MediaQuery.atLeast
.
if (Foundation.MediaQuery.atLeast('medium')) {
// True if medium or large
// False if small
}
To see if the screen is currently a certain breakpoint, use MediaQuery.is
.
if (Foundation.MediaQuery.is('small only')) {
// True if small
// False if medium or large
}
To get the media query of a breakpoint, use MediaQuery.get
.
Foundation.MediaQuery.get('medium') // => only screen and (min-width: 640px)
Watching for Breakpoint Changes
The media query helper broadcasts an event on the window every time the breakpoint changes. We use this internally with plugins like Interchange to detect a shift in breakpoint. You can also subscribe to the event yourself.
$(window).on('changed.zf.mediaquery', function(event, newSize, oldSize) {
// newSize is the name of the now-current breakpoint, oldSize is the previous breakpoint
});
Sass Reference
Variables
The default styles of this component can be customized using these Sass variables in your project's settings file.
Name | Type | Default Value | Description |
---|---|---|---|
$breakpoints |
Map |
small: 0 |
A list of named breakpoints. You can use these with the |
$print-breakpoint |
Keyword | large |
The largest named breakpoint in which to include print as a media type |
$breakpoint-classes |
List | small medium large |
All of the names in this list will be output as classes in your CSS, like |
Mixins
We use these mixins to build the final CSS output of this component. You can use the mixins yourself to build your own class structure out of our components.
breakpoint
@include breakpoint($value) { }
Wraps a media query around the content you put inside the mixin. This mixin accepts a number of values:
- If a string is passed, the mixin will look for it in the
$breakpoints
map, and use a media query there. - If a pixel value is passed, it will be converted to an em value using
$global-font-size
as the base. - If a rem value is passed, the unit will be changed to em.
- If an em value is passed, the value will be used as-is.
Parameter | Type | Default Value | Description |
---|---|---|---|
$value |
Keyword or Number | None |
Breakpoint name, or px, rem, or em value to process. |
Functions
breakpoint
breakpoint($val)
Generates a media query string matching the input value. Refer to the documentation for the breakpoint()
mixin to see what the possible inputs are.
Parameter | Type | Default Value | Description |
---|---|---|---|
$val |
Keyword or Number | small |
Breakpoint name, or px, rem, or em value to process. |
JavaScript Reference
Methods
atLeast
$('#element').foundation('atLeast', size);
Checks if the screen is at least as wide as a breakpoint.
Name | Type | Description |
---|---|---|
size |
String | Name of the breakpoint to check. |
is
$('#element').foundation('is', size);
Checks if the screen matches to a breakpoint.
Name | Type | Description |
---|---|---|
size |
String | Name of the breakpoint to check, either 'small only' or 'small'. Omitting 'only' falls back to using atLeast() method. |
get
$('#element').foundation('get', size);
Gets the media query of a breakpoint.
Name | Type | Description |
---|---|---|
size |
String | Name of the breakpoint to get. |