Hey there, I'm looking to use different CSS fonts on Windows and Mac for the same page on a web application. Can someone please guide me on how to accomplish this?

In order to tailor the font based on the operating system, the following criteria should be followed:

For Windows: "Segoe UI" For Mac: "SF Pro"

Attempts have been made using the code provided below, but it seems to load before the DOM and lacks persistence.

      if (navigator.appVersion.indexOf("Win")!=-1) 
            {
                document.body.style.background = "green"; 
            }

Answer №1

To customize the font based on the operating system, you can use JavaScript to set the user agent value in the data attribute of the HTML tag and then apply specific fonts using CSS attribute selectors. Here is an example:

document.documentElement.setAttribute('data-useragent', navigator.userAgent);
html[data-useragent*='Mac OS X'] {
  font-family: 'SF Pro', sans-serif;
}

html[data-useragent*='Win'] {
  font-family: 'Segoe UI', sans-serif;
  font-weight: 800; 
}
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Placeat, corrupti! Dolore, repellat quo modi minus, aliquid mollitia amet suscipit ipsam quisquam esse, inventore quod tempore consequatur sint! Sapiente saepe quidem omnis, voluptatibus, cumque impedit maiores iste culpa asperiores accusamus assumenda temporibus minus adipisci porro odit ut reiciendis quia incidunt dolores!</p>

OS Specific Font - CSS Trics

Answer №2

implement it within a useEffect using an empty dependency array

useEffect(() => {
  // ... your unique code here
  if (navigator.appVersion.indexOf("Mac")!==-1) 
            {
                document.body.style.background = "blue"; 
            }
}, [])

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trim the edges of a photo and add the Kinetic filter

Currently, I am utilizing Kinetic for image processing purposes. The issue arises when I crop an image and then attempt to convert it to black and white by clicking a button. Unfortunately, the straightforward setFilter function does not seem to work in th ...

Google Authentication integration in React Native

What is the process for enabling logging in my application created with react-native init using a Google account? ...

Ways to customize the default CSS styles of angularJS for elements like search box and more

Looking for a way to customize the appearance of an AngularJs search box? <div ng-controller="PersonListCtrl"> <div class="bar">Search: <input ng-model="query"> </div> <ul cl ...

Navigating nested loops within multidimensional arrays

Currently, I am experimenting with nested loops to search through nested arrays in order to find a specific value called "codItem". Below is a test model for the array (as I do not have access to the original fetch request on weekends): let teste = [{ it ...

Can you assist me with setting a value in an ASP dropdownlist?

I have an asp dropdownlist and I need to set its value from the client side. Although I am able to retrieve the data from the client side, I am facing difficulty in setting it in my asp dropdownlist using JavaScript. HTML <div class="col-md-6 form-gro ...

Leverage environment variables in a React application that has been containerized with Docker

I am working towards the objective of creating a docker image (containing a react app) that utilizes environment variables from the host. Here is my planned workflow: Build the docker image locally Upload the docker image Run the command docker-compose u ...

My React project is unable to import the foundation SCSS file into the App.scss file

After installing zurb-foundation using npm, I attempted to import the .scss file into my App.scss file. Utilizing the "live sass server" for compiling the .scss code into .css code, a .css file was generated but did not display any code despite successfull ...

Trouble with the Rotate <text> feature in Internet Explorer 11 on Windows 10

I am using d3.js to draw SVG with the transform:rotate(180deg) property. It displays perfectly on Chrome, but doesn't work on IE11. I tried changing it to rotateY(180deg), and it works with div elements but not with text elements. Here is my screen an ...

Having trouble retrieving the content within an HTML tag using jQuery?

I'm facing some challenges when it comes to extracting the content of an html tag using the Chrome console. I've been trying different methods for about half an hour, but nothing seems to work. That's why I need some help :) The code I want ...

Acquire the content of an interactive website with Python without using the onclick event

I am looking to extract the content of a dynamically generated website after clicking on a specific link. The link is structured as follows: <a onclick="function(); return false" href="#">Link</a> This setup prevents me from directly accessin ...

Showing content in a single line causes it to drop down to the next

Check out my code snippet here: http://jsfiddle.net/spadez/aeR7y/23/ I'm curious about how I can make my header list align on the same line as the header, without dropping down. Here's the CSS code I have for the list: #header li { display: ...

Reset Form State Upon Submission in React

Is there a way to clear the previous state, whether it's a word or an error value, on each new submit? I attempted to reset the state by setting it to an empty string after submission but it didn't seem to work as expected. state = { word:&a ...

Increase the background image size for a <li> element

I am working with an unordered list and have set it up to display a background image on the li element when the cursor hovers over it: <div class="sidebar"> <ul> <li>test</li> <li>test</li> ...

Sending Python Object from Django View to Javascript Script in Template

Here is a basic code snippet created solely to illustrate a problem. While the task at hand can be achieved without JavaScript, I am exploring how to accomplish it using JavaScript for my actual code where it is necessary. In view.py Inside the play_game ...

CAUTION: Attempted to initialize angular multiple times...all because of jQuery...such a puzzling issue, isn

I am attempting to comprehend the situation at hand. The warning is clear, and I acknowledge that in my application, with the provided code and structure, the ng-view runs twice ('test' is logged twice in the console, indicating that angular is l ...

Caution: The value supplied to Material-ui Autocomplete is not valid

Currently, I am working on a project using React and material-ui. While working with the Autocomplete component in my form submission, I encountered a warning. Following the basic approach provided in the documentation, here's what I tried: let Form ...

Exploring additional parameters within express.js

I'm currently working on creating an email sender that includes all necessary components such as 'from', 'to', 'subject', 'textBody', etc. However, I am facing a challenge in setting optional parameters in expre ...

Receiving an error with React Proptypes when using the union type Breakpoint

Struggling to assign the correct proptype to the material-ui Breakpoint type. The breakpoint values are: export type Breakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl'; In my App.tsx file, I have the following ...

How can Components access variables declared in a custom Vue.js plugin?

I had developed a unique Vue js plugin to manage global variables as shown below: CustomConstants.js file: import Vue from 'vue' export default { install(Vue){ Vue.CustomConstants = { APP_VERSION: '2.1.0' ...

What security measures does Angular implement to safeguard against malicious user input?

While I was learning how to build a router in vanilla JS, the tutorial author mentioned that it's advisable to use frameworks like Angular or React for various reasons. The author pointed out that Angular, for example, sanitizes user input before inse ...