Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
Like Tree4Likes
  • 1 Post By TheGAME1264
  • 1 Post By mlseim
  • 1 Post By Ronald Roe
  • 1 Post By Ronald Roe

Thread: js and media queries

  1. #1
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times

    js and media queries

    I could be overlooking something pretty simple but ill post anyway.

    media queries are great, they allow us to set different styles for different devices, what more could we want as web developers? well imo there good but not great, the drawback being, having styles set for 5 different devices all in the one css file. so my question is first, is there a way to set one stylesheet per device size using some sort of attribute in the link tag, so only the relevant stylesheet is loaded or read. if the answer to that is no, would it be possible to use a js switch statement to append a link/stylesheet tag to the head of the document, case being device size?

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    You can do what you're asking for, but you really shouldn't.

    CSS3 Media Queries

    The reason is that every time you call a CSS file for a specific device size or whatever, that's an additional file the browser has to load...which leads to slowdowns.

    You're better off putting it all into one file. Most of the time your media queries won't be that large anyway if you're building something properly.
    busso likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    In your one CSS stylesheet file, you have sections that are selected based on device screen size.

    So, in essence, the styles are already automatically selected for you, from one file. Each section is run as the screen size changes.

    Code:
    body {
      background: white;    
    }
    @media screen and (min-width: 1020px) /* Desktop */ {
      body {
        background: red;
      }
    }
    @media screen  and (max-width: 979px) /* Tablet */ {
      body {
        background: blue;
      }
    }
    @media screen and (max-width: 500px) /* Mobile */ {
      body {
        background: green;
      }
    }
    @media screen and (max-width: 320px) /* Mobile */ {
      body {
        background: white;
      }
    }
    busso likes this.


  5. #4
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Unless you're working an unwieldy large project, it's actually better to just have it all in one file. It doesn't change the file size enough for it to be an issue. When you're talking in terms of a few kilobytes, it's a drop in a bucket.
    busso likes this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  6. #5
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    Quote Originally Posted by Ronald Roe View Post
    Unless you're working an unwieldy large project, it's actually better to just have it all in one file. It doesn't change the file size enough for it to be an issue. When you're talking in terms of a few kilobytes, it's a drop in a bucket.
    ...or a Wordpress child theme. Oh wait...you said unwieldy large project. Carry on.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  7. #6
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Child themes are for chumps who don't care about optimization.
    TheGAME1264 likes this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 06:24 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com