deviant art

Deviant Login Shop
 Join deviantART for FREE Take the Tour
[x]
more ▶

Featured in Groups:

Details

October 4, 2012
Sta.sh Writer
Link
Thumb

Statistics

Comments: 40
Favourites: 29 [who?]

Views: 10,500 (8 today)
[x]

Media queries and you

Thu Oct 4, 2012, 2:53 PM
First, a quick primer, which probably consists of information that most of you already know.

So, you want to write an article with cool CSS, and you want to make it work nicely when it's displayed on the tiny screen on your smartphone. Tricky! However, with media queries it becomes pretty easy. By writing

.text { color: blue; }
@media only screen and (max-width:480px) {
    .text { color: red; }
}

...suddenly you have blue text at normal resolutions, and red text on a smartphone. Or on a browser which has been resized down to be really narrow.

In fact, try it out on this article! If you resize your window right now to make it narrower, it should magically invert its colors. Madness!

As an example of where this might be particularly useful, let's consider the #hq site update articles with that big ol' sidebar.

It looks like:

Untitled by $kemayo

If we add this CSS:
@media only screen and (max-width:480px) {
    .sidebar { display:none; }
    .text { padding-left:33px; }
    .gr-box { background-image:none; }
}

It can now look like:

Untitled by $kemayo


This is awesome, and makes writing skins targeting multiple resolutions far easier. Unfortunately, dA skins don't support it. It'd be a pain to add in support for writing arbitrary media queries, because our CSS parser just plain isn't set up to handle it at the moment.

So we're adding in a way to target mobile resolutions with a special class.

Instead of @media only screen and (max-width:480px), you'll add a class selector like .da-media[mobile], and it'll be magically transformed onto a media query wrapping that rule.

The above example becomes:

.da-media[mobile] .sidebar { display:none; }
.da-media[mobile] .text { padding-left:33px; }
.da-media[mobile] .gr-box { background-image:none; }

Right now [mobile] is the only media type supported. We may add others in the future, which is why we decided to have it explicitly stated in the current code.

A few things to keep in mind:
  • It's called "mobile", but that doesn't mean it'll only trigger when you're viewing the mobile version of dA. Anyone with a narrow enough browser window will see it. You can even watch it switching back and forth by resizing your window.
  • "Narrow enough" in this case means 767px. We might choose to tweak this value in the future, if it seems to make sense to do so.
  • This still won't help with pages like the blog index page, where you can have a pretty wide screen and still be forced into a narrow column.
Enjoy.


Add a Comment:
 
love 0 0 joy 3 3 wow 0 0 mad 0 0 sad 0 0 fear 0 0 neutral 0 0
:icontheal:
*TheAL Apr 28, 2013  Hobbyist Digital Artist
Are queries usable in all places CSS is allowed? I tried applying these to my journal and it said they're not allowed. :(
Reply
:iconasjjohnson:
~AsjJohnson Nov 30, 2012  Hobbyist General Artist
Hey, um... I tried using that for my journal, and for some reason when I click the journal header from my profile page, it messes up. Or at least, the footer part of it messes up. Any idea why?
This is the code I used:
.da-media[mobile] .gr-top {background: url("http://fc01.deviantart.net/fs70/f/2012/223/b/5/daskinimg1_by_asjjohnson-d58q4ko.png") no-repeat 97% top; background-size: 85%;
}
.da-media[mobile] .lowertext {background: url("http://fc01.deviantart.net/fs71/f/2012/223/a/c/daskinimg2_by_asjjohnson-d58qb7s.png") no-repeat 97% 100%; background-size: 109%;
}

The images I used for the header and footer, I noticed they get cut off when resizing a window, so I thought that mobile code would help with that. And I thought it was working okay, except, I found that one specific link messes it up, as though the window thinks it's smaller than it is or something, and the 109% code makes the footer huge. It doesn't seem to effect the header image, though.
Reply
:icontifa22:
*Tifa22 Oct 27, 2012  Hobbyist Digital Artist
Thanks for sharing! :)
Reply
:iconbanane9:
Mood: Joy ~Banane9 Oct 23, 2012  Hobbyist Digital Artist
I dont write skins, but still awesome :D
Reply
:icondeviant-garde:
Didn't actually know about media queries; interesting. Speaking of mobile, is it true that the mobile website serves a page of the exact same size as the same page on the desktop website?
Reply
:iconbanane9:
Mood: Optimism ~Banane9 Oct 23, 2012  Hobbyist Digital Artist
it depends. if you would go to facebook.com with your mobile for example you would get the same as on a desktop. if you would go to m.facebook.com though it would be way smaller and 0.facebook.com doesnt even have images, so it really depends.
Reply
:icondeviant-garde:
I was talking about dA in particular.
Reply
:iconbanane9:
~Banane9 Oct 23, 2012  Hobbyist Digital Artist
if it looks the same its the same size :P
Reply
:icondeviant-garde:
The size of the page sent to the requester, not the literal physical appearance of the elements on the page.
Reply
:iconbanane9:
~Banane9 Oct 23, 2012  Hobbyist Digital Artist
yea i mean that. if the page looks like the one on your desktop on your mobile its the same amount of data :P
Reply
Add a Comment: