Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP conditions depending on window width (media queries?)

I have a responsive website and I need some PHP conditions depending on the windows width (or media queries).

Example:

if ($window_width > 1400px) {
    echo 'Your window is wider than 1400px';
}

elseif ($window_width > 1000px) AND ($window_width < 1399px) {
    echo 'Your window is between 1000px and 1399px';
}

else {
    echo 'Your window is narrower than 1000px.';
}

Thanks for your help!

like image 419
user1706680 Avatar asked Sep 19 '13 08:09

user1706680


People also ask

How do you change the width of a media query?

Setting a particular "width-range" isn't any different from the way media queries are created. The only difference is the addition of more media feature expressions (that is, the screen width sizes). Take a look: @media only screen and (min-width: 360px) and (max-width: 768px) { // do something in this width range. }

How do you give a media query a minimum width and maximum width?

Combining media query expressions Max-width and min-width can be used together to target a specific range of screen sizes. @media only screen and (max-width: 600px) and (min-width: 400px) {...} The query above will trigger only for screens that are 600-400px wide.

Can media queries resize based on a div element instead of the screen?

They cannot be used to refer to a certain element on a page. If you need to apply styles depending on the size of a certain div element on your page, you'll have to use JavaScript to observe changes in the size of that div element instead of media queries.

What is @media rule?

The @media rule is used in media queries to apply different styles for different media types/devices. Media queries can be used to check many things, such as: width and height of the viewport. width and height of the device. orientation (is the tablet/phone in landscape or portrait mode?)


2 Answers

check this

Goolgle Mobile Detect

Try to use http://www.php.net/get_browser and check for isMobileDevice field. It might help only, of course, if the path to browscap.ini is set up in php.ini. If not, you can use php classes like https://github.com/garetjax/phpbrowscap

like image 97
Shakti Patel Avatar answered Oct 11 '22 03:10

Shakti Patel


Nope you can not do it with php. php is strictly server side

user javascript instead. Below is my code to get device resolution using javascript

<script>
     screenWidth = window.screen.width,
     screenHeight = window.screen.height;
    console.log(screenWidth);
    console.log(screenHeight);
</script> 
like image 22
Janak Prajapati Avatar answered Oct 11 '22 01:10

Janak Prajapati