CSS媒体查询

CSS媒体查询

媒体查询简介

CSS媒体查询(Media Queries)是CSS3引入的一个重要特性,允许开发者根据设备特性(如屏幕宽度、高度、分辨率等)应用不同的CSS样式。这是实现响应式设计的核心技术。

为什么需要媒体查询?

现代网页需要在各种设备上正常显示,包括:

  • 桌面电脑(1920px+)
  • 笔记本电脑(1366px-1920px)
  • 平板电脑(768px-1024px)
  • 手机(320px-768px)

媒体查询让我们能够为不同设备提供最佳的用户体验。

基本语法

基本结构

@media 媒体类型 and (媒体特性) {
    /* CSS规则 */
}

在HTML中使用

<!-- 在link标签中使用 -->
<link rel="stylesheet" media="screen and (max-width: 768px)" href="mobile.css">

<!-- 在style标签中使用 -->
<style>
@media screen and (max-width: 768px) {
    body {
        font-size: 14px;
    }
}
</style>

在CSS文件中使用

/* 默认样式 */
.container {
    width: 1200px;
    margin: 0 auto;
}

/* 平板样式 */
@media screen and (max-width: 1024px) {
    .container {
        width: 100%;
        padding: 0 20px;
    }
}

/* 手机样式 */
@media screen and (max-width: 768px) {
    .container {
        padding: 0 10px;
    }
}

媒体类型

常用媒体类型

媒体类型描述
all所有设备(默认值)
screen彩色屏幕设备
print打印设备
speech屏幕阅读器

示例

/* 针对屏幕设备 */
@media screen {
    body {
        background-color: #f0f0f0;
    }
}

/* 针对打印设备 */
@media print {
    body {
        background-color: white;
        color: black;
    }
    
    .no-print {
        display: none;
    }
}

媒体特性

宽度相关特性

/* 最大宽度 */
@media (max-width: 768px) {
    /* 屏幕宽度小于等于768px时生效 */
}

/* 最小宽度 */
@media (min-width: 769px) {
    /* 屏幕宽度大于等于769px时生效 */
}

/* 精确宽度 */
@media (width: 1024px) {
    /* 屏幕宽度正好为1024px时生效 */
}

/* 宽度范围 */
@media (min-width: 768px) and (max-width: 1024px) {
    /* 屏幕宽度在768px-1024px之间时生效 */
}

高度相关特性

/* 最大高度 */
@media (max-height: 600px) {
    .header {
        height: 50px;
    }
}

/* 最小高度 */
@media (min-height: 800px) {
    .content {
        min-height: 600px;
    }
}

设备相关特性

/* 设备像素比 */
@media (-webkit-min-device-pixel-ratio: 2) {
    /* Retina显示屏 */
    .logo {
        background-image: url('logo@2x.png');
        background-size: 100px 50px;
    }
}

/* 屏幕方向 */
@media (orientation: landscape) {
    /* 横屏 */
    .gallery {
        flex-direction: row;
    }
}

@media (orientation: portrait) {
    /* 竖屏 */
    .gallery {
        flex-direction: column;
    }
}

交互相关特性

/* 悬停能力 */
@media (hover: hover) {
    .button:hover {
        background-color: #007bff;
    }
}

/* 指针精度 */
@media (pointer: coarse) {
    /* 触摸设备 */
    .button {
        padding: 15px 20px;
        font-size: 16px;
    }
}

@media (pointer: fine) {
    /* 鼠标等精确指针 */
    .button {
        padding: 10px 15px;
        font-size: 14px;
    }
}

常用断点

移动优先断点

/* 手机 */
@media (min-width: 320px) {
    .container {
        width: 100%;
        padding: 0 15px;
    }
}

/* 大屏手机 */
@media (min-width: 480px) {
    .container {
        padding: 0 20px;
    }
}

/* 平板 */
@media (min-width: 768px) {
    .container {
        width: 750px;
        margin: 0 auto;
    }
}

/* 桌面 */
@media (min-width: 1024px) {
    .container {
        width: 1000px;
    }
}

/* 大屏桌面 */
@media (min-width: 1200px) {
    .container {
        width: 1170px;
    }
}

桌面优先断点

/* 默认桌面样式 */
.container {
    width: 1200px;
    margin: 0 auto;
}

/* 小屏桌面 */
@media (max-width: 1199px) {
    .container {
        width: 100%;
        max-width: 1000px;
    }
}

/* 平板 */
@media (max-width: 1023px) {
    .container {
        max-width: 768px;
        padding: 0 20px;
    }
}

/* 手机 */
@media (max-width: 767px) {
    .container {
        padding: 0 15px;
    }
}

实战案例

案例1:响应式导航菜单

/* 默认桌面导航 */
.nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
}

.nav-menu {
    display: flex;
    list-style: none;
    margin: 0;
    padding: 0;
}

.nav-menu li {
    margin: 0 15px;
}

.nav-toggle {
    display: none;
}

/* 平板和手机导航 */
@media (max-width: 768px) {
    .nav-menu {
        display: none;
        flex-direction: column;
        position: absolute;
        top: 100%;
        left: 0;
        width: 100%;
        background-color: white;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    }
    
    .nav-menu.active {
        display: flex;
    }
    
    .nav-menu li {
        margin: 0;
        padding: 15px 20px;
        border-bottom: 1px solid #eee;
    }
    
    .nav-toggle {
        display: block;
        background: none;
        border: none;
        font-size: 24px;
        cursor: pointer;
    }
}

案例2:响应式卡片网格

/* 卡片容器 */
.card-grid {
    display: grid;
    gap: 20px;
    padding: 20px;
}

/* 桌面:4列 */
@media (min-width: 1200px) {
    .card-grid {
        grid-template-columns: repeat(4, 1fr);
    }
}

/* 平板:3列 */
@media (min-width: 768px) and (max-width: 1199px) {
    .card-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* 大屏手机:2列 */
@media (min-width: 480px) and (max-width: 767px) {
    .card-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 15px;
    }
}

/* 小屏手机:1列 */
@media (max-width: 479px) {
    .card-grid {
        grid-template-columns: 1fr;
        gap: 10px;
        padding: 10px;
    }
}

/* 卡片样式 */
.card {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    overflow: hidden;
}

.card img {
    width: 100%;
    height: 200px;
    object-fit: cover;
}

.card-content {
    padding: 20px;
}

@media (max-width: 479px) {
    .card-content {
        padding: 15px;
    }
}

案例3:响应式表格

/* 默认表格样式 */
.table-container {
    overflow-x: auto;
}

.table {
    width: 100%;
    border-collapse: collapse;
}

.table th,
.table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}

/* 小屏设备:转换为卡片布局 */
@media (max-width: 768px) {
    .table,
    .table thead,
    .table tbody,
    .table th,
    .table td,
    .table tr {
        display: block;
    }
    
    .table thead tr {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }
    
    .table tr {
        border: 1px solid #ccc;
        margin-bottom: 10px;
        padding: 10px;
        border-radius: 5px;
    }
    
    .table td {
        border: none;
        position: relative;
        padding-left: 50%;
    }
    
    .table td:before {
        content: attr(data-label) ": ";
        position: absolute;
        left: 6px;
        width: 45%;
        padding-right: 10px;
        white-space: nowrap;
        font-weight: bold;
    }
}

最佳实践

1. 移动优先设计

/* 好的做法:移动优先 */
/* 基础样式(移动端) */
.container {
    width: 100%;
    padding: 0 15px;
}

/* 逐步增强(大屏设备) */
@media (min-width: 768px) {
    .container {
        max-width: 750px;
        margin: 0 auto;
    }
}

@media (min-width: 1200px) {
    .container {
        max-width: 1170px;
    }
}

2. 合理的断点选择

/* 基于内容而非设备选择断点 */
.text-block {
    font-size: 16px;
    line-height: 1.5;
}

/* 当文本行长度过长时调整 */
@media (min-width: 800px) {
    .text-block {
        font-size: 18px;
        line-height: 1.6;
        max-width: 70ch; /* 70个字符宽度 */
    }
}

3. 性能优化

/* 避免过度嵌套 */
/* 不好的做法 */
@media (max-width: 768px) {
    @media (orientation: portrait) {
        .element {
            /* 样式 */
        }
    }
}

/* 好的做法 */
@media (max-width: 768px) and (orientation: portrait) {
    .element {
        /* 样式 */
    }
}

4. 逻辑操作符

/* AND 操作符 */
@media screen and (min-width: 768px) and (max-width: 1024px) {
    /* 同时满足多个条件 */
}

/* OR 操作符(逗号分隔) */
@media screen and (max-width: 768px), print {
    /* 满足任一条件 */
}

/* NOT 操作符 */
@media not print {
    /* 除了打印设备 */
}

/* ONLY 操作符 */
@media only screen and (min-width: 768px) {
    /* 仅限屏幕设备 */
}

高级技巧

1. 自定义属性配合媒体查询

:root {
    --container-width: 100%;
    --font-size: 16px;
    --spacing: 10px;
}

@media (min-width: 768px) {
    :root {
        --container-width: 750px;
        --font-size: 18px;
        --spacing: 20px;
    }
}

@media (min-width: 1200px) {
    :root {
        --container-width: 1170px;
        --font-size: 20px;
        --spacing: 30px;
    }
}

.container {
    width: var(--container-width);
    margin: 0 auto;
    padding: var(--spacing);
    font-size: var(--font-size);
}

2. 容器查询(Container Queries)

/* 现代浏览器支持 */
.card-container {
    container-type: inline-size;
}

@container (min-width: 300px) {
    .card {
        display: flex;
        flex-direction: row;
    }
}

@container (min-width: 500px) {
    .card {
        flex-direction: column;
    }
}

3. 媒体查询调试

/* 调试断点 */
body::before {
    content: "Mobile";
    position: fixed;
    top: 0;
    left: 0;
    background: red;
    color: white;
    padding: 5px;
    z-index: 9999;
}

@media (min-width: 768px) {
    body::before {
        content: "Tablet";
        background: orange;
    }
}

@media (min-width: 1024px) {
    body::before {
        content: "Desktop";
        background: green;
    }
}

4. 打印样式优化

@media print {
    * {
        -webkit-print-color-adjust: exact !important;
        color-adjust: exact !important;
    }
    
    body {
        font-size: 12pt;
        line-height: 1.4;
    }
    
    .page-break {
        page-break-before: always;
    }
    
    .no-print {
        display: none !important;
    }
    
    a[href]:after {
        content: " (" attr(href) ")";
    }
}

总结

CSS媒体查询是构建响应式网站的基础工具。通过合理使用媒体查询,我们可以:

  • 创建适应不同设备的用户界面
  • 优化不同屏幕尺寸下的用户体验
  • 提供针对性的样式和布局
  • 实现现代化的响应式设计

掌握媒体查询的关键是理解不同设备的特性,选择合适的断点,并采用移动优先的设计理念。随着技术的发展,容器查询等新特性也为我们提供了更多的可能性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值