Resolution breakpoints best practice - css

What is the best practice for using breakpoints in CSS? I need to have a mobile breakpoint, tablet (768*1024) and desktop.
Is it with sass variables? CSS custom properties it maybe other way?

You use media queries. https://www.w3schools.com/css/css_rwd_mediaqueries.asp
For example:
#media only screen and (max-width: 600px) {
body {
background-color: #f00;
}
/* css here will take effect when the query is true */
}
This will turn the background-color of body to #f00 (red) when the width of the screen is 600px or less.

So what I discovered is that there are 2 most common ways to work with resolution breakpoints. The first one is to write the breakpoints in scss variables and then use them as literal (interpolation).
$desktop: 'screen and (min-width:1024px)';
And then use it like so:
#media #{$dektop} {
color:red;}
The second way which I prefer is to use mixins with #content
#mixin on-dektop {
#media screen and (min-width:1024px) {
#content }
}
And use it like so:
#include on-desktop{
color:red;
}
There is an excellent article at css-tricks website
https://css-tricks.com/snippets/sass/mixin-manage-breakpoints

Related

Avoiding overlaps when querying CSS range features [duplicate]

Working a lot now with CSS media queries, I wondered in which order it's best to use them.
Method 1
#media only screen and (min-width: 800px) {
#content { ... }
#sidebar { ... }
}
#media only screen and (max-width: 799px) {
#content { ... }
#sidebar { ... }
}
Like this obviously the code is shorter, but with a lot of CSS you end up having the CSS of one container spread to multiple places in your stylesheet.
Method 2
#media only screen and (min-width: 800px) {
#content { ... }
}
#media only screen and (max-width: 799px) {
#content { ... }
}
#media only screen and (min-width: 800px) {
#sidebar { ... }
}
#media only screen and (max-width: 799px) {
#sidebar { ... }
}
Like this if you specify the screen size (at which the CSS is active) for each container a new, the overview in my humble opinion is much better.
But with a lot of CSS you will use the #media query dozens and dozens times.
Does the second method cause significantly longer load time or has any other disadvantages?
EDIT:
I might have been not clear enough. My question doesn't really concern the order or the queries as such or about overwriting CSS declarations.
What I wonder about is rather the norms how other people include the media query "statments" into their css.
Lets say I have only one breaking point where I switch some CSS.
So I have one media query for min:800px and a second for max:799px.
Should I use both query "statements"
#media only screen and (min-width: 800px) { ... }
#media only sreen and (max-width: 799px) { ... }
only once in my whole stylesheet and include ALL the CSS for ALL containers into the two media query "statments"?
Or is it okay as well to use the media query "statments" mutiple times?
I mean instead of making two seperate areas in the stylesheet (one for CSS above and one for below 800px), if there are any concerns about the method of using the media query "statments" instead multiple times (for each part of the page again, like for Content, Widgets etc to make them responsive)?
I would just like to have the CSS for above and below 800px in two different parts of my stylesheet.
I know that ofc both methodes are working, I am jsut curious about the norms and if using the media query "statements" dozens or hundreds of times within a CSS sheet (instead of just twice in the case I jsut mentioned) will increase the loading times?
My answer on how you should use media queries can be applied to your question:
Here is how you should use media queries:
Remember use the sizes you like/need. This below is just for demo
purposes.
Non-Mobile First Method using max-width:
/*========== Non-Mobile First Method ==========*/
#media only screen and (max-width: 960px) {
/*your CSS Rules*/
}
#media only screen and (max-width: 768px) {
/*your CSS Rules*/
}
#media only screen and (max-width: 640px) {
/*your CSS Rules*/
}
#media only screen and (max-width: 480px) {
/*your CSS Rules*/
}
#media only screen and (max-width: 320px) {
/*your CSS Rules*/
}
Mobile First Method using min-width:
/*========== Mobile First Method ==========*/
#media only screen and (min-width: 320px) {
/*your CSS Rules*/
}
#media only screen and (min-width: 480px) {
/*your CSS Rules*/
}
#media only screen and (min-width: 640px) {
/*your CSS Rules*/
}
#media only screen and (min-width: 768px) {
/*your CSS Rules*/
}
#media only screen and (min-width: 960px) {
/*your CSS Rules*/
}
Here is a good tutorial from W3.org
Based on your edited question:
I guess this depends on each developer and how they need/think to develop his/her project.
Here is what I use to do ** (when not not using Pre-compliers)**:
I create a file styles.css which includes the general styles that will apply to the project like this:
/*========== All Screens ==========*/
{
/*General CSS Rules*/
}
Then having the media queries below, either using the non-mobile or mobile approach method explained above (in my case I usual use the non-mobile approach method).
But, depending on the projects you may need to have some other breaks besides the "standard" which can led you to use the rules in the way you mentioned.
Plus there are developers who prefer to separate into 2 files, the one with general styles CSS and other one with media queries styles.
Important: There is one difference from creating a file with general styles + 1 media queries (min-width:800px or max-width:799px), then only having a file with 2 media queries (min-width:800px/max-width:799px), which is when you have the general rules it will apply to ALL widths, therefore you just need to set the rules for 1 media queries.
Based on your last comment, the answer I could give you would be opinion-wised, so the best I can do for you is to give you a few articles so you can have your own opinion on this topic:
How many media queries is too many?
Web Performance: One or thousands of Media Queries?
Debunking Responsive CSS Performance Myths
It means that, if you apply two rules that collide to the same elements, it will choose the last one that was declared, unless the first one has the !important marker
The second one will always display the content at 799px and whatever content has been styled as the style allocated for 799 rather than 800px if the device is 800px, in this case because it's 1px difference it doesn't make much different, but if you did it at around 200px different it would cause problems for your design.
Example:
if you have it this way:
#media (max-width: 800px) {
body {
background: red;
}
}
#media (max-width: 799px) {
body {
background: green;
}
}
The background would be green if the device is 799px in width or less.
if it was the other way round
#media (max-width: 799px) {
body {
background: red;
}
}
#media (max-width: 800px) {
body {
background: green;
}
}
if the device width was less than 799px the background would be green because no !important keyword has been defined.
when the !important keyword has been defined, result for example one will be the same
#media (max-width: 799px) {
body {
background: red; !important
}
}
#media (max-width: 800px) {
body {
background: green;
}
}
It won't take the processor longer unless the two elements collide. You'll be fine to vary min-width and max-width.
I suggest you to use the first method.
If you are developing a site mobile first then you won't need media queries for mobile but for tablet and desktop only.
//Mobile first
.your-mobile-styles-also-shared-with-tablet-and-desktop{
}
//Tablet
#media only screen and (min-width: 641px) {
#content { ... }
#sidebar { ... }
}
//Desktop
#media only screen and (min-width: 1025px) {
#content { ... }
#sidebar { ... }
}
If you are using a CSS pre-processor like SASS or LESS you can always create many LESS or SASS components that you will include in your main.less or main.scss / .sass file.
So each component will have not so many media queries and you can divide each component with some comments like shown above.
Your code this way will be easier to read and also much shorter, because all properties shared by tablet and desktop can be defined at the beginning of you CSS component file.

Can't get this media query working with boostrap 3 styles

I want to change the width of a div\grid via a media query for desktop users, but can't get the style to apply.
Here is the div in Chrome dev tools:
So I want to set the width of my .ticketInforHeader div. I tried to do this, but it does not do anything:
#media screen and (min-width: 992px) {
.ticketInfoHeader {
width 30%;
}
}
Try using
#media screen and (min-width: 992px) {
.ticketInfoHeader {
width 30% !important;
}
}
I guess if you're using Bootstrap's grid the width of the columns will be defined by the already existing classes like .col-md-4
You might need to add an !important to overwrite the Bootstrap style
#media screen and (min-width: 992px) {
.ticketInfoHeader.col-md-4 {
width 30% !important;
}
}
but that doesn't look really good in the code and I feel it breaks the logic of using bootstrap's grid.

How do I stop a media query forcing inheritance of both "left" and "right" css property on a div?

I have a media query and corresponding CSS that sets up a toolset on the right side of the screen in the case for desktop users and on the bottom for mobile. However, it overrides the CSS properties and assigns 0 to both "left" and "right". What I am doing wrong? Please see the screenshot from firebug below.
Here are my media tags:
/*
- Binds all layouts together with imports and mediaqueries.
*/
//#import "libs/reset.less";
#import "mediaqueries/global.less";
#import "mediaqueries/desktop.less";
#import "mediaqueries/mobile.less";
#media only screen and (min-width: 480px) {
.mqMobile; /* Use mix-in to make sure that the styles are expanded inside the mediaquery */
}
#media only screen and (min-width: 992px) {
.mqDesktop; /* Use mix-in to make sure that the styles are expanded inside the mediaquery */
}
For the mobile media query, use right: auto. This should override the normal CSS, and switch the right property from 0 without actually specifying a specific value. For example:
#media only screen and (min-width: 480px) {
.mqMobile {
left: 0;
right: auto;
}
}
U need to add media query in ur css file.
This will target all
#toolset{
float:left;
}
This will target mobile
#media only screen and (max-device-width: 480px) {
#toolset{
float:right;
}
}
Refer to this:
http://css-tricks.com/snippets/css/media-queries-for-standard-devices/

CSS3 Arithmetic

I have been using and learning CSS3 a fair bit of late and enjoying its many capabilities. Right now I am wondering if it is possible to setup a CSS rule that assigns a block element width conditionally. The sort of thing I am after - if the screenwidth is less than, say 500px, use a width of 320px otherwise use a width of, say, 80%, of screen size.
Yes, I realize I could do this sort of thing through JavaScript - just wondering if there isn't a more elegant CSS3 approach.
Yes, it is very much possible using CSS media queries - http://www.css3.info/preview/media-queries/
.mydiv {
width: 80%; /* normal case */
}
/* special case if screen width < 500 */
#media all and (max-width: 500px) {
.mydiv {
width: 320px;
}
}
#media only screen and (max-width: 500px) {
// CSS rules go here
}
#media only screen and (min-width: 501px) and (max-width: 959px) {
// CSS rules go here
}
#media only screen and (min-width: 960px) {
// CSS rules go here
}
etc...
From the W3C

responsive design css3 show on small screens not on large

new to css3 media queries and responsive design.
I would like to know how to show something (say a div) on small screens only but not on large screens.
I've tried something like:
#media (max-width: 480px) {
.show-on-small-only{ display:block; visibility:visible;}
}
...
and anything larger has eg:
#media (max-width: 768px) {
.show-on-small-only{ display:hidden; visibility:none;}
}
it doesn't seem to work as intended.
might be worth pointing out that i'm using bootstrap 2.0
It's a better practice to make all your default style mobile-friendly and then use min- media queries to size up:
div { /*put whatever your default styles are first*/ }
/* Then use the media query to hide it at 481 and wider */
#media all and (min-width:481px) {
div { display:none }
}
Look at 320andup and Skeleton and the CSS of this page for examples. Look at the helper classes towards the bottom of this CSS for differences between invisible/hidden etc.
You can put this first
/* for small screens, only execute in if statement */
#media only screen and (min-width : 320px) and (max-width : 768px) {
.smallOnly {
visibility:visible!important;
display:block!important;
}}
Then at the bottom of it put it for large screens (always execute since not in if statement)
.smallOnly {
visibility: none;
display: none;}
The important tg makes it so that anything with important always overwrite everything else and it will be the master rule regardless of where it is in the file.

Resources