Can Media Query Using Dynamic Size on width? - html

I'm new when using media query, but I just found out when trying to develop a Desktop application (using electron but the CSS concept is still the same as normal web development) that there is a condition like this.
usually I'm using media screen like this (in this example I'm using 3 device as example: mobile, tablet, & desktop):
// default style for desktop
.my-default-css-code-is-here
// tablet
#media screen and (min-width: 768px) and (max-width: 1024px) {
.my-specific-css-code-for-tablet-is-here
}
// mobile
#media screen and (max-width: 767px) {
.my-specific-css-code-for-mobile-is-here
}
but my friend ask me like this:
"For this desktop application we want to have a layout like this: Normal Size (window-size): > 60% and when has a tablet size than at least the style will change if window around Tablet Size (window-size): <= 60% desktop resolution (Laptop or PC resolution such as HD, retina, etc. ) How can we achieve this?"
Since I'm usually using fixed size, I don't know if #media screen can achieve this. can someone help me about this problem? I'm quite confused myself about this. can I use Ratio like in this article ? or can I just apply it like this?
// Desktop
#media screen and (min-width: 60% from A full Resolution) and (max-width: 100% from A full Resolution) {
}
// Tablet
#media screen and (max-width: 59% from A full Resolution) {
}
or
// default css
.my-code-is-here {}
// Tablet
#media screen and (max-width: 59% from A full Resolution) {
.my-code-for-tablet-is-here {}
}
for more information, Let say if there is 2 type of laptop:
Laptop A :
Real resolution = 1368px, The it means 59% from real is 807px
Laptop B :
Real resolution = 2560px, The it means 59% from real is 1510px

if you want your media screen be dynamic. you can use npm package postcss-media-variables
this package allow you to use css variable in media query. you can set your css variable in javascript and use it in media query like :
:root {
--min-width: 1000px;
}
#media (min-width: var(--min-width)) {}
you can change your css variable --min-width in javascript

Although I'm not entirely sure to understand what you are trying to achieve, I can explain quickly about vw (width of the viewport) and vh (height of the viewport). These two values are relative to the current displayed viewport only and for that reason, will change if you are displaying the website on a desktop screen (lets say 1920px by 1080px) (vw by vh) compared to displaying the website on a tablet (much less for vw and vh).
Said another way, they don't depend on the device itself, whether it's a laptop or a mobile phone, but only on the actual active viewport (viewport = the browser windows size). More info here: https://www.w3schools.com/cssref/css_units.asp
From what I understand of your friend's request, you should/could still use fixed values for your media queries, customizing them to fit your website actual layout on each screen size.

Related

Media queries give a headache

My main smartphone is a Galaxy S8 Plus.
The media queries for this device are:
#media only screen and (min-width: 360px) and (orientation: portrait)
Let's start with the portrait orientation. This one, I'm understanding 100%, but here comes the problem.
This is the media query for landscape:
#media only screen and (min-width: 740px) and (orientation: landscape)
Everytime I code in this media query it applies to my desktop which has a 1920 * 1200 resolution. I know it's influenced by the min-width: 740px.
Now, my question is are:
How do I tackle this problem?
Can I create a single query that covers both portrait and landscape?
If so , what are the best practices for units in responsive web design? Right now I'm using vh and vw in my project, but I think it creates a mess sometimes.
And one last question: how do I cover most devices out there with a minimal use of queries?
Good CSS is minimal. Test my approach:
Global styles on top. For example font colors, font weights, backgrounds etc.
Then, use media queries:
#media screen and (max-width:1200px){
}
#media screen and (max-width:992px){
}
#media screen and (max-width:640px){
}
and so on... Higher widths are on top. In "mobile-first" approach, use min-width, and then lower widths are on top.
Try to avoid orientation property. Use this property only when you really need it.
vw and vh are convenient but remember that they are not supported on older browsers.
Bootstrap is good framework but you should learn how to make logic CSS from the scratch first. Keep up the good work.
To deal with the problem that it applies to desktop change min to max, there is a "standard" for what the media queries should be seen here, your media query described the medium size of < 768px for horizontal and very small size of < 576px
You don't need to include the orientation, you can simply write #media only screen and (min-width: 740px) then you apply for both, but you should have two media queries to make sure you cover both
vh and vw work best for creating responsive design, however if you are coding for IE then it might a problem, and you will need to find an alternativ to calculating height
Use Boostrap, it does everything for you almost

How to keep a webpage in tune with browser size ?

I apologize if I sound vague/ abrupt.
The web page should be in tune with the browser size. I mean if the browser size is reduced/ shrunk, the full web page should also shrink and should be visible in the shrunk browser window. Is this possible ?
You will need to use media queries for a responsive layout in your css
Example:
#media screen and (max-width: 699px) and (min-width: 300px) {
<css template for 300 to 699 px width goes here>}
You can also use bootstrap (a responsive framework which has predefined classes for what you may need) for a faster implementation.

How do I make a webpage respond to different scale widths?

I'm starting to learn how to make a 'Responsive web page' and have so far just used the width of the window the web page is displayed in to change the layout. I'm wondering if there's a way to detect the scale of the monitor it's displayed in (if it's 4:3, standard 16:9, 21:9, etc) to change the layout since 1440p (2560x1440) and Ultrawide 1080p (2560x1080) shares the same pixel width.
Try setting width and height in one media query, separated with the word "and":
#media (min-height: 500px) and (min-width: 580px) {
/* CSS stuff */
}
With such media query you can target different proportions of the screen - even while having same widths.
Easy. To do it in CSS, combine two media queries with the word and:
#media screen and (min-width: 1336px) and (min-height: 768px) {
/* Your code */
}
To do it in javascript, just divide screen.width by screen.heightto get the aspect ratio:
var aspectratio = screen.width / screen.height

Responsive device type targeting

All,
I am trying to develop a responsive site, but for some reason the media query I use for the ipad/tablet is also effecting the iphone/mobile. Are my dimensions wrong?
What is the best way to target all three device types?
Thanks
/desktop/
#media (min-width:1100px)
/ipad/
#media screen and (max-width: 1115px)
/iphone/
#media screen and (max-width: 767px)
This is a common problem in responsive design and there are many approaches that try to solve it. I myself find a 4-breakpoint layout to be the most fitting for most of the situations.
Phone: default
Phone-Landscape: min-width 480px;
Tablet: min-width 768px
Tablet-Landscape: min-width 1024px
Desktop: min-width 1260px
Think of min-width as meaning greater than or equal to and think of max-width as meaning less than or equal to.
By that logic your iPad rules (less than or equal than 1115px) are also going to affect the iPhone since it's screen is less than 1115px.
It sounds like you want to use an AND on your ipad rule to make it only affect rules that are bigger than your iphone rule set. Something like:
#media screen and (max-width: 1115px) and (min-width: 768px)
See https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries for more information

What is the best way to detect smaller devices like mobiles or tablets in CSS?

When i read about responsive design, people always seam to use this statement:
#media screen and(max-width: )
But mobile phones today seem to have really great resolution (often more than pc), whats the best way to detect small devices?
Thx ;=)
The screen resolution does not matter. The value used in media queries is the device width. For example:
My phone has a screen with a resolution of 1280x720 pixels. When held upright (in portrait mode) the width is 720px, but since it is an HD screen, it has a 200% ratio, and the resulting device width is 360px. This is the value used in media queries:
/* Even though my phone has a screen width of 720px… */
#media screen and (max-width: 360px) {
/*
* This code will apply
*/
}
#media screen and (min-width: 361px) {
/*
* This code will not apply
*/
}
The general rule is that phones in portrait mode have a device width less or equal to 400px, regardless of how many actual pixels their screen contains.
You can't directly query physical size.
You can, however, perform a media-type query for DPI along with Height and Width.
Example
#media(resolution: 326dpi) and (device-width: 640) and (device-height: 1136) {
// Iphone 5s
}
This should be a good starting point: List of displays by pixel density
Physical pixels and CSS pixels are not the the same on retina/HD mobile displays.
Research the viewport meta tag for information on device-width. i.e. <meta name="viewport" content="width=device-width"> is the CSS pixel width scaled at 100%.
See Viewport Device-Widths for a list of common mobile screen sizes.
When you are doing responsive design, you don't actually "detect" the screen size, rather you "target" various size using CSS Media Queries.
If you are using a library like Modernizer for example, that's when you are actually doing detection for various properties.