Home Web Front-end HTML Tutorial Combining html and css to implement adaptive code for mobile web pages

Combining html and css to implement adaptive code for mobile web pages

Aug 10, 2018 pm 05:34 PM

The content of this article is about the code that combines HTML and CSS to realize mobile web page adaptation. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

First, I am working on a project recently, writing mobile web pages, mainly about adaptive issues. If you don’t use bootstrap and other front-end frameworks well, and you don’t want to waste time, it’s annoying that you can’t adapt. Here I will introduce my method to you.

is also a combination of many people’s ideas.

1. Add this line of code to the head:

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

This sentence is to automatically set the zoom. However, it cannot fully adapt to all mobile phones, and you are using a browser phone It may be normal during mode debugging, but it is actually abnormal when switching to the real mobile phone. So we still have to make changes.

2. It is recommended that you use rem instead of px when using magin, padding, font-size, and other attributes. You may be unfamiliar with rem. Here I have provided a piece of js code, which can be imported directly. , there is no need to worry about what rem is. Regarding the principle, I will write another blog to introduce it. This blog will talk about how to use it. It is the following js code, which can be put into html when writing a web page. The 640 in the code below refers to the screen width of the mobile phone. Generally speaking, the maximum width of the mobile phone screen on the market is 640px, so i=640 is used here to refer to the maximum width of the screen, the minimum is 320px, and then by citing the following js You can write your own web page. Remember that 1rem=100px in this js is actually for easy conversion. For example, font_size:14px; we can write, font_size:0.14rem.

<script>
!function(n) {
    var e = n.document,
    t = e.documentElement,
    i = 640,
    d = i / 100,
    o = "orientationchange" in n ? "orientationchange": "resize",
    a = function() {
        var n = t.clientWidth || 320;
        n > 640 && (n = 640),
        t.style.fontSize = n / d + "px"
    };
    e.addEventListener && (n.addEventListener(o, a, !1), e.addEventListener("DOMContentLoaded", a, !1))
} (window);
</script>
Copy after login

3. Okay, let me show you the effect of a carousel to illustrate. First, enter the code

<!doctype html>
<html>
<head>
<meta charset="utf-8">
&lt;meta content=&quot;width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0&quot; name=&quot;viewport&quot;&gt;
<title>无标题文档</title>
<script src="images/jquery-1.js"></script>
<script src="images/TouchSlide.js"></script>
<script src="images/rem.js"></script>
<style>
    /* banner 轮播切换 star */
body{margin:0 auto !important;max-width:640px;min-width:320px;background: #fff !important;overflow-x: hidden;}
#banner{position: relative;}
#banner .hd{position: absolute;width: 100%;text-align: center;bottom: 2%;left: 0;}
#banner .hd li{width: 0.2rem;height: 0.2rem;border:1px solid #fff;border-radius: 50%;display: inline-block;margin:0 4px;}
#banner .hd li.on{background: #9e5fd4;border-color:#9e5fd4; }
        
    </style>
    <script type="text/javascript">
    $(function(){
    /*根据banner个数增加下方指示点*/
     n=$(".ibanner .bd li").size();
     for (m=0;m<n;m++){
            $('.ibanner .hd').append('<li></li>');
        }
    /*banner切换*/    
    if($('.ibanner').length){
            TouchSlide({slideCell: "#banner",mainCell: ".bd",autoPlay: true,effect: "leftLoop",interTime:5000});
        }
    
});
    </script>
</head>
<body>
    <!-- 上部轮播开始-->
  <p id="banner" class="ibanner">
    <p class="tempWrap" style="overflow:hidden; position:relative;">
 
     <ul class="bd" style="width: 3840px; position: relative; 
overflow: hidden; padding: 0px; margin: 0px; transition-duration: 200ms;
 transform: translateX(-2560px);">
        <!-- 此处添加伦比图片-->
 
       <li style="display: table-cell; vertical-align: top; width: 
640px;"><a 
href="https://rgtk15.kuaishang.cn/bs/im.htm?cas=113477___235266&fi=115470"><img
 src="images/banner11.jpg"></a></li>
        <li 
style="display: table-cell; vertical-align: top; width: 640px;"><a
 
href="https://rgtk15.kuaishang.cn/bs/im.htm?cas=113477___235266&fi=115470"><img
 src="images/banner12.jpg"></a></li>
        <li 
style="display: table-cell; vertical-align: top; width: 640px;"><a
 
href="https://rgtk15.kuaishang.cn/bs/im.htm?cas=113477___235266&fi=115470"><img
 src="images/banner13.jpg"></a></li>
        <li 
style="display: table-cell; vertical-align: top; width: 640px;"><a
 
href="https://rgtk15.kuaishang.cn/bs/im.htm?cas=113477___235266&fi=115470"><img
 src="images/banner14.jpg"></a></li>
        <li 
style="display: table-cell; vertical-align: top; width: 640px;"><a
 
href="https://rgtk15.kuaishang.cn/bs/im.htm?cas=113477___235266&fi=115470"><img
 src="images/banner15.jpg"></a></li>
      </ul>
    </p>
    <ul class="hd">
    </ul>
  </p>
  <!-- 上部轮播结束--> 
</body>
</html>
Copy after login

Related recommendations:

How to achieve font shadow effect using css attributes? (Code Demonstration)

#How to use css to automatically add a shadow effect when the mouse moves to a div block?

Combining 2D and 3D in HTML and CSS3 to achieve animation effects

The above is the detailed content of Combining html and css to implement adaptive code for mobile web pages. 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 Article Tags

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)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

How do I use HTML5 form validation attributes to validate user input?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

What is the purpose of the <iframe> tag? What are the security considerations when using it?

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

What is the purpose of the <meter> element?

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

What is the purpose of the <datalist> element?

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

What are the best practices for cross-browser compatibility in HTML5?

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

What is the purpose of the <progress> element?

See all articles