简体   繁体   English

CSS 媒体在移动设备和桌面设备中隐藏/显示 div?

[英]CSS media to hide/show div in mobile and desktop?

I have this real code to show and hide two divs depending on device type:我有这个真实的代码来根据设备类型显示和隐藏两个 div:

Problem : in Android the #div-desktop is shown.问题:在 Android 中显示 #div-desktop。

  • I need to show only div#div-mobile on mobile devices我只需要在移动设备上显示 div#div-mobile

  • I need to show only div#div-desktop on desktop devices我只需要在桌面设备上显示 div#div-desktop

CSS CSS

        @media screen and (min-width: 0px) and (max-width: 700px) {
      #div-mobile {    display: block;  }
      #div-desktop {    display: none;  }
    }

    @media screen and (min-width: 701px) and (max-width: 3000px) {
      #div-mobile {    display: none;  }
      #div-desktop {    display: block;  }

}

HTML HTML

<div id="div-mobile">m<img width="100%" height="auto" src="http://uggafood.com/wp-content/uploads/2017/03/ugga-food_mobile.jpg" alt="" width="600" height="1067" /></div>


<div id="div-desktop">d<img width="100%" height="auto"  src="http://uggafood.com/wp-content/uploads/2017/03/ugga-food_desktop.jpg" alt="" width="1920" height="1280" /></div>

I have just checked the live link.我刚刚检查了实时链接。

Meta tag is missing for responsive devices.响应式设备缺少元标记。

Add the below code for detecting mobile devices.添加以下代码以检测移动设备。

<meta name="viewport" content="width=device-width, initial-scale=1.0">

EDIT After seeing your site, you need to add:编辑看到您的网站后,您需要添加:

<meta name="viewport" content="width=device-width, initial-scale=1">

You can just use min-width你可以只使用min-width

Also, don't use width/height html tags in img use CSS instead另外,不要在img使用width/height html 标签,而是使用 CSS


 img { max-width: 100% } #div-desktop { display: none; } @media screen and (min-width: 701px) { #div-mobile { display: none; } #div-desktop { display: block; } }
 <div id="div-mobile">m<img src="http://uggafood.com/wp-content/uploads/2017/03/ugga-food_mobile.jpg" alt="" /></div> <div id="div-desktop">d<img src="http://uggafood.com/wp-content/uploads/2017/03/ugga-food_desktop.jpg" alt="" /></div>

Change your media queries to the following.将您的媒体查询更改为以下内容。

Just change the widths to whatever you'd like.只需将宽度更改为您想要的任何值。 The top media query says if the min width is above standard mobile sizes show xyz , then the second one says if it's below do abc .顶部媒体查询说如果最小宽度高于标准移动尺寸显示xyz ,然后第二个说如果它低于 do abc

@media screen and (min-width: 769px) {

    #div-mobile { display: none; }
    #div-desktop { display: block; }

}

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

    #div-mobile { display: block; }
    #div-desktop { display: none; }

}

此行仅查找用户系统的大小分辨率并提供给您的 css 代码<meta name="viewport" content="width=device-width, initial-scale=1.0">

If you use sass, this is a very efficient way of effecting media queries and writing appropriate css per devise: https://eduardoboucas.github.io/include-media如果您使用 sass,这是一种非常有效的方式来影响媒体查询并为每个设计编写适当的 css: https : //eduardoboucas.github.io/include-media

include-media is a Sass library for writing CSS media queries in an easy and maintainable way, using a natural and simplistic syntax. include-media 是一个 Sass 库,用于以简单且可维护的方式使用自然和简单的语法编写 CSS 媒体查询。

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

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