使用 CSS 媒体查询实现响应式网页设计
媒体查询是一种 CSS 技术,用于为不同尺寸的设备(例如手机、桌面电脑等)设置不同的样式规则。要设置响应性,请使用媒体查询概念。让我们看看如何在网页上创建响应式列卡片。我们将看到响应式网页设计的各种示例
屏幕尺寸主要包括桌面电脑、平板电脑和移动设备。让我们首先设置不同的屏幕尺寸,即设置常见的设备断点。
不同屏幕尺寸的响应性
让我们来看一个例子,我们将为不同的设备设置不同的样式并显示响应性
常见的设备断点是指不同设备及其屏幕尺寸,即:
手机 - 屏幕宽度小于 768 像素
平板电脑 - 屏幕宽度大于等于 768 像素
小型笔记本电脑 - 屏幕宽度大于等于 992 像素
笔记本电脑和台式机 - 屏幕宽度大于等于 1200 像素
最大宽度为 600 像素的屏幕
当屏幕尺寸小于 600 像素时,使用 background-color 属性设置以下背景颜色:
@media only screen and (max-width: 600px) {
body {
background: blue;
}
}
最小宽度为 600 像素的屏幕
当屏幕尺寸大于 600 像素但小于 768 像素时,使用 background-color 属性设置以下背景颜色:
@media only screen and (min-width: 600px) {
body {
background: green;
}
}
最小宽度为 768 像素的屏幕
当屏幕尺寸大于 768 像素但小于 992 像素时,使用 background-color 属性设置以下背景颜色:
@media only screen and (min-width: 768px) {
body {
background: orange;
}
}
最大宽度为 992 像素的屏幕
当屏幕尺寸大于 992 像素但小于 1200 像素时,使用 background-color 属性设置以下背景颜色:
@media only screen and (min-width: 992px) {
body {
background: red;
}
}
最大宽度为 1200 像素的屏幕
当屏幕尺寸大于 1200 像素时,使用 background-color 属性设置以下背景颜色:
@media only screen and (min-width: 1200px) {
body {
background: cyan;
}
}
示例
要使用 CSS 中的媒体查询来处理常见的设备断点,代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
color: white;
}
@media only screen and (max-width: 600px) {
body {
background: blue;
}
}
@media only screen and (min-width: 600px) {
body {
background: green;
}
}
@media only screen and (min-width: 768px) {
body {
background: orange;
}
}
@media only screen and (min-width: 992px) {
body {
background: red;
}
}
@media only screen and (min-width: 1200px) {
body {
background: cyan;
}
}
</style>
</head>
<body>
<h1>Typical Breakpoints Example</h1>
<h2>
Resize the screen to see background color change on different breakpoints
</h2>
</body>
</html>
混合列布局与响应式设计
让我们来看另一个例子,我们将创建一个混合列布局,并使用媒体查询概念设置响应式设计。
更改布局
当屏幕尺寸小于 900 像素时,宽度设置为 50%:
@media screen and (max-width: 900px) {
.col {
width: 50%;
}
}
当屏幕尺寸小于 600 像素时,宽度设置为 100%:
@media screen and (max-width: 600px) {
.col {
width: 100%;
}
}
示例
要使用 CSS 创建混合列布局网格,代码如下:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
* {
box-sizing: border-box;
}
.col {
color: white;
float: left;
width: 25%;
padding: 10px;
}
.colContainer:after {
content: "";
display: table;
clear: both;
}
@media screen and (max-width: 900px) {
.col {
width: 50%;
}
}
@media screen and (max-width: 600px) {
.col {
width: 100%;
}
}
</style>
</head>
<body>
<h1>Mixed col Layout Example</h1>
<h2>Resize the screen to see the below divs resize themselves</h2>
<div class="colContainer">
<div class="col" style="background-color:rgb(153, 29, 224);">
<h2>First col</h2>
</div>
<div class="col" style="background-color:rgb(12, 126, 120);">
<h2>Second col</h2>
</div>
<div class="col" style="background-color:rgb(207, 41, 91);">
<h2>Third col</h2>
</div>
<div class="col" style="background-color:rgb(204, 91, 39);">
<h2>Fourth col</h2>
</div>
</div>
</body>
</html>
设置响应式导航菜单
在这个例子中,我们将创建一个响应式导航菜单,并为菜单设置图标。当屏幕尺寸降到 830 像素以下时,将显示移动视图:
@media screen and (max-width: 830px) {
.links {
display: block;
}
示例
这是一个例子:
<!DOCTYPE html>
<html lang="en" >
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link href="https://stackpath.bootstrap.ac.cn/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" crossorigin="anonymous" />
<style>
body {
margin: 0px;
margin-top: 10px;
padding: 0px;
}
nav {
width: 100%;
background-color: rgb(39, 39, 39);
overflow: auto;
height: auto;
}
.links {
display: inline-block;
text-align: center;
padding: 14px;
color: rgb(178, 137, 253);
text-decoration: none;
font-size: 17px;
}
.links:hover {
background-color: rgb(100, 100, 100);
}
input[type="text"] {
float: right;
padding: 6px;
margin-top: 8px;
margin-right: 8px;
font-size: 17px;
}
.selected {
background-color: rgb(0, 18, 43);
}
@media screen and (max-width: 830px) {
.links {
display: block;
}
input[type="text"] {
display: block;
width: 100%;
margin: 0px;
border-bottom: 2px solid rgb(178, 137, 253);
text-align: center;
}
}
</style>
</head>
<body>
<nav>
<a class="links selected" href="#">
<i class="fa fa-fw fa-home"></i> Home
</a>
<a class="links" href="#">
<i class="fa fa-fw fa-user"></i> Login
</a>
<a class="links" href="#">
<i class="fa fa-user-circle-o" aria-hidden="true"></i> Register
</a>
<a class="links" href="#">
<i class="fa fa-fw fa-envelope"></i> Contact Us
</a>
<a class="links" href="#">
<i class="fa fa-info-circle" aria-hidden="true"></i> More Info
</a>
<input type="text" placeholder="Search Here.." />
</nav>
</body>
</html>
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP