1

I've been working with a page which has two layouts dependent upon the width of the device being used to view the page:

/*Above is Mobile*/
@media screen and (min-device-width: 600px){
/*Below is Web*/

}

This approach essentially takes the various "web" and "mobile" divs throughout the page and displays, hides, or alters them as required for either layout; however, while the "web" divs are hidden, they are still loaded by the mobile device, potentially slowing down the page load.

My first thought was that if I could define only the "mobile" divs and not the "web" divs, then I could avoid loading all of these additional elements. Thus, does a method similar to the CSS media query exist for HTML? Or alternatively, is is there a way to define two different HTML layouts based on the width of the device the page is displayed on?


EDIT: A better approach, at least as far as images and graphics are concerned, is likely to use CSS to define the image rather than the HTML. Thus, instead of doing:

<div id="img"><img src="URL"></div>

...and trying to hide the div, you would instead take this approach:

<div id="img"></div>

and...

div#img {
background: none;
}

/*Above is Mobile*/
@media screen and (min-device-width: 600px){
/*Below is Web*/

div#img {
background: url(URL);
    height: 400px;
    width: 600px;
}
}

Thus, the mobile version doesn't load the images and we're still only using CSS.

Nick Mischler
  • 175
  • 5
  • 20
  • If you are using PHP, Python, Java, .NET, etc to serve up the pages then you can use detect functions to determine what form factor the user is accessing the website with. You can then redirect the user to a mobile version of the website or to a mobile template. – Lowkase Jul 09 '12 at 20:36
  • At the moment, it is strictly defined in HTML and should eventually be defined in PHP (which I can see working). Before I get that far however, I'd like to arrange something in, shall we say, "pure HTML". – Nick Mischler Jul 09 '12 at 20:38
  • Don't think you can do it in a pure html way. You could use javascript to detect the browser and only insert the html with it. – João Paulo Macedo Jul 09 '12 at 21:57

2 Answers2

0

Or alternatively, is is there a way to define two different HTML layouts based on the width of the device the page is displayed on?

Thats the route to take imho. HTML doesn't have a similar mechanism to define different rulesets for viewports.

Lowkase
  • 5,413
  • 2
  • 28
  • 47
0

I think there are some js options. Does this conversation help?

What is the best way to detect a mobile device in jQuery?

Community
  • 1
  • 1
imakeitpretty
  • 2,098
  • 5
  • 14
  • 16