Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
How to change font family only for one element - nextjs

My project has a global font. but for only one element I get font families randomly from a hosted url. example:

https://*****.com/file/fonts/Parnian.ttf

my page is a CSR.

now How I can use this font for my element?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can dynamically load fonts with Javascript.

const fontFace = new FontFace(newFontName, `url(${newFontUrl})`);
fontFace.load().then((loadedFace: FontFace) => {
  document.fonts.add(loadedFace);
  setFontFamily(newFontName);
};

Here is an example dynamically loading Google Fonts in NextJS.

enter image description here

import { useState } from 'react';
import type { NextPage } from 'next';
import * as font from 'random-google-font';

const Home: NextPage = () => {
  const [fontFamily, setFontFamily] = useState('cursive');

  const updateFont = async () => {
    const response = await font.get();
    if (response.length < 1) return;
    const newFontName = response[0].local[0].substring(
      1,
      response[0].local[0].length - 1
    );
    const newFontUrl = response[0].url.ttf;
    const fontFace = new FontFace(newFontName, `url(${newFontUrl})`);
    const loadedFace = await fontFace.load();
    document.fonts.add(loadedFace);
    setFontFamily(newFontName);
  };

  return (
    <div
      style={{
        width: '100%',
        height: '100%',
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'center',
        gap: '1em',
      }}
    >
      <span style={{ fontFamily }}>Hello S.M_Emamian!</span>
      <div style={{ cursor: 'pointer' }} onClick={updateFont}>
        🔁
      </div>
    </div>
  );
};

export default Home;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda