0

I am using PHP to determine if a user is on a mobile device or desktop browser. Right now I have the statement set to echo a "yes" or "no." Those are both functioning correctly. However, I want to add certain CSS code if the device is mobile and certain CSS code if it is not. Is this possible?

Here is an example:

$ismobile = check_user_agent('mobile');
if($ismobile) {
    <div id="slider1">
         Sample Text
        </div>

} else {
    <div id="slide2">
        Sample Text 2
        </div>

Thanks!

3
  • just add <link rel="stylesheet" href="...." /> in the document head, using the same PHP if() structure. Commented Oct 21, 2013 at 16:14
  • #slider1 { background-color: red; } #slide2 { background-color: blue; }?? Commented Oct 21, 2013 at 16:14
  • 1
    The best way to deal with this issue is to use css media query instead of a mobile detection (same website for every devices). However, you can create two separate css files, that you can load with a condition. Otherwise, just add desktop + mobile css inside the same file it will work fine. Commented Oct 21, 2013 at 16:45

7 Answers 7

4
$ismobile = check_user_agent('mobile');
if($ismobile) {
    ?><link rel="stylesheet" type="text/css" href="mobile.css">
<?php
} else {
    ?><link rel="stylesheet" type="text/css" href="desktop.css">
<?php
}
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, just like you echo "yes" and "no", you can echo your CSS styles inside a HTML style tag, or, even better, load a CSS file (<link rel="stylesheet" type="text/css" href="mystyle.css">).

2 Comments

I like this answer and I think we can work from here. But I actually want to display a different div if it is mobile or a different div if it is a desktop browser. I don't think you can put a div in a stylesheet. Can you?
I figured this out on my own somehow (who has any idea how!) Regardless, I just needed to end my PHP tags and begin them again before and after my CSS code. Everything works perfectly now!
0

You can put a class on your body like <body class="mobile"> and then use css to target specifically the mobile or not

.mobile #mydiv {background:red} 

Comments

0

There are a few ways of doing this. I would first look into media queries because it is a bit of a neater solution and means you dont need to mix in php.

However the quick way would be

    <?php
    $ismobile = check_user_agent('mobile');
    if($ismobile) {?>
        <div id="slider1"> Sample Text</div>
    <?php } else { ?>
        <div id="slide2">Sample Text 2</div>
<?php } ?>

Comments

0

You could add two different style sheets:

<link rel="stylesheet" type="text/css" href="<?php echo check_user_agent('mobile') ? 'mobile' : 'desktop'; ?>.css">

Or just add a class to the body tag:

<body class="<?php echo check_user_agent('mobile') ? 'mobile' : ''; ?> another-body-class">

But you really should be using CSS Media queries which bases appearance off of screen resolution not the User Agent screen.

Comments

0
$ismobile = check_user_agent('mobile');
$style = $ismobile ? 'mobile' : 'notmobile';

echo '<div id="slider1" class='.$style.'>
   Sample Text
</div>';

Comments

0

While the other replies work, I like to slimline code, so I'd stick it on 2 lines and be done with it.

$ismobile = check_user_agent('mobile') ? 'mobile' : 'main' ;
echo '<link rel="stylesheet" type="text/css" href="' . $ismobile . '.css">';

The logic behind it is simple. You have 2 stylesheets, named main.css for the main template, while mobile.css is for the mobile layout.

With that in mind, $ismobile always returns true or false, ending in mobile.css or main.css, depending on return value.

As you will ALWAYS catch one of the 2 (true or false), you will always have a value for the included stylesheet.

The end result is that you have 2 stylesheets, rendering the need to have 2 divs, which duplicated the code at html level, since #slider can be defined with 2 separate sets of attributes in each, which means #slide2 isn't needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.