简体   繁体   English

设计Mobile First时如何处理`display:none`?

[英]How to deal with `display:none` when designing Mobile First?

Most guides recommend designing mobile first.大多数指南建议首先设计移动设备。 Ie adding css-rules for mobile as default and putting tablet/desktop rules into media queries:即为移动设备添加 css-rules 作为默认设置,并将平板电脑/桌面规则放入媒体查询中:

.class1 {
   width: 100px
}

@media only screen and (min-width: 800px) {
  .class1 {
     width: 200px
  }
}

the argument there is that mobile design is the simplest one and desktop would just add elements above it most of the time.那里的论点是移动设计是最简单的设计,而台式机大多数时候只会在其上方添加元素。 It made 100% sense for me, until I tried it.在我尝试之前,它对我来说是 100% 有意义的。
My problem is that I need to hide elements on mobile all the time.我的问题是我需要一直隐藏移动设备上的元素。 So the code ends up like this:所以代码最终是这样的:

.class2 {
   display: none
}

@media only screen and (min-width: 800px) {
  .class2 {
     display: block
  }
}

and this is very hard to manage, since not always I need display to be block, sometimes it's flex or something else, which can be defined in a completely different class, which I mix with the class, which does the hiding.这很难管理,因为并不总是我需要显示为块,有时它是 flex 或其他东西,可以在一个完全不同的类中定义,我与类混合,后者进行隐藏。 Ie I get conflicts all the time, results depends on the order I put my classes into stylesheet, and it's very easy to make a mistake.即我总是遇到冲突,结果取决于我将类放入样式表的顺序,而且很容易出错。 Since everyone recommends Mobile First I'm sure there is a work around this problem.由于每个人都推荐 Mobile First,因此我确信可以解决此问题。 Ie a way to hide the element by default (on mobile) and undo the hiding w/o making a conflict.默认情况下(在移动设备上)隐藏元素并在不产生冲突的情况下撤消隐藏的方法。 What is this way?这是什么方式?

If I simply use unset it unset property in all classes on the element:如果我只是在元素的所有类中使用unset it unset 属性:

 .flex { display: flex; background: yellow; } .class2 { display: none; background: red; } .class2 { display: unset; }
 <div class="class2 flex">I am not flex</div> <div class="flex">I am flex</div>

Edit: people are asking why I'm using two classes: I do it to separate responsibilities with, BEM.编辑:人们问我为什么使用两个类:我这样做是为了将职责与 BEM 分开。 For example, you mix two classes, one is responsible for stuff inside your element (ie color, fonts, and whether it is flexbox or block).例如,您混合使用两个类,一个负责元素内部的内容(即颜色、字体以及它是 flexbox 还是块)。 Another for the position of the element on your page and whether to show it.另一个用于元素在页面上的位置以及是否显示它。 The class which desides what to do with your element as a whole should not know whether this element uses flexbox or block layout.决定如何处理整个元素的类不应该知道这个元素是使用 flexbox 还是块布局。

I would do something like this.我会做这样的事情。 Creating classes for what you need, say i want my .something hidden on phone we would hide it by default and give it a class like md-flex to become display-flex at breakpoint md ( md can be anything you like ofcourse, maybe medium-flex ).为你需要的东西创建类,比如说我想要我的.something隐藏在手机上,我们默认情况下会隐藏它,并给它一个像md-flex这样的类在断点md成为display-flex (当然, md可以是你喜欢的任何东西,也许是medium-flex )。

this way you dont need to add everything into media queries, just these few classes to handle the display and maybe positions.通过这种方式,您不需要将所有内容添加到媒体查询中,只需这几个类来处理显示和位置。 leaving the queries in the bottom of the css files and working top side of the website == top side in css file works fine.将查询保留在 css 文件的底部并在网站的顶部工作 == css 文件中的顶部工作正常。 for example例如

  • header before标题前
  • header标题
  • header after标题后
  • content内容
  • sidebar侧边栏
  • footer before之前的页脚
  • footer页脚
  • footer after之后的页脚

 .something { display: none; background: yellow; } /*this could be md for example*/ @media only screen and (min-width: 900px){ .md-flex{ display: flex; } .md-inline{ display: inline-block; } } /*this could be lg for example*/ @media only screen and (min-width: 1280px){ .lg-block{ display: block; } }
 <div class="something md-flex">I am hidden on phone</div> <div class="something lg-block">I am hidden on phone</div> <div class="something md-inline">I am hidden on phone</div>

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

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