Table of Contents
演示:12列布局
演示:表单ui
演示:表格
Home Web Front-end HTML Tutorial Css3媒体查询使用_html/css_WEB-ITnose

Css3媒体查询使用_html/css_WEB-ITnose

Jun 21, 2016 am 08:56 AM

css3媒体查询技术的出现,在多设备支持上为我们提供了解决方案。

媒体查询的使用方法:

 @media screen and (max-width: 480px) {  .col-xs-1{width: 8.333333333333332%; float:left;}  .col-xs-2{ width: 16.666666666666664%; float:left;}  .col-xs-3{ width: 25%; float:left;} }
Copy after login

在设备窗口宽度小于480时,下面的样式会被采用,不满足这个查询设置,下面定义的css是无效的,html根本就不会识别到。

简单理解就是,如果当前宽度是1000,那么就是div上有类名.col-xs-1,div还是只是简单块元素(不会浮动和宽为百分比),查询的设置是不起作用的。

充分利用媒体查询,实现多设备支持的框架当前最热门就是bootstrap了,我们可以学习和使用它进行开发。

下面是我自己利用媒体查询写的实例页面,都是常用css布局方式配合了媒体查询,多的就不做解释了,一句话就是写法都是css里面,看看就能懂:

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>media/媒体查询/简易UI</title>    <!--设备设置-->    <meta name="viewport" content="width=device-width;initial-scale=1.0">    <!--渲染ie内核-->    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <style> /*========================全局样式============================*/ *{ margin:0; padding:0;} html{height:100%;} body{height:100%; font-size:16px; font-family:"微软雅黑";} a{ text-decoration:none;} img{ border:none;} ul{ list-style:none;}    /*========================预定义样式==========================*/  /*通用*/ .left{float:left;} .right{float:right;} .clear{clear:both;} .clearfix:after{ content:"."; display:block; height:0; clear:both; overflow:hidden;} .clearfix{ zoom:1;} /*12列布局*/ .row:after{ content:"."; display:block; height:0; clear:both; overflow:hidden;} @media screen and (max-width: 480px) {  .col-xs-1{width: 8.333333333333332%; float:left;}  .col-xs-2{ width: 16.666666666666664%; float:left;}  .col-xs-3{ width: 25%; float:left;}  .col-xs-4{ width: 33.33333333333333%; float:left;}  .col-xs-5{width: 41.66666666666667%; float:left;}  .col-xs-6{width: 50%; float:left;}  .col-xs-7{ width: 58.333333333333336%; float:left;}  .col-xs-8{width: 66.66666666666666%; float:left;}  .col-xs-9{width: 75%; float:left;}  .col-xs-10{width: 83.33333333333334%; float:left;}  .col-xs-11{ width: 91.66666666666666%; float:left;}  .col-xs-12{ width:100%; float:left;}  .col-xs-mar-1{ margin-left: 8.333333333333332%; margin-right: 8.333333333333332%;}  .col-xs-mar-2{ margin-left:16.666666666666664%; margin-right:16.666666666666664%;}  .col-xs-mar-3{margin-left:25%; margin-right:25%;}  .col-xs-mar-4{margin-left:33.33333333333333%; margin-right:33.33333333333333%;}  .col-xs-mar-5{margin-left:41.66666666666667%; margin-right:41.66666666666667%;}  .col-xs-mar-6{margin-left:50%; margin-right:50%;}  .col-xs-mar-7{ margin-left:58.333333333333336%; margin-right:58.333333333333336%;}  .col-xs-mar-8{ margin-left:66.66666666666666%; margin-right:66.66666666666666%;}  .col-xs-mar-9{margin-left:75%; margin-right:75%;}  .col-xs-mar-10{margin-left:83.33333333333334%; margin-right:83.33333333333334%;}  .col-xs-mar-11{margin-left:91.66666666666666%; margin-right:91.66666666666666%;} } @media screen and (min-width: 481px) and (max-width: 768px) {  .col-sm-1{ width: 8.333333333333332%; float:left;}  .col-sm-2{ width:16.666666666666664%; float:left;}  .col-sm-3{ width:25%; float:left;}  .col-sm-4{ width:33.33333333333333%; float:left;}  .col-sm-5{ width:41.66666666666667%; float:left;}  .col-sm-6{ width:50%; float:left;}  .col-sm-7{ width:58.333333333333336%; float:left;}  .col-sm-8{ width:66.66666666666666%; float:left;}  .col-sm-9{ width:75%; float:left;}  .col-sm-10{ width:83.33333333333334%; float:left;}  .col-sm-11{ width:91.66666666666666%; float:left;}  .col-sm-12{ width:100%; float:left;}  .col-sm-mar-1{ margin-left: 8.333333333333332%; margin-right: 8.333333333333332%;}  .col-sm-mar-2{ margin-left:16.666666666666664%; margin-right:16.666666666666664%;}  .col-sm-mar-3{margin-left:25%; margin-right:25%;}  .col-sm-mar-4{margin-left:33.33333333333333%; margin-right:33.33333333333333%;}  .col-sm-mar-5{margin-left:41.66666666666667%; margin-right:41.66666666666667%;}  .col-sm-mar-6{margin-left:50%; margin-right:50%;}  .col-sm-mar-7{margin-left:58.333333333333336%; margin-right:58.333333333333336%;}  .col-sm-mar-8{margin-left:66.66666666666666%; margin-right:66.66666666666666%;}  .col-sm-mar-9{margin-left:75%; margin-right:75%;}  .col-sm-mar-10{margin-left:83.33333333333334%; margin-right:83.33333333333334%;}  .col-sm-mar-11{margin-left:91.66666666666666%; margin-right:91.66666666666666%;} } @media screen and (min-width: 769px) and (max-width: 992px) {  .col-md-1{ width: 8.333333333333332%; float:left;}  .col-md-2{ width:16.666666666666664%; float:left;}  .col-md-3{ width:25%; float:left;}  .col-md-4{ width:33.33333333333333%; float:left;}  .col-md-5{ width:41.66666666666667%; float:left;}  .col-md-6{ width:50%; float:left;}  .col-md-7{ width:58.333333333333336%; float:left;}  .col-md-8{ width:66.66666666666666%; float:left;}  .col-md-9{ width:75%; float:left;}  .col-md-10{ width:83.33333333333334%; float:left;}  .col-md-11{ width:91.66666666666666%; float:left;}  .col-md-12{ width:100%; float:left;}  .col-md-mar-1{ margin-left: 8.333333333333332%; margin-right: 8.333333333333332%;}  .col-md-mar-2{ margin-left:16.666666666666664%; margin-right:16.666666666666664%;}  .col-md-mar-3{margin-left:25%; margin-right:25%;}  .col-md-mar-4{margin-left:33.33333333333333%; margin-right:33.33333333333333%;}  .col-md-mar-5{margin-left:41.66666666666667%; margin-right:41.66666666666667%;}  .col-md-mar-6{margin-left:50%; margin-right:50%;}  .col-md-mar-7{margin-left:58.333333333333336%; margin-right:58.333333333333336%;}  .col-md-mar-8{margin-left:66.66666666666666%; margin-right:66.66666666666666%;}  .col-md-mar-9{margin-left:75%; margin-right:75%;}  .col-md-mar-10{margin-left:83.33333333333334%; margin-right:83.33333333333334%;}  .col-md-mar-11{margin-left:91.66666666666666%; margin-right:91.66666666666666%;} } @media screen and (min-width: 993px) and (max-width: 1200px) {  .container{ width:900px; margin:0 auto;}  .col-lg-1{ width: 8.333333333333332%; float:left;}  .col-lg-2{ width:16.666666666666664%; float:left;}  .col-lg-3{ width:25%; float:left;}  .col-lg-4{ width:33.33333333333333%; float:left;}  .col-lg-5{ width:41.66666666666667%; float:left;}  .col-lg-6{ width:50%; float:left;}  .col-lg-7{ width:58.333333333333336%; float:left;}  .col-lg-8{ width:66.66666666666666%; float:left;}  .col-lg-9{ width:75%; float:left;}  .col-lg-10{ width:83.33333333333334%; float:left;}  .col-lg-11{ width:91.66666666666666%; float:left;}  .col-lg-12{ width:100%; float:left;}  .col-lg-mar-1{ margin-left: 8.333333333333332%; margin-right: 8.333333333333332%;}  .col-lg-mar-2{ margin-left:16.666666666666664%; margin-right:16.666666666666664%;}  .col-lg-mar-3{margin-left:25%; margin-right:25%;}  .col-lg-mar-4{margin-left:33.33333333333333%; margin-right:33.33333333333333%;}  .col-lg-mar-5{margin-left:41.66666666666667%; margin-right:41.66666666666667%;}  .col-lg-mar-6{margin-left:50%; margin-right:50%;}  .col-lg-mar-7{margin-left:58.333333333333336%; margin-right:58.333333333333336%;}  .col-lg-mar-8{margin-left:66.66666666666666%; margin-right:66.66666666666666%;}  .col-lg-mar-9{margin-left:75%; margin-right:75%;}  .col-lg-mar-10{margin-left:83.33333333333334%; margin-right:83.33333333333334%;}  .col-lg-mar-11{margin-left:91.66666666666666%; margin-right:91.66666666666666%;} } @media screen and (min-width: 1201px) {  .container{ width:1100px; margin:0 auto;}  .col-xs-max-1{ width: 8.333333333333332%; float:left;}  .col-xs-max-2{ width:16.666666666666664%; float:left;}  .col-xs-max-3{ width:25%; float:left;}  .col-xs-max-4{ width:33.33333333333333%; float:left;}  .col-xs-max-5{ width:41.66666666666667%; float:left;}  .col-xs-max-6{ width:50%; float:left;}  .col-xs-max-7{ width:58.333333333333336%; float:left;}  .col-xs-max-8{ width:66.66666666666666%; float:left;}  .col-xs-max-9{ width:75%; float:left;}  .col-xs-max-10{ width:83.33333333333334%; float:left;}  .col-xs-max-11{ width:91.66666666666666%; float:left;}  .col-xs-max-12{ width:100%; float:left;}  .col-xs-max-mar-1{ margin-left: 8.333333333333332%; margin-right: 8.333333333333332%;}  .col-xs-max-mar-2{ margin-left:16.666666666666664%; margin-right:16.666666666666664%;}  .col-xs-max-mar-3{margin-left:25%; margin-right:25%;}  .col-xs-max-mar-4{margin-left:33.33333333333333%; margin-right:33.33333333333333%;}  .col-xs-max-mar-5{margin-left:41.66666666666667%; margin-right:41.66666666666667%;}  .col-xs-max-mar-6{margin-left:50%; margin-right:50%;}  .col-xs-max-mar-7{margin-left:58.333333333333336%; margin-right:58.333333333333336%;}  .col-xs-max-mar-8{margin-left:66.66666666666666%; margin-right:66.66666666666666%;}  .col-xs-max-mar-9{margin-left:75%; margin-right:75%;}  .col-xs-max-mar-10{margin-left:83.33333333333334%; margin-right:83.33333333333334%;}  .col-xs-max-mar-11{margin-left:91.66666666666666%; margin-right:91.66666666666666%;} } /*ui细化*/ .width-10{ width:10px;} .width-50{ width:50px;} .width-100{ width:100px;} .width-150{ width:150px;} .width-200{ width:200px;}  .border-normal{ background:#999; border:none; margin:10px 10%; display:block; border-radius:10px; height:50px; line-height:40px; color:#fff; text-align:center; width:80%; font-size:18px; cursor:pointer;} .border-success{background:#0C6;border:none;margin:10px 10%;display:block; border-radius:10px;height:50px;  line-height:40px; color:#fff; text-align:center;width:80%; font-size:18px; cursor:pointer;} .border-err{background:#F33;border:none;margin:10px 10%;display:block; border-radius:10px;height:50px; line-height:40px; color:#fff;  text-align:center;width:80%; font-size:18px; cursor:pointer;} .table-normal{ width:100%; margin:10px 0px; border-collapse:collapse;} .table-normal td{ border:1px solid #999; padding:5px;} .table-radius{ width:100%; margin:10px 0px; border-spacing:0px;border-right:1px solid #999;border-bottom:1px solid #999;border-radius:10px;} .table-radius td{ border-top:1px solid #999;border-left:1px solid #999; padding:5px;} .table-radius tr td:last-child{} .table-radius tr:last-child td{} .table-radius tr:first-child td:first-child{border-radius:10px 0 0 0;} .table-radius tr:first-child td:last-child{border-radius:0 10px 0 0;} .table-radius tr:last-child td:first-child{border-radius:0 0 0 10px;} /*=========================自定义样式===========================*/ #size{ height:50px; line-height:50px; text-align:center; font-weight:bold; font-size:20px; margin:50px; border-bottom:5px solid #09F;} .col{ margin:20px 0; background:#0CF; box-sizing:border-box; border:2px solid #993;} .col2{background:#C30; box-sizing:border-box; border:2px solid #0CF;height:50px;}    </style></head><body> <div id="size">测试窗口大小当前为<span id="sizeval">0</span>px</div>    <h3 id="演示-列布局">演示:12列布局</h3>    <div class="container">        <div class="row">            <div class="col col-md-5 col-lg-3 col-lg-mar-2 col-xs-max-2 col-xs-max-mar-1">1</div>            <div class="col col-md-5 col-lg-3 col-xs-max-2">2</div>            <div class="col col-md-5 col-lg-3 col-lg-mar-2 col-xs-max-2">3</div>            <div class="col col-md-5 col-lg-3 col-xs-max-2">             <div class="row">                    <div class="col2 col-md-5 col-lg-3 col-lg-mar-2 col-xs-max-2">3-1</div>                    <div class="col2 col-md-5 col-lg-3 col-lg-mar-2 col-xs-max-2">3-2</div>                </div>            </div>            <div class="col col-md-12 col-lg-6 col-xs-max-12">4</div>            <div class="col col-md-8 col-lg-6 col-xs-max-3">5</div>            <div class="col col-md-4 col-lg-6 col-xs-max-3">6</div>            <div class="col col-md-4 col-lg-6 col-xs-max-6">7</div>        </div>    </div>    <h3 id="演示-表单ui">演示:表单ui</h3>    <div class="container">     <input type="button" class="border-normal" value="提交">        <input type="button" class="border-success" value="提交">        <input type="button" class="border-err" value="提交">    </div>    <h3 id="演示-表格">演示:表格</h3>    <div class="container">    <table class="table-normal">     <tr><td>1</td><td>1</td><td>1</td></tr>        <tr><td>2</td><td>2</td><td>2</td></tr>        <tr><td>3</td><td>3</td><td>3</td></tr>    </table>    <table class="table-radius">     <tr><td>1</td><td>1</td><td>1</td></tr>        <tr><td>2</td><td>2</td><td>2</td></tr>        <tr><td>3</td><td>3</td><td>3</td></tr>    </table>    </div>    </body><script>var size=document.getElementById("size");var sizeval=document.getElementById("sizeval");sizeval.innerHTML=document.documentElement.offsetWidth;window.onresize=function(){ sizeval.innerHTML=document.documentElement.offsetWidth;};</script></html>
Copy after login

流式布局,百分比赋值,多预设设置结合媒体查询。

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

See all articles