css中的媒體查詢
CSS | 媒體查詢 (CSS | Media Queries)
Creating a web page is not an easy task as it requires loads of content and data so that it becomes strongly responsive to the users. To do that various contents are even added e.g.: resources, informative content, blogs, line of codes, etc. but most importantly the factor that attracts users to any web page is media.
創建網頁不是一件容易的事,因為它需要加載內容和數據,以使其對用戶產生強烈的響應。 為此,甚至添加了各種內容,例如:資源,信息內容,博客,代碼行等,但最重要的是,吸引用戶訪問任何網頁的因素是媒體。
Trivia:
瑣事:
On that note let us talk about media queries. Media queries are the tools to implement media on your web page. For example, the @media rule which was introduced in CSS2 helped in making it possible to define style rules for different media types.
關于這一點,讓我們談談媒體查詢。 媒體查詢是在網頁上實現媒體的工具 。 例如,CSS2中引入的@media規則有助于為不同的媒體類型定義樣式規則。
But CSS2's idea did not get much support as it got obsolete. So CSS3 came into play by extending the CSS2 media type ideas. In this idea, they did not focus on the type of device but its capability.
但是CSS2的想法由于已經過時而沒有得到太多的支持。 因此,CSS3通過擴展CSS2媒體類型概念而發揮作用。 在這種想法下,他們并不關注設備的類型,而是關注其功能。
Purpose:
目的:
Media Queries are used to check many things also,
媒體查詢還用于檢查許多內容,
Width as well as the height of the viewport
視口的寬度和高度
Width and height of the device as well orientation (whether the tablet/phone is in landscape or portrait mode?)
設備的寬度和高度以及方向(平板電腦/手機處于橫向還是縱向模式?)
Resolution of the device
設備分辨率
In fact, using media queries is a great practice in order to deliver tailored style desktops, laptops, phones, etc.
實際上,使用媒體查詢是提供定制樣式的臺式機,筆記本電腦,電話等的好習慣。
Media Queries come in with a media type and can have one or more expressions like true or false.
媒體查詢帶有一種媒體類型,可以有一個或多個表達式,如true或false。
Ways to implement:
實施方式:
There are various ways to implement Media Queries,
有多種方法可以實施媒體查詢,
One way is to have a backup CSS section right underneath your style sheet.
一種方法是在樣式表的下面有一個備用CSS部分。
So if someone wishes to include Media type files into your web page then one must opt for Media Queries.
因此,如果有人希望將媒體類型文件包含在您的網頁中,則必須選擇“媒體查詢”。
It is an enhanced version of @media that was earlier used in CSS2. So this property might come in handy to one and all who wish to create responsive and presentable web pages.
它是CSS2之前使用的@media的增強版本。 因此,此屬性可能對希望創建響應式和可呈現網頁的所有人都派上用場。
No one wishes to stick to the olde tools for so long, so why not adapt to something better?
沒有人愿意長期使用舊工具,為什么不適應更好的東西呢?
And, that better is Media Queries.
而且,更好的是媒體查詢。
There are various CSS3 Media Types and with the help of the table mentioned below, it can be easily understood.
有多種CSS3媒體類型,借助下面提到的表格,可以很容易地理解它。
Syntax:
句法:
@media not | only mediatype and(expressions) {
//CSS CODE
}
Example:
例:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: green;
}
@media screen and (min-width: 520px) {
body {
background-color: pink;
}
}
</style>
</head>
<body>
<p>Media query will only if the media type is screen and viewport is 520px wide or wider.</p>
</body>
</html>
Output
輸出量
In the above example, the media type is screen and min width is 520px.
在上面的示例中,媒體類型為screen,最小寬度為520px 。
Other values,
其他值
Value | Description |
---|---|
all | It is for all media type devices |
It is for printers | |
screen | It is for computer screens, tablets, smart-phones etc. |
speech | It is for screen readers that "reads" the page out. |
值 | 描述 |
---|---|
所有 | 適用于所有媒體類型的設備 |
打印 | 適用于打印機 |
屏幕 | 它用于計算機屏幕,平板電腦,智能手機等。 |
言語 | 它是供屏幕閱讀器“讀取”頁面的。 |
翻譯自: https://www.includehelp.com/code-snippets/media-queries-in-css.aspx
css中的媒體查詢