Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target Safari for Mac only?

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 have an idea how to solve it (reduce the margin on an element by a few pix), but i don't know how to target Safari only, preferably Safari mac only.
What's the best way to do this?

like image 208
Moak Avatar asked May 15 '10 02:05

Moak


People also ask

How do I make Safari My search engine on Mac?

On your Mac, choose Apple menu > System Settings, then click Desktop & Dock in the sidebar. (You may need to scroll down.) Click the pop-up menu next to “Default web browser” on the right, then choose Safari.

What is the best browser for Mac besides Safari?

Vivaldi and Brave are the best browsers for Mac. Recent updates to Safari make Apple's default web browser a viable option. Chrome leads the way for anyone looking for the most extensions when browsing.

How do I change Safari Preferences on Mac?

In the Safari app on your Mac, choose Safari > Settings, then click a settings pane: General: Change your homepage, and choose what to see when you open a window or tab, how long to keep your browsing history, which bookmarks to show in Favorites view, and choose where to save downloads and how long to keep them.

Is Safari just for Mac?

Safari works seamlessly and syncs your passwords, bookmarks, history, tabs, and more across Mac, iPad, iPhone, and Apple Watch.


2 Answers

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.

like image 143
Tim Abell Avatar answered Sep 30 '22 16:09

Tim Abell


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.

like image 34
Mantas Vidutis Avatar answered Sep 30 '22 18:09

Mantas Vidutis