简体   繁体   English

CSS 媒体查询将影响所有移动设备

[英]CSS Media Query to affect all mobile devices

Total NOOB at web development and trying to teach myself here and it's quite daunting to say the least, but I'm having fun nonetheless.网络开发方面的总菜鸟,并试图在这里自学,至少可以说这很令人生畏,但我仍然很开心。

Anyway, I know that media queries affect the way mobile devices render the page on various screen sizes, but I want to know if there's just one media query that can affect ALL mobile devices regardless of screen size?无论如何,我知道媒体查询会影响移动设备在各种屏幕尺寸上呈现页面的方式,但我想知道是否只有一个媒体查询可以影响所有移动设备而不管屏幕尺寸如何?

I just want to make sure it won't affect the Desktop.我只是想确保它不会影响桌面。

For instance I want to tweak a navigation menu on all mobile devices, but I don't want to meticulously change each media query that pertains to a screen size in my style.css.例如,我想在所有移动设备上调整导航菜单,但我不想在 style.css 中仔细更改与屏幕尺寸相关的每个媒体查询。

I just want to create one media query to make this tweak that will affect all mobile screen sizes.我只想创建一个媒体查询来进行这一调整,这将影响所有移动屏幕尺寸。

Hope that makes sense.希望这是有道理的。

As always, you all are awesome!一如既往,你们都很棒!

Thanks for your help!谢谢你的帮助!

happy that you are choose to learn Web-Development.很高兴您选择学习 Web 开发。

But your way sounds more complicated than it is.但你的方式听起来比实际复杂。 First, Desktop and Mobile can be the same at all.首先,桌面版和移动版可以完全相同。 It only counts down to Media Queries.它只倒计时到媒体查询。 On a Desktop, your Browser can be have the same width as a mobile device.在台式机上,您的浏览器可以与移动设备具有相同的宽度。 So you need to clarify in your Project at which point you want to show the User the "Mobile" Styles and when to display the "Desktop" Styles.所以你需要在你的项目中澄清你想在什么时候向用户显示“移动”样式以及何时显示“桌面”样式。 In most Projects I worked or saw, the default Media Queries are something like that:在我工作或看到的大多数项目中,默认的媒体查询是这样的:

@media (min-width: 320px) {}
@media (min-width: 768px) {}
@media (min-width: 1024px) {}
@media (min-width: 1220px) {}
@media (min-width: 1440px) {}

So you see on every media query you can attach some new styles for the selected query size.因此,您可以在每个媒体查询中看到,您可以为选定的查询大小附加一些新样式。 To make its easier for writing styles and don't override all these things on every new width, you can make something like that:为了更轻松地编写样式并且不要在每个新宽度上覆盖所有这些内容,您可以制作如下内容:

@media (min-width: 320px) {} // for general stylings (both, mobile && desktop)
@media (max-width: 767px) {} // for only styles between 320px and 768px (most mobile devies)
@media (min-width: 768px) {}  // general desktop && tablet styles if needed
@media (min-width: 768px) and (max-width: 1024px) {} // only tablet styles
@media (min-width: 1025px) // start with desktop styling

All these styles between the media queries are only attached to the sizes.媒体查询之间的所有这些样式仅附加到大小。 So just choose your needed width, for example:所以只需选择您需要的宽度,例如:

All mobile styles attached only between 320px and 1024px

@media (min-width: 320px) and (max-width: 1024px) {
  .nav{ background: red; }
}

All desktop styles attached only after 1025px
@media (min-width: 1025px) {
  .nav{ background: green; }
}

All these media queries just show the different widths, you also can do this by heights, but its way more difficult because of the device/display sizes.所有这些媒体查询只显示不同的宽度,您也可以按高度执行此操作,但由于设备/显示尺寸的原因,它的方式更加困难。

If you really want to check the User Agent and divide between the browser, agents, devices or something like that you will need JavaScript and thats way more complex than just display the styles for different widths.如果您真的想检查用户代理并在浏览器、代理、设备或类似的东西之间进行划分,您将需要 JavaScript,这比仅显示不同宽度的样式要复杂得多。

I hope this helps you!我希望这可以帮助你! If you have any questions about Media Queries and how to write them correctly, MDN is a good resource: MDN - Media Queries如果您对媒体查询以及如何正确编写它们有任何疑问,MDN 是一个很好的资源: MDN - 媒体查询

For anyone looking for a generic and easy media query for mobile, I would suggest the following:对于寻找通用且简单的移动媒体查询的任何人,我建议如下:

@media screen and (max-width: 767px) {}

Similar to the suggestion by @m4n0, but this is the correct query including the "and".类似于@m4n0 的建议,但这是包含“and”的正确查询。 This is a good start, and then you can continue to define more breakpoints as you need more responsiveness along the way.这是一个好的开始,然后您可以继续定义更多断点,因为您在此过程中需要更多的响应能力。

It depends on is your mobile layout is designed.这取决于您的移动布局是设计的。 As even in the mobile view you need to think about Portrait and landscape mode.即使在移动视图中,您也需要考虑纵向和横向模式。 For some common styling, I normally use对于一些常见的样式,我通常使用

@media screen  (max-width: 767px) { }

You can also use orientation to set media queries like below您还可以使用方向来设置媒体查询,如下所示

@media screen  and (max-device-width: 480px) and (orientation: portrait) { 
  Your classes here
}

@media screen  and (max-device-width: 640px) and (orientation: landscape)  {
  Your classes here
}

Great question, Android and Apple devices in my search normally fall within 450px in portrait and 800px on landscape, I would suggest you create a media query for both these sizes and you would have covered a high number of mobile devices in both portrait mode and landscape mode.好问题,我的搜索中的 Android 和 Apple 设备通常在纵向 450 像素和横向 800 像素内,我建议您为这两种尺寸创建媒体查询,并且您将涵盖大量纵向模式和横向模式的移动设备模式。 If you are targeting a specific device I would suggest looking up those specific screen viewport sizes and adjusting or adding more media queries to cover those cases.如果您的目标是特定设备,我建议您查找那些特定的屏幕视口尺寸并调整或添加更多媒体查询以涵盖这些情况。 Hope this helps!希望这可以帮助! Keep learning.保持学习。


Credit to following link for Popular Device Screen Resolution Sizes归功于以下流行设备屏幕分辨率尺寸的链接
https://mediag.com/blog/popular-screen-resolutions-designing-for-all/ https://mediag.com/blog/popular-screen-resolutions-designing-for-all/

Credit to following link for great explanation of Responsive Design归功于以下链接对响应式设计的精彩解释
https://www.toptal.com/responsive-web/introduction-to-responsive-web-design-pseudo-elements-media-queries https://www.toptal.com/responsive-web/introduction-to-responsive-web-design-pseudo-elements-media-queries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM