[英]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>
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.