Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ocorreiododiogo/7c36a237843fc8b4970d to your computer and use it in GitHub Desktop.
Save ocorreiododiogo/7c36a237843fc8b4970d to your computer and use it in GitHub Desktop.
A Pen by diogo oliveira.

CSS media queries in JS

Here's a small hack to easily reflect CSS media queries in JavaScript. I'm using the css content attribute in HTML and I'm not sure abou nasty consequences of this and if it works on every browser. Consider this Highly alpha.

A Pen by diogo oliveira on CodePen.

License.

<h1>resize the window</h1>
$(window).on('resize',function(){
var windowSize = $('html').css('content');
var myColor = '';
switch (windowSize) {
case 'desktop':
myColor = 'SkyBlue';
break;
case 'tablet':
myColor = 'YellowGreen';
break;
case 'mobile':
myColor = 'Salmon';
break;
}
$('p').text(windowSize);
$('body').css('backgroundColor',myColor);
});
html {
content: 'mobile';
}
@media (min-width: 600px) {
html {
content: 'tablet';
}
}
@media (min-width: 1024px) {
html {
content: 'desktop';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment