using #media queries in CSS for desktop and phone - css

I am developing a website which will be responsive in that different CSS styles are applied depending on the width of the users' browser.
the method chosen was to use CSS media queries.
my problem is this: when I use the following code
#media screen
and (min-width: 200px)
and (max-width: 800px)
{
#example
{
background: purple;
}
}
this works when I resize the window on my PC, but is not recognised by my phone whose resolution is within the limits.
perhaps more perculiarly, when I use the following code
#media screen
and (min-device-width: 200px)
and (max-device-width: 800px)
{
#example
{
background: purple;
}
}
this has the inverse effect: displays on phone, but not on PC.
as far as I have read there is no scope for an "OR" operator for something along the lines of the following to be valid
#media screen
and (
((min-width: 200px) and (max-width: 800px))
| / || / OR
((min-device-width: 200px) and (max-device-width: 800px))
)
{
#example
{
background: purple;
}
}
so my question is this: is there a way test responsive CSS on both desktop and phone simultaneously?
I have tried so many combinations:
using #media only screen,
using Android, Firefox and Chrome browsers on the phone,
but to no avail, the result is always the same.
The only way I can think to do this at the moment is to create two separate stylesheets, one for desktop and one for phone, but this would mean updating two stylesheets every time I wanted to view changes in the browser, which is impractical and counters the idea of responsiveness.
I looked into using the orientation: landscape/portrait target, but as far as I can make out this would again involve writing two sets of CSS.
One last consideration is that the website is currently using pure CSS; so no javascript, user-agent determination nor server-side scripting at this point in time.
This must be possible so any insights will be appreciated. I'm sure someone will have had the same problem and enlighten me.

Try using a viewport meta tag:
<meta name="viewport" content="width=device-width,initial-scale=1" />
Place that into the head section of your page.

Related

divi builder #media command

I am currently using DIVI Builder to build a simple website.
I have a fullwidth section, with a full width slider inside. The text inside the slider seems to behave differently on desktop versus mobile, so I figured i can help that with #media. What i have done so far is to create a duplicate, identical slide, adjust the 2nd of the two for mobile and hide the 2nd from desktop users. While I don't exactly know, I assume that this way will eventually slow down the page loading speed, so I resorted to #media.
The issue preventing this from working is that the media inquiry starts with:
#media all and (max-width: 980px) {
XXX {
margin-top: 100px;
}
}
The XXX represents the unknown for me, because I want to target the whole column, in this case automatically labeled as .et_pb_slide_0. From what I understand i cannot replace the XXX with a class, or in other words, something that starts with a . Is there any way to make this work ??
Media queries can contain classes like .et_pb_slide_0 or .anything_you_like!
This is valid:
#media all and (max-width: 980px) {
.et_pb_slide_0 {
margin-top: 100px;
}
}

Tablet Emulators, CSS Mobile Media Tags

I am using this code:
#media (min-device-width: 400px) and (max-device-width: 1020px) {
.gform_wrapper.two-column_wrapper ul.gform_fields.gform_column {float:none !important;}
}
When I view the website on the actual tablet, the code seems to work fine. However, when I view the site on a tablet emulator website, it doesn't work.
Am I doing something wrong with the syntax ?
As far as I understand the emulator is not treated as a "device", therefor the media query does not apply to it. The emulator is likely opening the website in an iFrame, so a simple min/max-width query will apply.
change this:
#media (min-device-width: 400px) and (max-device-width: 1020px) {
.gform_wrapper.two-column_wrapper ul.gform_fields.gform_column {float:none !important;}
}
to this:
#media (min-width: 400px) and (max-width: 1020px) {
.gform_wrapper.two-column_wrapper ul.gform_fields.gform_column {float:none !important;}
}
Here is why:
It is also possible to create queries based on *-device-width; though
this practice is strongly discouraged.
The difference is subtle but very important: min-width is based on the
size of the browser window, whereas min-device-width is based on the
size of the screen. Unfortunately some browsers, including the legacy
Android browser may not report the device width properly and instead
report the screen size in device pixels instead of the expected
viewport width.
In addition, using *-device-width can prevent content from adapting on
desktops or other devices that allow windows to be resized because the
query is based on the actual device size, not the size of the browser
window.

Is it possible to load a different image based on a media query to improve load times?

For example, say I have a div and set the background image to 'X' that is displayed only for desktop sizes. When the query changes to mobile I would set the background image to 'Y'.
Would doing this make it so that on a mobile only 'Y' gets loaded and not 'X'? 'X' is a large image and 'Y' is small, I am trying to improve load times. Also if on a desktop would 'X' and 'Y' get loaded or just 'X' and not 'Y'?
You can accomplish this by setting different background images based on the media query.
Unfortunately you cannot set the source of an image tag (only with javascript).
Example:
.container {
background-image: url(big.jpg);
}
#media all and (max-width: 699px) {
.container {
background-image: url(small.jpg);
}
}
You could use the CSS Media Type and screen size to display different backgrounds.
http://css-tricks.com/snippets/css/media-queries-for-standard-devices/
Yes this what media query do
You can create two css for every screen size for example:
#import url("test.css") screen and (min-width: 960px);
#import url("testmobile.css") screen and (max-width: 959px);
or you can add the media query on each css rule.
#media all and (max-width: 699px) and (min-width: 520px) {
}
Please check this link, it helps you

Improve 960gs adaptive CSS

Im using 960gs adaptive CSS in all the pages of my site. I have the script below in all the pages:
<script>
var ADAPT_CONFIG = {
path: '/css/960gs/',
dynamic: true,
range: [
'0px to 760px = mobile.min.css',
'760px to 980px = 720.min.css',
'980px to 1280px = 960.min.css',
'1280px to 1600px = 1200.min.css',
]
};
</script>
<script src="/js/adapt.min.js" type="text/javascript"></script>
My problem is that when a user go to another page, the selection of appropriate stylesheet is visible to the user specially when using Mozilla Firefox. What I mean is that at first, the layout takes all the screen and in less than a second, the lay-out changed to the appropriate one. I don't know how to describe this thing. But what I want is to improve it's performance in loading the appropriate stylesheet.
Thanks
On the 960.gs site it states
"A potential drawback of Adapt.js is the possibility of a brief flash of unstyled content as a new stylesheet is being fetched (think of it as “Ajax” for CSS). I have done my best to mitigate this by keeping CSS files small (3 KB). It is worth noting this is a proposed, not prescribed, approach to a problem with multiple solutions"
So this is a known issue. I have to say I'm not a big fan of doing this with javascript, CSS is perfectly capable of handling different resolutions. If you have your basic CSS and add some separate properties for handling different resolutions as follows:
#media only screen and (min-width: 980px) and (max-width: 1280px) { <<<CSS Here>>> }
#media only screen and (min-width: 760px) and (max-width: 980px) { <<<CSS Here>>> }
#media only screen and (max-width: 767px) { <<CSS for mobile portrait>> }
#media only screen and (min-width: 480px) and (max-width: 767px) { <<CSS for Mobile landscape>> }

While making responsive website which CSS we should keep outside media queries? Smaller one or bigger one?

I'm making a website which has 3 breakpoint 768px, 1024px and 1900 px. Which size of CSS is good to keep outside media query containers?
Adding example
All specific styling inside media queries and all common styling outside
h1 {color:red}
#media only screen and (min-width: 480px) {
h1 {font-size:18px}
}
#media only screen and (min-width: 768px) {
h1 {font-size:22px}
}
#media only screen and (min-width: 1024px) {
h1 {font-size:28px}
}
or
Most common used desktop first
#media only screen and (min-width: 1024x) {
h1 {font-size:28px; font-color:red}
}
#media only screen and (min-width: 480px) {
h1 {font-size:18px}
}
#media only screen and (min-width: 768px) {
h1 {font-size:22px; }
}
or
Mobile first
#media only screen and (min-width: 480px) {
h1 {font-size:18px; font-color:red}
}
#media only screen and (min-width: 1024x) {
h1 {font-size:28px; }
}
#media only screen and (min-width: 768px) {
h1 {font-size:22px; }
}
I believe you mean to ask what CSS should not be inside of the media query blocks, right?
If that is the case I recommend that any CSS that does not change be placed outside of the media query blocks. Any colors, font styling, etc. Any CSS that changes placement of elements, the padding, floats, inline or block display types, any structure-type CSS is what I would put in the media query blocks.
Update: To respond to the updated question, are you asking which order you should put the media blocks in? If that's the case as far as I know it doesn't really matter what order they go in. But to comment on the number of possible media queries, I would separate that CSS into different style sheets just to make it more maintainable. Your media queries would then be a part of the links to your style sheets in your HTML.
There are so many ways to approach this problem - and the decision may be different depending on the circumstances. For example, is there an existing site that you are reverse engineering to be responsive or are you starting from scratch?
STARTING FROM SCRATCH
If starting from scratch, one method is to create all of the basic styles OUTSIDE of any media query - so that these styles can be seen by any device (especially those devices that do not support media queries).
Basic styles could include just colors, and fonts etc - or it could be everything except layout.
Then, media queries are used to add the different layouts on top of the basic styles.
MIN or MIN AND MAX
The next question is how will you work your different media queries...
Will you allow them to be applied on top of one another - in which case you may start small and build up - using min-width only.
For example:
#media only screen and (min-width: 600px)
OR you may want to set them in a series of brackets - so that styes for one size do not interact with another size.
For example:
#media only screen and (min-width: 600px) and (max-width: 800px)
Again, there is no right or wrong - both have strengths and weaknesses. The first option allows you to use styles that flow through all widths. The second option allows you to fully control styles that appear in a specific width - without having to deal with the cascade.
DEALING WITH IE
There are a range of ways for dealing with older versions of IE including.
allow IE to see basic styles only
place media queries in separate CSS files and link to these files using media queries... then also link to a selection of these files (like wide screen CSS files only) via conditional comments.
Use some sort of JS solution like respond.js or others to force IE to understand the media queries.
HTH
I've read many articles recently that suggest starting with the smallest resolution first and working your way upwards using media queries. To me that also makes a lot of sense. The only problem is old browsers (IE) not understanding media queries. There are solutions to that problem though (if you Google).

Resources