跳转到帖子
在手机APP中查看

一个更好的浏览方法。了解更多

OKCCN - XenForo & IPS Plugin Marketplace

主屏幕上的全屏APP,带有推送通知、徽章等。

在iOS和iPadOS上安装此APP
  1. 在Safari中轻敲分享图标
  2. 滚动菜单并轻敲添加到主屏幕
  3. 轻敲右上角的添加按钮。
在安卓上安装此APP
  1. 轻敲浏览器右上角的三个点菜单 (⋮) 。
  2. 轻敲添加到主屏幕安装APP
  3. 轻敲安装进行确认。
  • 选择语言

Responsive Web Design - Media Queries

推荐的帖子

  • 行政经理

Responsive Web Design - Media Queries


CSS Media Queries

CSS media queries allow you to apply styles based on the characteristics of a device or the environment displaying the web page.

CSS media queries are essential for creating responsive web pages.

The CSS @media rule is used to add media queries to your style sheet.


Use Mediaqueries to Add a Breakpoint

On the previous page we made a grid layout web page. Now we want to add a breakpoint with mediaqueries to rearrange the grid items on bigger screens.


Mobile phone

Desktop

Example

Here we use a media query to add a breakpoint at 600px:

@media (min-width: 600px) {
  .header {grid-area: 1 / span 6;}
  .menu {grid-area: 2 / span 1;}
  .content {grid-area: 2 / span 4;}
  .facts {grid-area: 2 / span 1;}
  .footer {grid-area: 3 / span 6;}
}
Try it Yourself »


Another Breakpoint

You can add as many breakpoints as you like.


Desktop

Tablet

Phone

Example

Here we use media queries to add breakpoints when screen is min 600px, and when screen is min 768px :

@media (min-width: 600px) {
  .header {grid-area: 1 / span 6;}
  .menu {grid-area: 2 / span 1;}
  .content {grid-area: 2 / span 4;}
  .facts {grid-area: 3 / span 6;}
  .footer {grid-area: 4 / span 6;}
}

@media (min-width: 768px) {
  .header {grid-area: 1 / span 6;}
  .menu {grid-area: 2 / span 1;}
  .content {grid-area: 2 / span 4;}
  .facts {grid-area: 2 / span 1;}
  .footer {grid-area: 3 / span 6;}
}
Try it Yourself »

Typical Device Breakpoints

There are tons of screens and devices with different heights and widths, so it is hard to create an exact breakpoint for each device. To keep things simple you could target five groups:

Example

/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {...}

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {...}

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {...}

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {...}

/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {...}
Try it Yourself »

Media Queries for Screen Orientation

Media queries can also be used to change the layout of a page depending on the orientation of the screen.

Here, we change the background-color of the body, if the screen orientation is in landscape mode:

Example

@media only screen and (orientation: landscape) {
  body {
    background-color: lightblue;
  }
}
Try it Yourself »

Hide Elements With Media Queries

Here, we use media queries to hide an element on small screens:

I will be hidden on small screens.

Example

/* Hide element if the viewport width is 600px or less */
@media screen and (max-width: 600px) {
  #div1 {
    display: none;
  }
}
Try it Yourself »

Change Font Size With Media Queries

Here, we use media queries to change the font size of an element on different viewport widths:

Example

/* If viewport width is 600px or more, set font-size to 80px */
@media screen and (min-width: 600px) {
  #div1 {
    font-size: 80px;
  }
}
Try it Yourself »

Media Queries for User Preferences

Some users have motion sensitivity and prefer websites with less animation.

The prefers-reduced-motion media feature lets you check if a user has asked to reduce motion, such as animations or transitions. Use this feature to turn off animations and transitions for the users who has activated this setting on their computer:

Example

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}
Try it Yourself »


CSS @media Reference

For a full overview of all the media types and features/expressions, please look at the @media rule in our CSS reference.


参与讨论

你可立刻发布并稍后注册。 如果你有帐户,立刻登录发布帖子。

游客
回帖…

帐户

导航

搜索

搜索

配置浏览器推送通知

Chrome (安卓)
  1. 轻敲地址栏旁的锁形图标。
  2. 轻敲权限 → 通知。
  3. 调整你的偏好。
Chrome (台式电脑)
  1. 点击地址栏中的挂锁图标。
  2. 选择网站设置。
  3. 找到通知选项并调整你的偏好。