--- title: ابزار تست رسانه پاسخگو slug: Web/CSS/Media_Queries/ابزار-تست-رسانه-پاسخگو translation_of: Web/CSS/Media_Queries/Using_media_queries ---
ابزارهای تست پاسخگو بسیار مفید هستند، زمانی که می خواهید سبک CSS را طبق نوع کلی دستگاه (مانند اندازه چاپ در مقابل سایز صفحه نمایش)، ویژگی های خاص (مانند عرض مرورگر رسانه) یا محیط (مانند شرایط نور محیط) استفاده کنید. با انواع مختلفی از دستگاه های متصل به اینترنت که امروزه در دسترس ما هستند و با وجود ابزارهای چند رسانه ای با اندازه های بیشمار، ابزار تست پاسخگو رسانه ها یک ابزار حیاتی برای ساخت وب سایت ها و برنامه هایی با سیستم طراحی حرفه ای جهت کار بر روی هر سخت افزاری که کاربران ممکن است با آن کار کنند، بشمار میرود.
انواع رسانه ها دسته کلی یک دستگاه مشخص را توصیف می کنند. اگر چه وبسایت ها معمولا با صفحه نمایش طراحی شده اند، ممکن است بخواهید سبکهایی را ایجاد کنید که دستگاه هایی خاص مانند چاپگرها یا صفحه نمایش مبتنی بر صدا را هدف قرار میدهند. به عنوان مثال، این کد CSS چاپگر ها را هدف قرار میدهد :
@media print { ... }
همچنین می توانید به راحتی چندین دستگاه را هدف قرار دهید. به عنوان مثال، دستور@media
با استفاده از دو ابزار رسانه پاسخگو، صفحه نمایش و همینطور چاپگر را هدف قرار میدهد:
@media screen, print { ... }
برای مشاهده لیستی از رسانه های مختلف روی انواع رسانه کلید کنید. از آنجایی که دستگاهها را فقط در شرایط بسیار وسیع توصیف می کنند، فقط تعداد کمی از آنها در دسترس هستند؛ برای اختصاص ویژگی های خاص تر، از ویژگی های رسانه استفاده کنید.
Media features describe the specific characteristics of a given {{glossary("user agent")}}, output device, or environment. For instance, you can apply specific styles to widescreen monitors, computers that use mice, or to devices that are being used in low-light conditions. This example applies styles when the user's primary input mechanism (such as a mouse) can hover over elements:
@media (hover: hover) { ... }
Many media features are range features, which means they can be prefixed with "min-" or "max-" to express "minimum condition" or "maximum condition" constraints. For example, this CSS will apply styles only if your browser's {{glossary("viewport")}} is equal to or narrower than 12,450 pixels:
@media (max-width: 12450px) { ... }
If you create a media feature query without specifying a value, the nested styles will be used as long as the feature's value is non-zero. For example, this CSS will apply to any device with a color screen:
@media (color) { ... }
If a feature doesn't apply to the device on which the browser is running, expressions involving that media feature are always false. For example, the styles nested inside the following query will never be used, because no speech-only device has a screen aspect ratio:
@media speech and (aspect-ratio: 11/5) { ... }
For more media feature examples, please see the reference page for each specific feature.
Sometimes you may want to create a media query that depends on multiple conditions. This is where the logical operators come in: not
, and
, and only
. Furthermore, you can combine multiple media queries into a comma-separated list; this allows you to apply the same styles in different situations.
In the previous example, we've already seen the and
operator used to group a media type with a media feature. The and
operator can also combine multiple media features into a single media query. The not
operator, meanwhile, negates a media query, basically reversing its normal meaning. The only
operator prevents older browsers from applying the styles.
Note: In most cases, the all
media type is used by default when no other type is specified. However, if you use the not
or only
operators, you must explicitly specify a media type.
and
The and
keyword combines a media feature with a media type or other media features. This example combines two media features to restrict styles to landscape-oriented devices with a width of at least 30 ems:
@media (min-width: 30em) and (orientation: landscape) { ... }
To limit the styles to devices with a screen, you can chain the media features to the screen
media type:
@media screen and (min-width: 30em) and (orientation: landscape) { ... }
You can use a comma-separated list to apply styles when the user's device matches any one of various media types, features, or states. For instance, the following rule will apply its styles if the user's device has either a minimum height of 680 pixels or is a screen device in portrait mode:
@media (min-height: 680px), screen and (orientation: portrait) { ... }
Taking the above example, if the user had a printer with a page height of 800 pixels, the media statement would return true because the first query would apply. Likewise, if the user were on a smartphone in portrait mode with a viewport height of 480 pixels, the second query would apply and the media statement would still return true.
not
The not
keyword inverts the meaning of an entire media query. It will only negate the specific media query it is applied to. (Thus, it will not apply to every media query in a comma-separated list of media queries.) The not
keyword can't be used to negate an individual feature query, only an entire media query. The not
is evaluated last in the following query:
@media not all and (monochrome) { ... }
... so that the above query is evaluated like this:
@media not (all and (monochrome)) { ... }
... rather than like this:
@media (not all) and (monochrome) { ... }
As another example, the following media query:
@media not screen and (color), print and (color) { ... }
... is evaluated like this:
@media (not (screen and (color))), print and (color) { ... }
only
The only
keyword prevents older browsers that do not support media queries with media features from applying the given styles. It has no effect on modern browsers.
<link rel="stylesheet" media="only screen and (color)" href="modern-styles.css" />
Mozilla offers several Gecko-specific media features. Some of these may be proposed as official media features in the future.
For WebKit-specific media features, please see the reference page for each specific feature.
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatChrome("21")}} | {{CompatGeckoDesktop("1.9.1")}} | {{CompatIE("9.0")}} | {{CompatOpera("9")}} | {{CompatSafari("4")}} |
grid |
{{CompatUnknown}} | {{CompatNo}} [1] | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
resolution |
{{CompatChrome("29")}} | {{CompatGeckoDesktop("1.9.1")}} [2] {{CompatGeckoDesktop("8.0")}} [3] |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
scan |
{{CompatUnknown}} | {{CompatNo}}[4] | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
-webkit-min-device-pixel-ratio , -webkit-max-device-pixel-ratio |
{{CompatVersionUnknown}} | {{CompatNo}}[7] | {{CompatNo}} | {{CompatVersionUnknown}} | {{CompatUnknown}} |
-webkit-transform-3d |
{{CompatVersionUnknown}}[5] | {{CompatGeckoDesktop("49")}}[6] | {{CompatUnknown}} | {{CompatVersionUnknown}}[5] | {{CompatSafari("1.0")}}[5] |
-webkit-transform-2d |
{{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatVersionUnknown}}[5] | {{CompatSafari("1.0")}}[5] |
-webkit-transition |
{{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatVersionUnknown}}[5] | {{CompatSafari("1.0")}}[5] |
display-mode |
{{CompatUnknown}} | {{CompatGeckoDesktop("47")}}[8] | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
grid |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
resolution |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
scan |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
-webkit-min-device-pixel-ratio , -webkit-max-device-pixel-ratio |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
-webkit-transform-3d |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
-webkit-transform-2d |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
-webkit-transition |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
display-mode |
{{CompatUnknown}} | {{CompatGeckoMobile(47)}}[8] | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
[1] grid
media type is not supported.
[2] Supports {{cssxref("<integer>")}} values only.
[3] Supports {{cssxref("<number>")}} values, as per the spec.
[4] tv
media type is not supported.
[5] See {{WebKitBug(22494)}}.
[6] Implemented for Web compatibility reasons in Gecko 46.0 {{geckoRelease("46.0")}} behind the preference layout.css.prefixes.webkit
defaulting to false
. See {{bug(1239799)}}. Since Gecko 49.0 {{geckoRelease("49")}} layout.css.prefixes.webkit
defaults to true
.
[7] Implemented as aliases for min--moz-device-pixel-ratio
and max--moz-device-pixel-ratio
for Web compatibility reasons in Gecko 45.0 {{geckoRelease("45.0")}} (see {{bug(1176968)}}) behind the preferences layout.css.prefixes.webkit
and layout.css.prefixes.device-pixel-ratio-webkit
, defaulting to false
. Since Gecko 49.0 {{geckoRelease("49")}} layout.css.prefixes.webkit
defaults to true
.
[8] Only the fullscreen
and browser
values of display-mode
were supported in 47. minimal-ui
and standalone
were added in Firefox 57.