How to target Safari for Mac only?

后端 未结 10 1870
终归单人心
终归单人心 2020-12-05 05:39

Hi I\'ve cross browser fixed a site on all thinkable PC browsers, including Safari. Now my smart ass designer sent me a screen shot of the whole layout collapsing on mac. I

相关标签:
10条回答
  • 2020-12-05 05:55

    Use this in css. it will be rendered only in Safari

    /*\*/
      .some-class {
        /* Here comes some options */
      }
    /**/
    
    0 讨论(0)
  • 2020-12-05 05:58

    The user agent string contains operating system information, and you are probably already checking the user agent string for the browser type.

    A mac browser will have the string "Mac OS X 10." in the user agent string.

    0 讨论(0)
  • 2020-12-05 05:58
    @media screen and (-webkit-min-device-pixel-ratio:0) {
         /* Add your Safari-specific styles here. */
    }
    
    0 讨论(0)
  • 2020-12-05 06:08

    I think the selected right answer is outdated and does not work now.

    Here is the output of navigator.vendor

    Safari on iPad

    Mozilla/5.0 (iPad; CPU OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F69 Safari/600.1.4

    Safari on Mac

    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5 Safari/600.1.4

    As you can see Mac appears in navigator.vendor of both scenarios.

    Here is my version

    var isSafariMac = /Safari/.test(navigator.userAgent) && 
                      !/Mobile/.test(navigator.userAgent) && 
                      /Apple Computer/.test(navigator.vendor);
    

    So you can use this to target Safari on Mac:)

    0 讨论(0)
  • 2020-12-05 06:10

    Here's a script you can include on your page (or in a separate js file) that will add a class to the html element so that you can add safari-mac specific css to your css file.

    (function($){
        // console.log(navigator.userAgent);
        /* Adjustments for Safari on Mac */
        if (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Mac') != -1 && navigator.userAgent.indexOf('Chrome') == -1) {
            // console.log('Safari on Mac detected, applying class...');
            $('html').addClass('safari-mac'); // provide a class for the safari-mac specific css to filter with
        }
    })(jQuery);
    

    Example css fixes, which can be in page or in your external css file etc:

    /* Safari Mac specific alterations
     * (relies on class added by js browser detection above),
     * to take into account different font metrics */
    .safari-mac #page4 .section p.fussyDesigner { margin-right: -15px; }
    .safari-mac #page8 .section-footer { width: 694px; }
    

    Thanks to other answers for ideas, I've tried to wrap everything up into a complete solution in this answer.

    0 讨论(0)
  • 2020-12-05 06:12

    You probably need to run a regex query against the User-Agent and selectively load a CSS file just for Safari for Mac.

    Also: Are you sure that Safari for mac and safari for windows render the same page drastically different?

    0 讨论(0)
提交回复
热议问题