Detect screen width with CSS Media Queries
You can use device-width
which will test of the screen's width in px. That however is not entirely recommended. Use max-width
and min-width
(for the viewport) instead.
If you are trying to GET the screen width and use it (something like content: (device-width);
of some sort, that's not possible. Stick with JavaScript.
Manual Reference
How can I detect which window dimension is bigger just using CSS media query?
You can use an orientation media query:
@media all and (orientation:portrait) { … }
@media all and (orientation:landscape) { … }
From the W3C media Queries spec:
The ‘orientation’ media feature is ‘portrait’ when the value of the ‘height’ media feature is greater than or equal to the value of the ‘width’ media feature. Otherwise ‘orientation’ is ‘landscape’.
But note that it usually make more sense to worry about the actual width than the orientation, see this article on QuirksBlog for details.
Media query about screen size instead of resolution
Well, for starters, the CSS pixel is an angular measurement and is decently normalized between devices. Not entirely, but enough so for this to be a non-issue in most cases.
Personally I measure media queries in em
s so that they're relative to my font size. I mean, people usually visit a web site to read the text found on the website, so as long as there's a reasonable amount of words per line I'm satisfied.
You really shouldn't measure with physical (device) widths because people may have UI elements taking up space (or simply not run their browsers in full screen).
Media query to detect a small (in inch) but high-width (in pixels) screen
Use resolution
in your media query to distinguish between devices of different pixel densities.
For instance to tend to a 288 dpi device where one logical pixel is three hardware pixels, use one of these
@media screen and (min-resolution: 3dppx) { ... }
@media screen and (min-resolution: 288dpi) { ... }
where dppx means dots per pixel, and dpi, of course, dots per inch.
See W3C or MDN.
CSS media query to detect width double the height?
See the min-aspect-ratio
and max-aspect-ratio
CSS media queries:
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries
You can then use CSS to specify a different image (as a background image) based on the aspect ratio.
/* regular, default image */
#myImage {background-image: url(...)}
/* image to use when screen width is more than double the height */
@media screen and (min-aspect-ratio: 2/1) {
#myImage {background-image: url(...)}
}
What is the media query for large desktops?
The challenges of optimizing for large-scale displays center around how to scale and manage content.
You need to assume screen width at certain points.
Example: for class "container"
@media screen and (min-width: 1400px) {
.container {
width: 1370px;
}
}
@media screen and (min-width: 1600px) {
.container {
width: 1570px;
}
}
@media screen and (min-width: 1900px) {
.container {
width: 1870px;
}
}
Related Topics
Multiple Attributes in Ng-Style
How to Set The Width and Height of a Textarea Using CSS
CSS: Not(), Selectors and Selecting Descendants
Cannot Stretch Svg Background Image, Aspect Ratio Will Be Preserved
Text-Shadow and Box-Shadow While Printing (Chrome)
CSS Change Border Color on Hover
:After,: Before Issues in Internet Explorer 11
How to Detect If Webp Images Are Supported via CSS
What Is The Limit of Character to Use in Alt="Text" According to Wcag 2.0
How to Change The Color of The Text Cursor in an Input Field in Ie
CSS Buttons Rounded on One Side
Chang Font Size Only for a Single Character with CSS
-Webkit-Text-Fill-Color: Transparent; Not Working in Safari 7.1.7
CSS Positioning Absolute Within Table Cells Not Working in Firefox
Remove CSS Filter on Child Elements
Twitter Bootstrap Tooltip Not Aligning Correctly When It Is Going to Go Outside of Window
How to Make The Native 'Browse' Button on a File Input Larger Cross Browser
Cross-Browser CSS for Left Align and Right Align on The Same Line