Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!