详解CSS3媒体查询响应式布局bootstrap 框架原理实战(推荐)


Posted in HTML / CSS onNovember 16, 2020

媒体设备类型使用详解:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>

    <!-- 屏幕设备 -->
    <style media="screen">
        h1{
            color:red;
        }
    </style>

    <!-- 打印机设备 -->
    <style media="print">
        h1{
            color:green;
        }
    </style>

    <!-- 屏幕设备和打印机设备 -->
    <style media="screen,print">
        h1{
            font-weight:normal;
        }
    </style>
</head>
<body>
  <h1>cyy</h1>
</body>
</html>

使用link标签设置媒体类型:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>

    <!-- 默认media为all,所有设备 -->
    <link rel="stylesheet" href="css/commob.css" media="all">
    <link rel="stylesheet" href="css/screen.css" media="screen">
    <link rel="stylesheet" href="css/print.css" media="print">
</head>
<body>
  <h1>cyy</h1>
</body>
</html>

使用@import简化页面多文件引用:

这是推荐的做法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>

    <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <h1>cyy</h1>
</body>
</html>

style.css

@import url(common.css);
@import url(screen.css) screen;
@import url(print.css) print;

样式表中使用@media局部定义响应查询:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>

    <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <div class="navbar">
      <a href="">cyy</a>
      <ul>
          <li>cyy1</li>
          <li>cyy2</li>
          <li>cyy3</li>
      </ul>
  </div>
</body>
</html>

相关less

.navbar{
  height:60px;
  width:900px;
  display:flex;
  align-items:center;
  background:#f3f3f3;
  margin:0 auto;

  ul{
    list-style:none;
    display:flex;
  }
}

@media screen and (max-width:600px){
  .navbar{
    ul{
      display:none;
    }
  }
}

and条件判断响应式应用:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>

    <style media="screen and (min-width:768px) and (max-width:1000px)">
        h1{
            color:red;
        }
    </style>

<style media="screen and (max-width:768px)">
    h1{
        color:blue;
    }
</style>
</head>
<body>
  <h1>CYY</h1>
</body>
</html>

逻辑或使用技巧操作:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>

    <!-- 当设备为横屏展示,或者设备宽度大于768px不论横竖屏时 -->
    <style media="screen and (orientation:landscape),screen and (min-width:768px)">
        h1{
            color:red;
        }
    </style>

</head>
<body>
  <h1>CYY</h1>
</body>
</html>

not关键词使用注意要点:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>

    
    <style>
        /* 如果满足条件,则不应用该样式 */
        @media not screen and (min-width:500px) and (max-width:768px){
            h1{
                color:red;
            }
        }
    </style>

</head>
<body>
  <h1>CYY</h1>
</body>
</html>

使用only排除低端浏览器:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>

    
    <style>
        /* 加上only之后,低端浏览器会忽略该句语法 */
        @media only screen and (min-width:500px){
            h1{
                color:red;
            }
        }
    </style>

</head>
<body>
  <h1>CYY</h1>
</body>
</html>

实战案例操作之文件结构:

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdn.staticfile.org/jquery/1.10.0/jquery.js"></script>

    <link rel="stylesheet" href="css/style.css">

</head>
<body>
  <header class="mb-2">
      <div class="container">
        <div class="navbar">
            <a href="" class="logo">CYY</a>
            <label for="toggle-nav"><i class="fa fa-tasks" aria-hidden="true"></i></label>
            <input type="checkbox" name="" id="toggle-nav">
            <div class="collapse">
                <ul class="links">
                    <li><a href="">系统学习</a></li>
                    <li><a href="">实战课程</a></li>
                    <li><a href="">话题讨论</a></li>
                    <li><a href="">签到打卡</a></li>
                </ul>
                <div class="form">
                    <a href="">登录</a>
                    <a href="" class="form-bg">注册</a>
                </div>
            </div>
        </div>
      </div>
  </header>

  
  <div class="container">
    <div class="row">
        <div class="col-6 col-lg-9 col-xs-12">
            <div class="card">
                <div class="card-header">
                    <h2>最新更新</h2>
                </div>
                <div class="card-body">
                    <ul class="list-group">
                        <li>
                            <a href="">cyy开始学习响应式布局</a>
                            <span>2020-11-13</span>
                        </li>
                        <li>
                            <a href="">cyy开始学习响应式布局</a>
                            <span>2020-11-13</span>
                        </li>
                        <li>
                            <a href="">cyy开始学习响应式布局</a>
                            <span>2020-11-13</span>
                        </li>
                        <li>
                            <a href="">cyy开始学习响应式布局</a>
                            <span>2020-11-13</span>
                        </li>
                        <li>
                            <a href="">cyy开始学习响应式布局</a>
                            <span>2020-11-13</span>
                        </li>
                        <li>
                            <a href="">cyy开始学习响应式布局</a>
                            <span>2020-11-13</span>
                        </li>
                        <li>
                            <a href="">cyy开始学习响应式布局</a>
                            <span>2020-11-13</span>
                        </li>
                        <li>
                            <a href="">cyy开始学习响应式布局</a>
                            <span>2020-11-13</span>
                        </li>
                    </ul>
                </div>
                <div class="card-footer">
                    <div class="page">
                        <a href=""><</a>
                        <a href="">1</a>
                        <a href="">2</a>
                        <a href="" class="current">3</a>
                        <a href="">4</a>
                        <a href="">5</a>
                        <a href="">></a>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-6 col-lg-3 col-xs-12">
            <div class="card">
                <div class="card-header">
                    <h3>社区小帖</h3>
                </div>
                <div class="card-body"></div>
                <div class="card-footer"></div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

引入统一控制的style.css

@import url(common.css);
@import url(navbar.css);
@import url(card.css);
@import url(title.css);
@import url(page.css);
@import url(margin.css);
@import url(list-group.css);
@import url(small-x.css) only screen and (max-width:768px);
@import url(small.css) only screen and (min-width:768px);
@import url(medium.css) only screen and (min-width:960px);
@import url(big.css) only screen and (min-width:1200px);

导航组件navbar.less

header{
  border-bottom:5px solid #009688;
  box-shadow:0 5px 5px rgba(0,0,0,.2);

  .navbar{
    display:flex;
    padding:1rem 0;
    align-items:center;

    .logo{
      color:#009688;
      margin-right:20px;
      font-weight:bold;
      font-size:1.3rem;

      &+label{
        display:none;

        &+input{
          display:none;
        }

      }
    }

    .collapse{
      display:flex;
      flex-grow:1;

      .links{
        display:flex;
        // 占满剩余空间
        margin-right:auto;

        li{
          margin:0 10px;

          a{
            color:#777;
            
  
            &:hover{
              color:#333;
              font-weight:bold;
            }
          }
        }
        
      }

      .form{
        a{
          border:1px solid #009688;
          color:#009688;
          padding:.3rem 1rem;
          border-radius:.3rem;

          &.form-bg{
            background:#009688;
            color:white;
          }
        }
      }
    }
  }
}

@media screen and (max-width:960px){
  header{
    .navbar{
      // flex-flow 属性是 flex-direction 和 flex-wrap 属性的复合属性。
      flex-flow:row wrap;

      .logo{
        margin-right:auto;

        &+label{
          display:block;
          border:1px solid #ddd;
          padding:.5rem 1rem;
          color:#555;
          cursor:pointer;

          &+input{
            display:none;
          }

          &+input:checked{
            &+.collapse{
              display:block;
            }
          }
        }
      }
      .collapse{
        display:none;
        flex-flow:column;
        width:100%;

        .links{
          flex-direction:column;
          margin-bottom:1.5rem;

          li{
            margin:.5rem 0;
          }
        }
      }
    }
  }
}

卡片组件card.less

.card{
  border:1px solid #ddd;
  box-shadow:0 0 5px rgba(0,0,0,.1);
  border-radius:.2rem;

  .card-header{
    padding:.5rem 1rem;
    border-bottom:1px solid #ddd;
  }
  .card-body{
    padding:1rem;
  }
  .card-footer{
    padding:.5rem 1rem;
    border-top:1px solid #ddd;
  }
}

文本组件title.less

h2 {
  font-size: 1rem;
}
h3 {
  font-size: 0.8rem;
}
h2,
h3,
h4 {
  color: #555;
}

分页组件page.less

.page{
  display:flex;

  a{
    display:block;
    padding:.3rem .8rem;
    border:1px solid #ddd;
    // 让重叠的两个边框线叠加在一起
    margin-left:-1px;
    color:#555;

    &:first-child{
      border-top-left-radius:.3rem;
      border-bottom-left-radius:.3rem;
    }

    &:last-child{
      border-top-right-radius:.3rem;
      border-bottom-right-radius:.3rem;
    }

    &.current{
      background:#009688;
      color:white;
      border:1px solid #009688;
    }
  }
}

边距组件margin.less

.mb-1 {
  margin-bottom: 1rem;
}
.mb-2 {
  margin-bottom: 2rem;
}
.mb-3 {
  margin-bottom: 3rem;
}

列表组件list-group.less

.list-group{
  li{
    display:flex;
    justify-content:space-between;
    padding:.8rem 0;
    border-bottom:1px solid #ddd;
    font-size:.9rem;

    &:last-child{
      border-bottom:none;
    }

    a{
      color:#777;
    }

    span{
      color:#888;
      font-size:.6rem;
    }
  }
}

超小屏适配 small-x.less

body {
  background: white;
}
.container {
  width: 95%;
  margin: 0 auto;
}
.col-xs-12 {
  grid-column: span 12;
}
.col-xs-11 {
  grid-column: span 11;
}
.col-xs-10 {
  grid-column: span 10;
}
.col-xs-9 {
  grid-column: span 9;
}
.col-xs-8 {
  grid-column: span 8;
}
.col-xs-7 {
  grid-column: span 7;
}
.col-xs-6 {
  grid-column: span 6;
}
.col-xs-5 {
  grid-column: span 5;
}
.col-xs-4 {
  grid-column: span 4;
}
.col-xs-3 {
  grid-column: span 3;
}
.col-xs-2 {
  grid-column: span 2;
}
.col-xs-1 {
  grid-column: span 1;
}

小屏适配small.less

body{
  background:white;
}
.container{
  width:750px;
  margin:0 auto;

}
.col-sm-12{
  grid-column:span 12;
}
.col-sm-11{
  grid-column:span 11;
}
.col-sm-10{
  grid-column:span 10;
}
.col-sm-9{
  grid-column:span 9;
}
.col-sm-8{
  grid-column:span 8;
}
.col-sm-7{
  grid-column:span 7;
}

.col-sm-6{
  grid-column:span 6;
}
.col-sm-5{
  grid-column:span 5;
}
.col-sm-4{
  grid-column:span 4;
}
.col-sm-3{
  grid-column:span 3;
}
.col-sm-2{
  grid-column:span 2;
}
.col-sm-1{
  grid-column:span 1;
}

中屏适配medium.less

body{
  background:white;
}
.container{
  width:950px;
  margin:0 auto;

}
.col-md-12{
  grid-column:span 12;
}
.col-md-11{
  grid-column:span 11;
}
.col-md-10{
  grid-column:span 10;
}
.col-md-9{
  grid-column:span 9;
}
.col-md-8{
  grid-column:span 8;
}
.col-md-7{
  grid-column:span 7;
}

.col-md-6{
  grid-column:span 6;
}
.col-md-5{
  grid-column:span 5;
}
.col-md-4{
  grid-column:span 4;
}
.col-md-3{
  grid-column:span 3;
}
.col-md-2{
  grid-column:span 2;
}
.col-md-1{
  grid-column:span 1;
}

大屏适配big.less

body{
  background:transparent;
}
.container{
  width:1180px;
  margin:0 auto;

}
.col-lg-12{
  grid-column:span 12;
}
.col-lg-11{
  grid-column:span 11;
}
.col-lg-10{
  grid-column:span 10;
}
.col-lg-9{
  grid-column:span 9;
}
.col-lg-8{
  grid-column:span 8;
}
.col-lg-7{
  grid-column:span 7;
}

.col-lg-6{
  grid-column:span 6;
}
.col-lg-5{
  grid-column:span 5;
}
.col-lg-4{
  grid-column:span 4;
}
.col-lg-3{
  grid-column:span 3;
}
.col-lg-2{
  grid-column:span 2;
}
.col-lg-1{
  grid-column:span 1;
}

效果图

详解CSS3媒体查询响应式布局bootstrap 框架原理实战(推荐)

使用rem单位操作尺寸响应处理

推荐使用自动化构建工具插件。

到此这篇关于详解CSS3媒体查询响应式布局bootstrap 框架原理实战的文章就介绍到这了,更多相关CSS 响应式布局bootstrap 框架内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章,希望大家以后多多支持三水点靠木!

HTML / CSS 相关文章推荐
纯CSS3实现Material Design效果
Mar 09 HTML / CSS
css3的transition效果和transfor效果示例介绍
Oct 30 HTML / CSS
图解CSS3制作圆环形进度条的实例教程
May 26 HTML / CSS
纯css3实现宠物小鸡实例代码
Oct 08 HTML / CSS
HTML5 Canvas 起步(1) - 基本概念
May 12 HTML / CSS
HTML5 Canvas像素处理使用接口介绍
Dec 02 HTML / CSS
html5基础教程常用技巧整理
Aug 20 HTML / CSS
用HTML5的canvas实现一个炫酷时钟效果
May 20 HTML / CSS
Html5实现首页动态视频背景的示例代码
Sep 25 HTML / CSS
HTML5页面无缝闪开的问题及解决方案
Jun 11 HTML / CSS
css实现文章分割线样式的多种方法总结
Apr 21 HTML / CSS
table设置超出部分隐藏,鼠标移上去显示全部内容的方法
Dec 24 HTML / CSS
CSS3 实现穿梭星空动画
Nov 13 #HTML / CSS
CSS3 实现图形下落动画效果
Nov 13 #HTML / CSS
css3实现平移效果(transfrom:translate)的示例
Nov 13 #HTML / CSS
CSS3 文字动画效果
Nov 12 #HTML / CSS
css3弹性盒子flex实现三栏布局的实现
Nov 12 #HTML / CSS
CSS3 按钮边框动画的实现
Nov 12 #HTML / CSS
CSS3 实现发光边框特效
Nov 11 #HTML / CSS
You might like
如何跨站抓取别的站点的页面的补充
2006/10/09 PHP
PHP的面试题集
2006/11/19 PHP
php读取mssql的ntext字段返回值为空的解决方法
2014/12/30 PHP
PHP安装扩展mcrypt以及相关依赖项深入讲解
2021/03/04 PHP
JS option location 页面跳转实现代码
2008/12/27 Javascript
Javascript拓展String方法小结
2013/07/08 Javascript
使用JQuery库提供的扩展功能实现自定义方法
2014/09/09 Javascript
js+csss实现的一个带复选框的下拉框
2014/09/29 Javascript
js实现滑动触屏事件监听的方法
2015/05/05 Javascript
使用jquery.qrcode.min.js实现中文转化二维码
2016/03/11 Javascript
浅谈jQuery中的eq()与DOM中element.[]的区别
2016/10/28 Javascript
nodeJS实现路由功能实例代码
2017/06/08 NodeJs
微信小程序使用checkbox显示多项选择框功能【附源码下载】
2017/12/11 Javascript
vue 监听屏幕高度的实例
2018/09/05 Javascript
浅谈React Event实现原理
2018/09/20 Javascript
vue + element-ui的分页问题实现
2018/12/17 Javascript
python 域名分析工具实现代码
2009/07/15 Python
利用Python自动监控网站并发送邮件告警的方法
2016/08/24 Python
python+pandas分析nginx日志的实例
2018/04/28 Python
python 巧用正则寻找字符串中的特定字符的位置方法
2018/05/02 Python
python检索特定内容的文本文件实例
2018/06/05 Python
python 含子图的gif生成时内存溢出的方法
2019/07/07 Python
简单了解python中的与或非运算
2019/09/18 Python
Django REST Framework 分页(Pagination)详解
2020/11/30 Python
matplotlib bar()实现多组数据并列柱状图通用简便创建方法
2021/02/24 Python
基于HTML5陀螺仪实现ofo首页眼睛移动效果的示例
2017/07/31 HTML / CSS
萨克斯第五大道的折扣店:Saks Fifth Avenue OFF 5TH
2016/08/25 全球购物
英国手工布艺沙发在线购买:Sofas & Stuff
2018/03/02 全球购物
俄罗斯玩具、儿童用品、儿童服装和鞋子网上商店:MyToys.ru
2019/10/14 全球购物
请用Python写一个获取用户输入数字,并根据数字大小输出不同信息的脚本
2014/05/20 面试题
俞敏洪一分钟演讲稿
2014/08/26 职场文书
六查六看自检自查剖析材料
2014/10/14 职场文书
行政介绍信范文
2015/05/04 职场文书
导游词之太原天龙山
2020/01/02 职场文书
无线电知识基础入门篇
2022/02/18 无线电
《艾尔登法环》Boss腐烂树灵很有可能是《黑暗之魂3》的一个废案
2022/04/11 其他游戏