Is there any equivalent to IE conditional comment for chrome and safari?

16,333

Solution 1

No, there are not.

You can hack it by doing browser detection in JS and attaching scripts/styles dynamically.

Or, if you are concerned only with having different css for different browsers, you can use css hacks. There are probably css hacks that work with the browsers you need.

Or, if the only thing you need to change is 'width' (of one css definition?) you can probably do it in jquery or javascript

jquery browser detection. see: http://docs.jquery.com/Utilities/jQuery.browser

Solution 2

I use this on every project: http://rafael.adm.br/css_browser_selector/

Although, if you are having to do a lot of targeting in Firefox or Webkit — you may want to reconsider how you are writing your HTML/CSS.

Solution 3

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Browsser Detection</title>

<link rel="stylesheet" href="Main.css" type="text/css">

<?php 

$msie        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') ? true : false; 
$firefox    = strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox') ? true : false;
$safari        = strpos($_SERVER["HTTP_USER_AGENT"], 'Safari') ? true : false;
$chrome        = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;

if ($msie) {
echo '
<!--[if IE 7]>
<link rel="stylesheet" href="ie7.css" type="text/css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" href="ie8.css" type="text/css">
<![endif]-->
';
}
if ($safari) {
echo '<link rel="stylesheet" href="safari.css" type="text/css">';
}

?>

</head>
<body>

    <br>
    <?php
    if ($firefox) { //Firefox?
    echo 'you are using Firefox!';
    }

    if ($safari || $chrome) { // Safari?
    echo 'you are using a webkit powered browser';
    }

    if (!$msie) { // Not IE?
    echo '<br>you are not using Internet Explorer<br>';
    }
    if ($msie) { // IE?
    echo '<br>you are using Internet Explorer<br>';
    }
    ?>

    <br>

</body>
</html>

From Chrome conditional comments

Share:
16,333
shin
Author by

shin

IB Diploma and MYP mathematics teacher who loves coding.

Updated on June 25, 2022

Comments

  • shin
    shin almost 2 years

    I am wondering if there is any thing which works like ie conditional comment for webkit.

    I want to change width.

    For example,

    <!--[if IE]>
    <link href="css/ie.css" rel="stylesheet" type="text/css" />
    <![endif]-->
    

    Thanks in advance.

  • mkoryak
    mkoryak over 14 years
    yes. try $.browser.msie == true. $.browser.mozilla == true etc. inspect the $.brower object or read docs
  • shin
    shin over 14 years
    After reading your reply, I found this and it works well. Thanks. tvidesign.co.uk/blog/…
  • Pal
    Pal almost 9 years
    But if I use email template and want to detect iPhone? I can't use scripts. Is there any way to hide or show some html markup specially designed for iPhone?