简体   繁体   English

如何根据桌面或移动视图设置 css 属性?

[英]How to set a css property depending on desktop or mobile view?

I would like to have 2 different margin for the same div depend on mobile and desktop view.我希望同一个 div 有 2 个不同的边距,这取决于移动和桌面视图。

The code below can perform a margin-left: 70px;下面的代码可以执行一个margin-left: 70px; on desktop.在桌面上。 The problem arises on mobile view, it doesn't reset to margin-left: 0px;问题出现在移动视图上,它不会重置为margin-left: 0px;

How can I do it ?我该怎么做 ?

Thanks.谢谢。

.body-container {
  padding-left: 0px;
  padding-right: 0px;
  max-width:1350px;
}
.content {
  margin-left: 70px;
}
.content-collapsed {
  margin-left: 0px;
}

You're not resetting margin in .content您没有在.content中重置边距

You need to use media queries您需要使用媒体查询

The solution below uses non-mobile approach下面的解决方案使用非移动方法

 body { margin: 0; } .content { background: red; margin-left: 70px; } @media (max-width: 800px) { /*choose the width you prefer*/ .content { margin-left: 0; } }
 <div class="content">content</div>

The solution below uses mobile approach (recommended, also is the same used by bootstrap 4)以下解决方案使用移动方法(推荐,也与 bootstrap 4 使用的相同)

 body { margin: 0; } .content { background: red; margin-left: 0; } @media (min-width: 800px) { /*choose the width you prefer*/ .content { margin-left: 70px; } }
 <div class="content">content</div>

You can use media queries ( MDN ).您可以使用媒体查询 ( MDN )。

Example :例子 :

 body { margin: 0; } div { width: 100px; height: 100px; border: 1px solid black; margin-left: 0px; /* mobile*/ } /* screen width higher than 750px */ @media screen and (min-width: 750px) { div { margin-left: 70px; } }
 <div></div>

Use media query.使用媒体查询。 Add:添加:

/* For mobile*/
@media only screen and (max-width: 768px) {
    div {
        margin-left: 0;
    }
}

This is the solution that worked the best for my case:这是最适合我的情况的解决方案:

    <style>
      .body-container {
        padding-left: 0px;
        padding-right: 0px;
        max-width:1350px;
      }
    
      .content {
        margin-left: 70px;
      }
    
      @@media (max-width: 800px) { /*choose the width you prefer*/
        .content {
        margin-left: 0px;
        min-width: 100%;
        }
      }
    </style>

Thank you.谢谢你。 You made my day.你让我今天一整天都感觉很好。

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

相关问题 根据移动或桌面 HTML 和 CSS 更改图像 - Changing Image depending on Mobile or Desktop HTML & CSS CSS代码的桌面视图和移动视图不起作用 - css code desktop view and mobile view is not working 如何在css中指定不同的图像,具体取决于用户是在桌面还是移动浏览器上访问 - how to specify a different image in css depending if the user visits on a desktop or a mobile browser 更正用于移动视图的自定义桌面CSS - Correcting custom desktop CSS for mobile view 移动和桌面视图的 CSS 对齐方式不同 - CSS alignment different for Mobile and Desktop view 内容重叠的背景图片取决于移动或桌面视图 - background image with overlapping content depending on mobile or desktop view 如何在移动视图中仅显示2张幻灯片和在桌面视图4张幻灯片中设置轮播滑块视图? - How to set carousel slider view in mobile view display only 2 slides and desktop view 4 slides? 删除CSS中用于移动视图但不用于桌面视图的模板中的填充和边距 - Removing padding and margins in template for mobile view but not for desktop view in CSS 如何使 css 在移动设备上显示图像但不在桌面上显示 - How to make css to show a image on mobile but not in desktop 如何在移动设备和桌面设备中查看响应式 iframe - How to view responsive iframe in both mobile and desktop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM