Home Web Front-end CSS Tutorial media media queries in CSS3

media media queries in CSS3

Feb 17, 2017 pm 01:11 PM
css3 media

Media queries are mostly used in responsive web pages.

1. Initialization settings:

In the HTML file, insert a sentence in the tag at the top of the webpage:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
Copy after login

This sentence is to make an initialization setting for the responsive webpage, which mainly includes:

name="viewport": mark the display device as the viewport;

width = device-width: the width is equal to the width of the current device;

initial-scale: the initial scaling ratio (default setting is 1.0);
minimum -scale: The minimum ratio that the user is allowed to zoom to (the default setting is 1.0);
maximum-scale: The maximum ratio the user is allowed to zoom to (the default setting is 1.0);
user-scalable: Whether the user can manually zoom (the default setting is 1.0) no, because we don't want users to zoom in or out of the page).


2. Solve the compatibility problem of IE browser:

Because IE browser (IE8) does not support media in HTML5 and CSS3, the JS file used to solve the compatibility problem of IE browser must be loaded:

<!--[if lt IE 9]>
Copy after login
	<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
Copy after login
	<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
Copy after login
<![endif]-->
Copy after login

The file link address pointed to by the src attribute in the two <script></script> tags is the file at the fixed address. It can be referenced directly from another location without downloading to local reference.

3. Set the rendering mode of IE to the highest:

Nowadays, many people’s IE browsers have been upgraded to IE9 or above. At this time, many strange things will happen. For example, it is IE9 browser now, but the browser’s The document mode is IE8. In order to prevent this situation, we need the following code to make IE’s document mode always up to date:

<meta http-equiv="X-UA-Compatible" content="IE=edge">
Copy after login


Of course there is a more powerful way of writing:

<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
Copy after login


A chrome=1 is added after this code. This is due to Google Chrome Frame (Google Embedded Browser Framework GCF). If the user's computer installs this chrome plug-in, the IE browser in the computer can avoid version factors and use Webkit engine and V8 engine for typesetting and calculation. Of course, if the user does not install this plug-in, this code will cause the IE browser to display the effect in the highest document mode.

4. CSS3 media media query is written as:

@media screen and (max-width: 960px){
	body{
		background: #000;
	}
}
Copy after login
Copy after login


This is a standard way of writing media. In the CSS file, it means: when the page is smaller than 960px, execute the following CSS code. The specific content is ignored for the time being.

For screen in the above code, it means to tell the device to use serif fonts when printing the page, and to use sans-serif fonts when displaying on the screen. At present, many websites directly omit screen, so there is no need to consider the user's need to print web pages, so there is another way of writing:

@media (max-width: 960px){
	body{
		background: #000;
	}
}
Copy after login

In line with the principle of rigorous thinking, I personally will not use this way of writing.
5. CSS3 media query body code combination:


In responsive web page layout, media query code combination needs to be continuously used. The main function is to determine the width of the adapted screen and apply different CSS according to various width conditions. style.

For example, when the screen width is equal to 960px, change the background color of the web page to red:

@media screen and (max-device-width:960px){
	body{
		background:red;
	}
}
Copy after login


For example, when the screen width is maximum 960px (less than 960px), change the background color of the web page to black:

@media screen and (max-width: 960px){
	body{
		background: #000;
	}
}
Copy after login
Copy after login

Like when When the screen width is at least 960px (greater than 960px), change the background color of the web page to orange:

@media screen and (min-width:960px){
	body{
		background:orange;
	}
}
Copy after login

More common is a mixed use, such as when the screen width is between 960px and 1200px, change the background color of the web page to orange Is yellow:

@media screen and (min-width:960px) and (max-width:1200px){
	body{
		background:yellow;
	}
}
Copy after login

6. Overall development idea:

The general idea of ​​using media queries in CSS3 is to determine the width range of the web page in different devices. There may be three such ranges (PC, tablet, mobile phone), and There may be four types (PC, tablet, medium and large screen mobile phone, small screen mobile phone), of course, there may be only two types (tablet, mobile phone, PC side does not need to be used as the object of CSS3 media query when developing a separate version), and for Apply different CSS styles to the required page elements in various width ranges to adapt to various devices.

7. Width issues in responsive web development:

In actual development, it is usually necessary to set the maximum width of responsive web pages. Once the maximum width is ignored, bloated or fragmented web page layout will cause visual flooding, which is what we Often said it looks very low.
Also let’s talk about the width of web pages in current display devices (due to space issues, I won’t start with the Industrial Revolution). The most common widths at present are basically: PC side greater than or equal to 960px (1920px, 1600px, 1440px , 1280px, 1140px, 960px), tablets between 960px and 640px (768px, 640px), and mobile phones below 640px (480px, 320px). The above widths have existed for a long time, and the width of web pages in display devices will remain like this for a long time. In this state, when it comes to responsive web page width design, it is basically enough to consider these dimensions.
8. Summary of all parameters of media media query:

The media query also includes related functions that are not commonly used, as follows:

  • width: browser visible width,

  • height: browser Visual height,

  • device-width: the width of the device screen,

  • device-height: the height of the device screen,

  • orientation: detect whether the device is currently in landscape or portrait mode,

  • aspect-ratio:检测浏览器可视宽度和高度的比例(例如:aspect-ratio:16/9),

  • device-aspect-ratio:检测设备的宽度和高度的比例,

  • color:检测颜色的位数(例如:min-color:32就会检测设备是否拥有32位颜色),

  • color-index:检查设备颜色索引表中的颜色(他的值不能是负数),

  • monochrome:检测单色楨缓冲区域中的每个像素的位数(这个太高级,估计咱很少会用的到),

  • resolution:检测屏幕或打印机的分辨率(例如:min-resolution:300dpi或min-resolution:118dpcm),

  • grid:检测输出的设备是网格的还是位图设备。

9.扩展——在CSS2中同样有媒体查询:

media媒体查询并不是CSS3诞生之后的专用功能,早在CSS2开始就已经支持media,比如:

在HTML文件中的标签中写入这句:

<link rel="stylesheet" type="text/css" media="screen" href="style.css">
Copy after login

以上是CSS2实现的衬线用法,href属性中写入在某单一显示设备中链接的CSS文件,但仅供入门,

如要判断移动设备是否为纵向放置的显示屏,可以这样写:

<link rel="stylesheet" type="text/css" media="screen and (orientation:portrait)" href="style.css">
Copy after login

如要让小于960px的页面执行指定的CSS样式文件,可以这样写:

<link rel="stylesheet" type="text/css" media="screen and (max-width:960px)" href="style.css">
Copy after login

当然,CSS2中的媒体查询方法放到现在并不推荐使用,最大的弊端在于这样会增加页面http的请求次数,增加页面负担,使用CSS3中的媒体查询才是目前的最佳方法。

更多CSS3中的media媒体查询相关文章请关注PHP中文网!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to achieve wave effect with pure CSS3? (code example) How to achieve wave effect with pure CSS3? (code example) Jun 28, 2022 pm 01:39 PM

How to achieve wave effect with pure CSS3? This article will introduce to you how to use SVG and CSS animation to create wave effects. I hope it will be helpful to you!

How to solve the problem that Lenovo computer's checking media cannot be turned on? How to solve the problem that Lenovo computer's checking media cannot be turned on? Feb 12, 2024 am 08:36 AM

Some users encountered the problem that checking media cannot be started when starting a Lenovo laptop, which is confusing. So how to solve the problem of checking media not starting on Lenovo computers? This tutorial will bring you the reasons and solutions for checking media failing to start on Lenovo laptops. Causes: 1. Hard drive damage: If the Lenovo notebook has hard drive damage or failure, it will cause the notebook to display checking media and not boot. The operating system is damaged: If the operating system of a Lenovo notebook is damaged, it will cause the notebook to display checking media and be unable to boot. 2. Restart the computer, press F12 to enter the BIOS, and select the "Startup" item.

Use CSS skillfully to realize various strange-shaped buttons (with code) Use CSS skillfully to realize various strange-shaped buttons (with code) Jul 19, 2022 am 11:28 AM

This article will show you how to use CSS to easily realize various weird-shaped buttons that appear frequently. I hope it will be helpful to you!

How to hide elements in css without taking up space How to hide elements in css without taking up space Jun 01, 2022 pm 07:15 PM

Two methods: 1. Using the display attribute, just add the "display:none;" style to the element. 2. Use the position and top attributes to set the absolute positioning of the element to hide the element. Just add the "position:absolute;top:-9999px;" style to the element.

How to implement lace borders in css3 How to implement lace borders in css3 Sep 16, 2022 pm 07:11 PM

In CSS, you can use the border-image attribute to achieve a lace border. The border-image attribute can use images to create borders, that is, add a background image to the border. You only need to specify the background image as a lace style; the syntax "border-image: url (image path) offsets the image border width inward. Whether outset is repeated;".

It turns out that text carousel and image carousel can also be realized using pure CSS! It turns out that text carousel and image carousel can also be realized using pure CSS! Jun 10, 2022 pm 01:00 PM

How to create text carousel and image carousel? The first thing everyone thinks of is whether to use js. In fact, text carousel and image carousel can also be realized using pure CSS. Let’s take a look at the implementation method. I hope it will be helpful to everyone!

How to enlarge the image by clicking the mouse in css3 How to enlarge the image by clicking the mouse in css3 Apr 25, 2022 pm 04:52 PM

Implementation method: 1. Use the ":active" selector to select the state of the mouse click on the picture; 2. Use the transform attribute and scale() function to achieve the picture magnification effect, the syntax "img:active {transform: scale(x-axis magnification, y Axis magnification);}".

How to set animation rotation speed in css3 How to set animation rotation speed in css3 Apr 28, 2022 pm 04:32 PM

In CSS3, you can use the "animation-timing-function" attribute to set the animation rotation speed. This attribute is used to specify how the animation will complete a cycle and set the speed curve of the animation. The syntax is "element {animation-timing-function: speed attribute value;}".

See all articles