--- title: Media queries slug: Web/CSS/Media_Queries tags: - CSS - CSS3 Media Queries - Media Queries - NeedsTranslation - Overview - Reference - Responsive Design - TopicStub translation_of: Web/CSS/Media_Queries ---
Media Queries are a key component of responsive design, which make it possible for CSS to adapt based on various parameters or device characteristics. For example, a media query can apply different styles if the screen is smaller than a certain size, or based on whether the user is holding their device in portrait or landscape mode. The {{cssxref("@media")}} at-rule is used to conditionally apply styles to a document.
In addition, the media query syntax is also used in other contexts, such as in the {{HTMLElement("source")}} element's {{htmlattrxref("media", "source")}} attribute, which can be set to a media query string to use to determine whether or not to use that source when selecting the specific image to use in a {{HTMLElement("picture")}} element.
In addition, the {{domxref("Window.matchMedia()")}} method can be used to test the window against a media query. You can also use {{domxref("MediaQueryList.addListener()")}} to be notified whenever the state of the queries changes. With this functionality, your site or app can respond to changes in the device configuration, orientation, or state.
You can learn more about programmatically using media queries in Testing media queries.
Specification | Status | Comment |
---|---|---|
{{SpecName('CSS3 Conditional')}} | {{Spec2('CSS3 Conditional')}} | |
{{SpecName('CSS4 Media Queries')}} | {{Spec2('CSS4 Media Queries')}} | |
{{SpecName('CSS3 Media Queries')}} | {{Spec2('CSS3 Media Queries')}} | |
{{SpecName('CSS2.1', 'media.html')}} | {{Spec2('CSS2.1')}} | Initial definition |
{{CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1.0 | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(1.7)}} | 9.0 | 9.2 | 1.3 |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 1.0 | {{CompatVersionUnknown}} | {{CompatGeckoMobile(1.7)}} | 9.0 | 9.0 | 3.1 |