如何你写了一个css媒体查询?(you write a css media query?)

响应式网页设计是一种构建网页的方法,这些网页可以根据访问者的屏幕大小动态更改其布局和外观。大屏幕接收适合大屏幕显示的布局,而较小的设备(如移动电话)接收以适合小屏幕的方式格式化的相同网站。这种方法为所有用户提供了更好的用户体验,甚至有助于提高搜索引擎排名。CSS媒体查询是响应式web设计的重要组成部分。...

响应式网页设计是一种构建网页的方法,这些网页可以根据访问者的屏幕大小动态更改其布局和外观。大屏幕接收适合大屏幕显示的布局,而较小的设备(如移动电话)接收以适合小屏幕的方式格式化的相同网站。这种方法为所有用户提供了更好的用户体验,甚至有助于提高搜索引擎排名。CSS媒体查询是响应式web设计的重要组成部分。

CSS logo

媒体查询就像网站CSS文件中的小条件语句,允许您设置某些CSS规则,这些规则只有在满足特定条件时才会生效,例如屏幕大小高于或低于特定阈值时。

媒体查询现在是标准的,但非常旧的internet explorer版本不支持它们。

行动中的媒体查询

从没有任何视觉样式的结构良好的HTML文档开始。

在CSS文件中,设置页面样式并设置网站外观的基线。要使页面的字体大小为16像素,请编写以下CSS:

body { font-size: 16px; } To increase that font size for larger screens that have ample real estate to do so, start a Media Query like this: @media screen and (min-width: 1000px) { } This is the syntax of a Media Query. It starts with @media to establish the Media Query itself. Next, set the media type, which in this case is screen. This type applies to desktop computer screens, tablets, phones, etc. End the Media Query with the media feature. In our example above, that is mid-width: 1000px. This means that the Media Query kicks in for displays with a minimum width of 1000 pixels wide. After these elements of the Media Query, add an opening and closing curly brace similar to what you would do in any normal CSS rule. The final step to a Media Query is to add the CSS rules to apply after this condition is met. Insert these CSS rules between the curly braces that make up the Media Query, like this: @media screen and (min-width: 1000px) { body { font-size: 20px; } When the conditions of the Media Query are met (the browser window is at least 1000 pixels wide), this CSS style takes effect, changing our site’s font size from the 16 pixels we established originally to our new value of 20 pixels.

Adding More Styles

Place as many CSS rules within this Media Query as needed to adjust your website’s visual appearance. For example, to not only increase the font size to 20 pixels, but also change the color of all paragraphs to black (#000000), add this: @media screen and (min-width: 1000px) { body { font-size: 20px; } p { color: #000000; } }

Adding More Media Queries

Additionally, you can add more Media Queries for every larger sizes, inserting them into your styles sheet like this: @media screen and (min-width: 1000px) { body { font-size: 20px; } p { color: #000000; { } @media screen and (min-width: 1400px) { body { font-size: 24px; } } The first Media Queries kick in at 1000 pixels wide, changing the font size to 20 pixels. Then, once the browser was above 1400 pixels, the font size would change again to 24 pixels. Add as many Media Queries as needed for your particular website.

Min-Width and Max-Width

There are generally two ways to write Media Queries—by using min-width or with max-width. So far, we have seen min-width in action. This approach activates Media Queries after a browser reaches at least that minimum width. So a query that uses min-width: 1000px applies when the browser is at least 1000 pixels wide. This style of Media Query is used when you are building a site in a mobile-first manner. If you use max-width, it works in the opposite manner. A Media Query of "max-width: 1000px" applies after the browser has fallen below this size.

  • 发表于 2021-09-04 21:15
  • 阅读 ( 204 )
  • 分类:IT

你可能感兴趣的文章

如何建立一个网站:初学者

...许你读过我们的一些HTML(理解HTML)和CSS教程,但不知道如何在更大的项目中使用这些语言。 ...

  • 发布于 2021-03-15 13:25
  • 阅读 ( 248 )

什么是javascript?它是如何工作的?

...统编程语言的特殊特性。我们将深入研究它是什么,它是如何工作的,以及你能用它做什么。我们把它分解一下。 ...

  • 发布于 2021-03-18 04:01
  • 阅读 ( 266 )

使用css格式化文档以便打印

... 相关:如何在CSS中设置背景图像 ...

  • 发布于 2021-03-28 16:05
  • 阅读 ( 228 )

德鲁帕尔7(drupal 7)和德鲁帕尔8(drupal 8)的区别

关键区别:Drupal8与Drupal7有许多不同之处,一个主要区别是它将默认的主题引擎从PHPTemplate替换为自己的Twig。drupal8具有更好的CSS和JavaScript集成,但是它也改变了添加它们的方式。它还推出了许多新功能。 Drupal是一种内容管理...

  • 发布于 2021-07-12 13:54
  • 阅读 ( 305 )

如何创建网站第五部分:附加资源

本周我们向您展示了如何使用HTML和CSS创建网站。。。但是现在呢?这里有一些额外的资源和方法,你可以用来了解更多。第一件事:如果你想要所有课程的笔记都是PDF格式的,你可以在这里下载。接下来,您应该知道,也许学...

  • 发布于 2021-07-24 22:32
  • 阅读 ( 145 )

如何创建网站第二部分:样式和css

在我们关于如何创建网站的夜校系列的第一部分中,我们学习了一些关于网页的基本知识:HTML。在今天的课程中,我们将开始使用样式和CSS将衣服放到我们的网站上。上面的视频将引导您了解将样式应用于HTML文档中元素的三种...

  • 发布于 2021-07-24 23:08
  • 阅读 ( 280 )

如何检测网页上移动设备的点击(detect hits from mobile devices on web pages)

...适合手机和其他移动设备的体验。 一旦你花时间学习如何为手机设计网页,并实施你的策略,你还需要确保网站的访问者能够看到这些设计。有很多方法可以做到这一点,有些方法比其他方法更好。下面是一个你可以用来在...

  • 发布于 2021-09-04 21:57
  • 阅读 ( 169 )

如何阻止使用css打印网页(block a web page from printing with css)

...网页吗?也许你没有。如果是这样,你有一些选择。 如何阻止使用css打印网页 使用CSS很容易防止人们打印您的网页。您只需创建一个名为“print.css”的单行样式表,其中包括以下一行css。 body { display: none; } 这一样式将...

  • 发布于 2021-09-05 00:22
  • 阅读 ( 199 )

如何插入css注释(insert a css comment)

每个网站都由结构、功能和风格元素组成。级联样式表决定了网站的外观(“外观”)。这些样式与HTML结构分开,以便于更新和遵守web标准。 样式表的问题 随着当今许多网站的规模和复杂性,样式表可能变得相当冗长和繁...

  • 发布于 2021-09-05 00:33
  • 阅读 ( 172 )

如何将响应性背景图像添加到网站(add responsive background images to a website)

...小”属性使这成为可能。 在另一篇文章中,我们介绍了如何使用CSS3属性background size来拉伸图像以适应窗口,但是有一种更好、更有用的方法来部署此属性。为此,我们将使用以下属性和值组合: background-size: cover; cover关...

  • 发布于 2021-09-08 03:44
  • 阅读 ( 130 )