Most guides recommend designing mobile first. I.e. adding css-rules for mobile as default and putting tablet/desktop rules into media queries:
.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.
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. I.e. 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. I.e. 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:
.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. For example, you mix two classes, one is responsible for stuff inside your element (i.e. color, fonts, and whether it is flexbox or block). 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.