Hiding DIVs based on screen resolution [was: Help with PHP (I'm new)]
Discuss Hiding DIVs based on screen resolution [was: Help with PHP (I'm new)] in the CSS Help forum on Dev Shed. Hiding DIVs based on screen resolution [was: Help with PHP (I'm new)] Cascading Style Sheets (CSS) forum discussing all levels of CSS, including CSS1, CSS2 and CSS Positioning. CSS provides a robust way of applying standardized design concepts to your web pages.
Posts: 5
Time spent in forums: 21 m 23 sec
Reputation Power: 0
Help with PHP (I'm new)
This is probably an easy fix,
I want a div-tag on my site to appear only when a use screen resolution is greater than 1024px (it's a floating left "fixed" navigation div that doesn't look good on iPhone). Is this possible? Is PHP the right way to do it, or is it possible with CSS too?
Posts: 1,875
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 42 m 18 sec
Reputation Power: 813
Hi,
PHP doesn't know anything about the screen solution of the client, because it runs on the server and not in the browser (like JavaScript). All it knows is the user agent (browser), but this information isn't really reliable, because it can be set to anything.
Google for "mobile website" to check common practices of making websites ready for mobile devices.
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Posts: 16
Time spent in forums: 7 h 47 m 18 sec
Reputation Power: 0
Javascript is the easiest way
Javascript is the way to go about this.
Code:
function getPageWidth()
{
var html = document.getElementsByTagName('html');
var pageWidth = parseInt(getComputedStyle(html[0],"").getPropertyValue('width'));
return (pageWidth);
}
// For Browser width use
if (getPageWidth() < 1024) document.getElementById("floatNav").style.display = "none";
// For full screen width use
if (screen.width < 1024) document.getElementById("floatNav").style.display = "none";
Posts: 19,835
Time spent in forums: 6 Months 1 Day 22 h 10 m 33 sec
Reputation Power: 4192
In case it wasn't clear, what Paul-Ninja posted is an example of a Media Query tailored to answer this specific question.
Quote:
Originally Posted by Galadai
Javascript is the way to go about this.
Why use getComputedStyle? IE8 doesn't support it (to be fair, IE8 doesn't support CSS3 Media Queries either). Isn't it easier to use innerWidth and/or clientWidth instead?
The good thing here is that the media query is used to give a style to mobile devices, so it doesn't matter very much that old versions of IE will ignore it. (Though they will show that element even when the browser window less wide than 1024px, which is probably more likely for people who rarely upgrade.)