Home Web Front-end H5 Tutorial How does html5 mobile page adapt to the screen? Four ways to adapt html5 pages to mobile phone screens

How does html5 mobile page adapt to the screen? Four ways to adapt html5 pages to mobile phone screens

Aug 20, 2018 pm 01:48 PM
Adaptive

The content of this article is about how to use background-orgin in css3 (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Use the meta tag: viewport

A commonly used method for H5 mobile page adaptation. In theory, using this tag can adapt to all screen sizes. , but each device interprets the tag differently and supports it differently, making it incompatible with all browsers or systems.

viewport is the visible area of ​​the user web page. Translated into Chinese, it can be called "view area".

Mobile browsers place the page in a virtual "window" (viewport). Usually this virtual "window" (viewport) is wider than the screen, so that each web page does not need to be squeezed into a small size. In a window (which would break the layout of webpages that are not optimized for mobile browsers), users can pan and zoom to see different parts of the webpage.

viewport tag and its attributes:

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

Detailed introduction of each attribute:

Attribute name Value describe
width Positive integer or device-width Define the width of the viewport in pixels
height Positive integer or device-height Define the height of the viewport, the unit is pixels, generally not used
initial-scale [0.0-10.0] Define initial scaling value
minimum-scale [0.0-10.0] Define the minimum reduction ratio, which must be less than or equal to the maximum-scale setting
maximum-scale [0.0-10.0] Define the maximum magnification ratio, which must be greater than or equal to the minimum-scale setting
user-scalable yes/no Define whether users are allowed to manually zoom the page, the default value is yes

2. Use css3 unit rem

rem is a new relative unit (root em, root em) in CSS3. Use rem as When setting the font size of an element, it is a relative size, but it is only relative to the HTML root element. Through it, you can adjust all font sizes proportionally by modifying only the root element, and you can avoid the chain reaction of compounding font sizes layer by layer.

Currently, all browsers except IE8 and earlier versions support rem. For browsers that do not support it, write an additional absolute unit declaration. These browsers ignore font sizes set with rem. Here is an example:

p {font-size:14px; font-size:.875rem;}
Copy after login

The default font-size of HTML is 16px, that is, 1rem=16px. If the width of a p is 32px, you can set it to 2rem.

Normally, in order to facilitate the calculation of values, 62.5%, which is the default 10px, is used as the base. Of course, this base can be any value, depending on the specific situation. The setting method is as follows:

Html{font-size:62.5%(10/16*100%)}
Copy after login

The specific definition of rules under different screens, that is, the way to define the base: It can be defined through CSS, and different base values ​​can be defined in different width ranges. Of course, it can also be defined once through js as follows:

<script type="text/javascript">
   (function (doc, win) {
      var docEl = doc.documentElement,
        resizeEvt = &#39;orientationchange&#39; in window ? &#39;orientationchange&#39; : &#39;resize&#39;,
        recalc = function () {
          var clientWidth = docEl.clientWidth;
          if (!clientWidth) return;
          docEl.style.fontSize = 20 * (clientWidth / 320) + &#39;px&#39;;//其中“20”根据你设置的html的font-size属性值做适当的变化
        };
      if (!doc.addEventListener) return;
      win.addEventListener(resizeEvt, recalc, false);
      doc.addEventListener(&#39;DOMContentLoaded&#39;, recalc, false);
    })(document, window);
</script>
Copy after login

3. Use media query

Media query is also a CSS3 method. The problem we want to solve is to adapt to the mobile phone screen. This media query is exactly to solve this problem. And born.

The function of media query is to set different css styles for different media. The "media" here includes page size, device screen size, etc.

For example: If the browser window is smaller than 500px, the background will change to light blue:

@media only screen and (max-width: 500px) {
    body {
        background-color: lightblue;
    }
}
Copy after login

4. Use percentage

Percentage refers to Parent element, all percentages are like this. If the width of the child element is 50%, then the width of the parent element is 100%;

So the default width of the body is the screen width (in PC, it refers to the browser width). The descendant elements can be positioned by percentage (or specify the size). , this is only suitable for pages with simple layouts, and it is difficult to implement complex pages.

Recommended related articles:

How to automatically adapt HTML web pages to mobile phone screens_html/css_WEB-ITnose

html5 under development viewport screen adaptation

The above is the detailed content of How does html5 mobile page adapt to the screen? Four ways to adapt html5 pages to mobile phone screens. For more information, please follow other related articles on the PHP Chinese website!

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 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 configure content adaptive brightness on Windows 11 How to configure content adaptive brightness on Windows 11 Apr 14, 2023 pm 12:37 PM

Adaptive brightness is a feature on Windows 11 computers that adjusts the brightness level of your screen based on the content being displayed or lighting conditions. Since some users are still getting used to Windows 11's new interface, Adaptive Brightness can't be easily found, and some even say the Adaptive Brightness feature is missing on Windows 11, so this tutorial will clear it all up. For example, if you're watching a YouTube video and the video suddenly shows a dark scene, Adaptive Brightness will make the screen brighter and increase the contrast level. This is different from auto-brightness, which is a screen setting that allows your computer, smartphone, or device to adjust brightness levels based on ambient lighting. There is a special one in the front camera

How to build an adaptive mobile interface with Vue? How to build an adaptive mobile interface with Vue? Jun 27, 2023 am 11:05 AM

With the popularity of mobile Internet, more and more websites and applications need to consider the mobile experience. As a popular front-end framework, Vue has responsive layout and adaptive capabilities, which can well help us build adaptive mobile interfaces. This article will introduce how to use Vue to build an adaptive mobile interface. Using rem instead of px as the unit and using px as the unit in the mobile interface may result in inconsistent display effects on different devices. Therefore, it is recommended to use rem instead of px as the unit. rem is relative

How to implement adaptive image size using CSS Viewport units vmin and vw How to implement adaptive image size using CSS Viewport units vmin and vw Sep 13, 2023 am 08:18 AM

How to use CSSViewport units vmin and vw to implement adaptive image size. In web design, we often encounter situations where images need to adapt to the screen size. To achieve this goal, CSS provides a powerful unit - the viewport unit. Among them, vmin represents the percentage of the smaller side of the viewport width, and vw represents the percentage of the viewport width. Therefore, we can use these two units to achieve the effect of adaptive image size. The specifics will be introduced below

Can vue be adaptive? Can vue be adaptive? Dec 30, 2022 pm 03:25 PM

Vue can achieve self-adaptation. The methods to achieve self-adaptation are: 1. Install the "scale-box" component through the "npm install" or "yarn add" command, and use "scale-box" to achieve adaptive scaling; 2. Through Set the device pixel ratio to achieve self-adaptation; 3. Set the zoom attribute through JS to adjust the zoom ratio to achieve self-adaptation.

CSS Viewport: How to use vmax and vw to implement adaptive text width CSS Viewport: How to use vmax and vw to implement adaptive text width Sep 13, 2023 am 10:16 AM

CSSViewport: How to use vmax and vw to implement adaptive text width. With the popularity of mobile devices, responsive design has become an important concept in web design. Among them, adaptive text width to maintain consistent display effects under different screen sizes is an important technology. This article will introduce how to use CSSViewport units, especially vmax and vw units, to implement adaptive text width. In addition to theoretical explanations, we will also provide specific

How to create a responsive website layout using HTML, CSS and jQuery How to create a responsive website layout using HTML, CSS and jQuery Oct 27, 2023 am 11:06 AM

How to create an adaptive website layout using HTML, CSS, and jQuery In today’s Internet age, adaptive layout of websites has become an essential requirement. The adaptive layout of the website can enable the website to display a good user experience on different devices and adapt to devices of different screen sizes, such as computers, tablets, and mobile phones. This article will introduce how to use HTML, CSS and jQuery to create a responsive website layout, and provide specific code examples. Create website skeleton using HTML

Adaptive Server in PHP8.0 Adaptive Server in PHP8.0 May 14, 2023 pm 01:10 PM

On November 26, 2020, the PHP team officially released the PHP 8.0 version. Compared with the previous version, PHP 8.0 brings many new features and improvements. One of the features worthy of attention is the adaptive server. This article will introduce the concept of adaptive server in PHP8.0 and its advantages. In previous PHP versions, developers could use PHP's own server (such as PHP-FPM, Apache) to run their own code. However, the disadvantages of these servers

Implement adaptive website design using PHP Implement adaptive website design using PHP Jun 22, 2023 pm 05:50 PM

Website design is becoming more and more important in the current Internet era. Designers and developers must consider issues such as different device resolutions, different screen sizes, and different operating systems. And these variables make website adaptability even more necessary. PHP is a popular programming language that can be used to develop responsive websites. In this article, we’ll introduce ways to implement responsive website design using PHP and provide a few practical tips. What is responsive website design? Adaptive website design refers to the

See all articles