掌握响应式设计的核心技术
@media screen and (max-width: 768px) {
body {
font-size: 14px;
}
}/* 基本媒体查询 */
@media screen and (min-width: 768px) {
/* 平板及以上设备的样式 */
}
/* 多个条件 */
@media screen and (min-width: 768px) and (max-width: 1024px) {
/* 平板设备的样式 */
}
/* 不同媒体类型 */
@media print {
/* 打印样式 */
body {
font-size: 12pt;
color: black;
}
}/* 移动设备默认样式 */
.container {
width: 100%;
padding: 10px;
}
/* 平板断点 */
@media screen and (min-width: 768px) {
.container {
padding: 20px;
}
}
/* 桌面断点 */
@media screen and (min-width: 1024px) {
.container {
max-width: 1200px;
margin: 0 auto;
}
}/* 桌面默认样式 */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
/* 平板断点 */
@media screen and (max-width: 1024px) {
.container {
max-width: 100%;
}
}
/* 移动设备断点 */
@media screen and (max-width: 768px) {
.container {
padding: 10px;
}
}| 设备类型 | 断点范围 | 建议媒体查询 |
|---|---|---|
| 移动设备 | 0 - 767px | 默认样式 |
| 平板 | 768px - 1023px | @media (min-width: 768px) |
| 小型桌面 | 1024px - 1199px | @media (min-width: 1024px) |
| 大型桌面 | 1200px+ | @media (min-width: 1200px) |
@media screen and (orientation: landscape) {
/* 横屏样式 */
}
@media screen and (orientation: portrait) {
/* 竖屏样式 */
}@media print {
/* 隐藏不需要打印的元素 */
.navbar,
.sidebar,
.footer {
display: none;
}
/* 设置打印样式 */
body {
font-size: 12pt;
color: black;
background: white;
}
/* 确保链接显示完整 URL */
a[href]::after {
content: " (" attr(href) ")";
font-size: 0.8em;
color: #666;
}
}.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-links {
display: flex;
gap: 20px;
}
.mobile-menu-btn {
display: none;
}
@media screen and (max-width: 768px) {
.nav-links {
display: none;
}
.mobile-menu-btn {
display: block;
}
}.responsive-demo {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.demo-item {
flex: 1 1 300px;
}
@media screen and (max-width: 768px) {
.demo-item {
flex: 1 1 100%;
}
}