CSS媒体查询(media)详解

@media常用参数

width、height 浏览器高度和宽度
device-width、device-height 设备高度和宽度

适配不同设备,对应不同的css

默认为 media=“all”

    <!-- 正常屏幕 -->
    <style media="screen">    

    </style>
    <!-- 打印机屏幕 -->
    <style media="print">

    </style>

外部导入css

  <link rel="stylesheet" href="screen.css" media="screen">
    <link rel="stylesheet" href="print.css" media="print">

或者

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

在同一个css文件中,进行对不同屏幕的设定
对处于宽度为100px以上的,进行设定

@media screen and (min-width: 100px){
    
}

对处于宽度为100px和1000px之间的,进行设定

@media screen and (min-width: 100px) and (max-width: 1000px){
    
}

对于横屏或者是100px至1000px之间的

@media screen and (min-width: 100px) and 
 (max-width: 1000px),(orientation: landscape)    {
 
 }

对于100px至1000px之外的屏幕

@media not screen and (min-width: 100px) and 
 (max-width: 1000px),(orientation: landscape)    {
 
 }

对于低端浏览器的忽略

@media only screen and (min-width: 100px) and 
 (max-width: 1000px),(orientation: landscape)    {
 
 }

案例

方式一

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>媒体查询案例</title>
</head>

<body>
    <div id="div_father">
        <div class="div1"></div>
        <div class="div1"></div>
        <div class="div1"></div>
    </div>
</body>

<style>
    #div_father {
        width: 100%;
        height: 400px;
    }

    #div_father div {
        float: left;
        height: 100px;
    }

    #div_father div:nth-child(1) {
        background-color: red;
    }

    #div_father div:nth-child(2) {
        background-color: green;
    }

    #div_father div:nth-child(3) {
        background-color: yellow;
    }

    @media screen and (max-width:400px) {
        #div_father div {
            width: 100%;
        }
    }

    @media screen and (min-width:401px) and (max-width:600px) {
        #div_father div {
            width: 50%;
        }
    }

    @media screen and (min-width:601px) {
        #div_father div {
            width: 33.33%;
        }
    }
</style>

</html>

方式二

方式一这种的都是在一个style里面,在进行划分
也可以多个上style进行划分

<style>
    '''
</style>
<style media="(max-width:600px)">
    '''
</style>
<style media="(min-width:601px) and (max-width:800px)">
    '''
</style>
<style media="(min-width:801px)">
   '''
</style>

方式三

<link rel="stylesheet" href="./media.css" media="(min-width:601px) and (max-width:800px)">

媒体查询不起作用解答

注意and的两边要有空格

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李和贵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值