How to target Safari for Mac only?

后端 未结 10 1892
终归单人心
终归单人心 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题