Home > Software design >  How do I make auto fontSize in Javascript?
How do I make auto fontSize in Javascript?

Time:03-29

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);
  
})
}

CodePudding user response:

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>

CodePudding user response:

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

  • Related