CSS web page responsive layout to automatically adapt to PC/Pad/Phone devices

CSS web page responsive layout to automatically adapt to PC/Pad/Phone devices

Preface

There are many devices nowadays, including PC, iPad, mobile phone, smart watch and smart TV. If there is no responsive layout, then the computer web page will be displayed on a mobile phone or iPad, the experience will be very poor, the operation will be inconvenient, and the visual fatigue will occur. Therefore, we must have a responsive layout when developing web pages.

index

<!DOCTYPE html>
<html>
<head>
    <title>Responsive Layout</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="content">
        <div id="header">Header</div>
        <div id="left">Left side</div>
        <div id="center">Middle</div>
        <div id="right">Right side</div>
        <div id="footer">Bottom</div>
    </div>
</body>
</html>

CSS

*{
    margin: 0;
    padding: 0;
}

/*Adapt to PC width greater than 1000px*/
@media screen and (min-width: 1000px) {
    #content{
        width: 960px;
        margin:0 auto;
    }

    #header{
        width: 100%;
        line-height: 100px;
        float: left;
        background: #333;
        color: #fff;
        text-align: center;
        font-size: 30px;
        margin-bottom: 10px;
    }

    #left{
        width: 200px;
        line-height: 400px;
        text-align: center;
        background: #333;
        float: left;
        font-size: 30px;
        color: #fff;
        margin-right: 10px;
    }

    #center{
        width: 540px;
        line-height: 400px;
        text-align: center;
        background: #333;
        float: left;
        font-size: 30px;
        color: #fff;
    }

    #right{
        width: 200px;
        line-height: 400px;
        text-align: center;
        background: #333;
        float: right;
        font-size: 30px;
        color: #fff;
    }

    #footer{
        width: 960px;
        height: 200px;
        background: #333;
        color: #fff;
        line-height: 200px;
        font-size: 30px;
        text-align: center;
        float: left;
        margin-top: 10px;
    }
}


/*Adapt to pad width between 640-1000*/
@media screen and (min-width: 640px) and (max-width: 1000px) {
    #content{
        width: 600px;
        margin:0 auto;
    }

    #header{
        width: 100%;
        line-height: 100px;
        float: left;
        background: #333;
        color: #fff;
        text-align: center;
        font-size: 30px;
        margin-bottom: 10px;
    }

    #left{
        width: 200px;
        line-height: 400px;
        text-align: center;
        background: #333;
        float: left;
        font-size: 30px;
        color: #fff;
        margin-right: 10px;
    }

    #center{
        width: 390px;
        line-height: 400px;
        text-align: center;
        background: #333;
        float: left;
        font-size: 30px;
        color: #fff;
    }

    #right{
        width: 600px;
        line-height: 300px;
        text-align: center;
        background: #333;
        float: left;
        font-size: 30px;
        color: #fff;
        margin-top: 10px;
    }

    #footer{
        width: 600px;
        height: 200px;
        background: #333;
        color: #fff;
        line-height: 200px;
        font-size: 30px;
        text-align: center;
        float: left;
        margin-top: 10px;
    }
}


/*Adapt to mobile terminal width less than 640*/
@media screen and (max-width: 639px) {
    #content{
        width: 400px;
        margin:0 auto;
    }

    #header{
        width: 100%;
        line-height: 100px;
        float: left;
        background: #333;
        color: #fff;
        text-align: center;
        font-size: 30px;
        margin-bottom: 10px;
    }

    #left{
        width: 100%;
        line-height: 150px;
        text-align: center;
        background: #333;
        float: left;
        font-size: 30px;
        color: #fff;
        margin-bottom: 10px;
    }

    #center{
        width: 100%;
        line-height: 300px;
        text-align: center;
        background: #333;
        float: left;
        font-size: 30px;
        color: #fff;
    }

    #right{
        width: 100%;
        line-height: 150px;
        text-align: center;
        background: #333;
        float: left;
        font-size: 30px;
        color: #fff;
        margin-top: 10px;
    }

    #footer{
        width: 100%;
        height: 200px;
        background: #333;
        color: #fff;
        line-height: 200px;
        font-size: 30px;
        text-align: center;
        float: left;
        margin-top: 10px;
    }
}

Use @media screen and (limited scope) to control the layout of the web page, for example

min-width represents the minimum limit, and max-width represents the maximum limit.

PC

Pad end

Phone

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  WeChat Mini Programs Implement Star Rating

>>:  Font selection problem of copyright symbol in Html (how to make copyright symbol more beautiful)

Recommend

Sample code for deploying ELK using Docker-compose

environment Host IP 192.168.0.9 Docker version 19...

Let's talk in detail about the difference between unknown and any in TypeScript

Table of contents Preface 1. unknown vs any 2. Th...

How to use mysqladmin to get the current TPS and QPS of a MySQL instance

mysqladmin is an official mysql client program th...

How to deploy k8s in docker

K8s k8s is a cluster. There are multiple Namespac...

HTML commonly used meta encyclopedia (recommended)

The Meta tag is an auxiliary tag in the head area...

The visual design path of the website should conform to user habits

Cooper talked about the user's visual path, w...

How to monitor mysql using zabbix

Zabbix deployment documentation After zabbix is ​...

How does Vue3's dynamic components work?

Table of contents 1. Component Registration 1.1 G...

Mycli is a must-have tool for MySQL command line enthusiasts

mycli MyCLI is a command line interface for MySQL...

How to use Vue to develop public account web pages

Table of contents Project Background start Create...

Zabbix redis automatic port discovery script returns json format

When we perform automatic discovery, there is alw...

How to remotely log in to the MySql database?

Introduction: Sometimes, in order to develop a pr...

MySQL scheduled task implementation and usage examples

This article uses examples to illustrate the impl...