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

194
Vistas
How do I make auto fontSize in Javascript?

I was trying to use the same function of fontSize() in mobile view to make it responsive but I don't know to do it. This is what I'm using in window view

document.getElementById('hakdog').onload = function () {func()};

function func() {
var str = document.getElementById("voter").innerHTML;
var a = str.length;

if(a >= 40) {
    voter.style.fontSize = "18px";
} else if(a >=30) {
    voter.style.fontSize = "22px";
} else {
    voter.style.fontSize = "27px";
}
}

Is there any same function for mobile view? I only know some function but I know its not responsive

$(document).ready(function(){
if($(window).width() < 480) { 
     $('h1').css('font-size','10px');
     $('h2').css('font-size','8px');
     $('h3').css('font-size','6px');
     $('h4').css('font-size','4px');
}
});

EDIT

I was trying to make it responsive using java just like on the first code I given, normal .css is not gonna work since its only going to give for h1, h2, h3, etc. I was pointing out that I wanted to make the responsive if the full name reached 40 letters then the fontSize would be 18px same with other functions given

EDIT 5:30pm MARCH 29

This is what I tried so far but didn't work as what I expected.

document.getElementById('hakdog').onload = function () {func()};
function func() {
$(document).ready(function() {
    function resizes() {
        if($(window).width() < 480) {
            var str = document.getElementById("voter").innerHTML;
            var a = str.length;

            if(a >= 40) {
                voter.style.fontSize = "9px";
            } else if(a >=30) {
                voter.style.fontSize = "10px";
            } else {
                voter.style.fontSize = "15px";
            }
        }
    }

    resizes();
    $(window).resize(resizes);
  
})
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Edit: Made changes to include the font size based on the length of the content.


This should work. You can check this out on the full page in the below code snippet.

$(document).ready(function() {
  function resizeMe() {
    if ($(window).width() < 480) {
      if ($("#name").text().length < 5) {
        $('h1').css('font-size', '46px');
      } else {
        $('h1').css('font-size', '10px');
      }
      $('h2').css('font-size', '8px');
      $('h3').css('font-size', '6px');
      $('h4').css('font-size', '4px');
    } else {
      $('h1').css('font-size', '20px');
      $('h2').css('font-size', '18px');
      $('h3').css('font-size', '16px');
      $('h4').css('font-size', '14px');

    }
  }

  resizeMe();
  $(window).resize(resizeMe);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 id="name">Helo</h1>
<h2>Yes</h2>
<h3>Nooooooooooooooooooooooooooo</h3>
<h4>okkkkkk</h4>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Do you try to change 'px' to 'rem', 'rem' resize in relation to the size of the screen.

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is what i meant for auto sizing for every device

var wid = $(window).width();

if(wid < 320) {
var str = document.getElementById("voter").innerHTML
var a = str.length;

if(a >= 40) {
    voter.style.fontSize = "9px";
} else if(a >= 25) {
    voter.style.fontSize = "11px";
} else {
    voter.style.fontSize = "15px";
}
}

Thanks for everyone commented out!

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