Making the font as big as possible without overflowing each div

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite
3












I made a function that makes font sizes in <div>s as big as possible without overflowing. It takes a long time to run. How do I optimize this?



$(document).ready(() => 
$('.text-fit').each((index, element) =>
let currentElement = $(element);
let fontSize = 999;
currentElement.wrapInner("<div class='wrapper'></div>");
while (currentElement.find('.wrapper').innerHeight() > currentElement.innerHeight())
fontSize -= 1;
currentElement.css('font-size', fontSize + 'px');

);
);






share|improve this question

















  • 2




    Well... It executes a piece of JQuery around 900 times... I think that would do it. This is also not what JQuery is for.
    – FreezePhoenix
    Jul 30 at 23:22







  • 2




    Can you post an example of this in use?
    – FreezePhoenix
    Jul 30 at 23:27
















up vote
2
down vote

favorite
3












I made a function that makes font sizes in <div>s as big as possible without overflowing. It takes a long time to run. How do I optimize this?



$(document).ready(() => 
$('.text-fit').each((index, element) =>
let currentElement = $(element);
let fontSize = 999;
currentElement.wrapInner("<div class='wrapper'></div>");
while (currentElement.find('.wrapper').innerHeight() > currentElement.innerHeight())
fontSize -= 1;
currentElement.css('font-size', fontSize + 'px');

);
);






share|improve this question

















  • 2




    Well... It executes a piece of JQuery around 900 times... I think that would do it. This is also not what JQuery is for.
    – FreezePhoenix
    Jul 30 at 23:22







  • 2




    Can you post an example of this in use?
    – FreezePhoenix
    Jul 30 at 23:27












up vote
2
down vote

favorite
3









up vote
2
down vote

favorite
3






3





I made a function that makes font sizes in <div>s as big as possible without overflowing. It takes a long time to run. How do I optimize this?



$(document).ready(() => 
$('.text-fit').each((index, element) =>
let currentElement = $(element);
let fontSize = 999;
currentElement.wrapInner("<div class='wrapper'></div>");
while (currentElement.find('.wrapper').innerHeight() > currentElement.innerHeight())
fontSize -= 1;
currentElement.css('font-size', fontSize + 'px');

);
);






share|improve this question













I made a function that makes font sizes in <div>s as big as possible without overflowing. It takes a long time to run. How do I optimize this?



$(document).ready(() => 
$('.text-fit').each((index, element) =>
let currentElement = $(element);
let fontSize = 999;
currentElement.wrapInner("<div class='wrapper'></div>");
while (currentElement.find('.wrapper').innerHeight() > currentElement.innerHeight())
fontSize -= 1;
currentElement.css('font-size', fontSize + 'px');

);
);








share|improve this question












share|improve this question




share|improve this question








edited Jul 30 at 23:47









200_success

123k14143398




123k14143398









asked Jul 30 at 23:20









sag

1636




1636







  • 2




    Well... It executes a piece of JQuery around 900 times... I think that would do it. This is also not what JQuery is for.
    – FreezePhoenix
    Jul 30 at 23:22







  • 2




    Can you post an example of this in use?
    – FreezePhoenix
    Jul 30 at 23:27












  • 2




    Well... It executes a piece of JQuery around 900 times... I think that would do it. This is also not what JQuery is for.
    – FreezePhoenix
    Jul 30 at 23:22







  • 2




    Can you post an example of this in use?
    – FreezePhoenix
    Jul 30 at 23:27







2




2




Well... It executes a piece of JQuery around 900 times... I think that would do it. This is also not what JQuery is for.
– FreezePhoenix
Jul 30 at 23:22





Well... It executes a piece of JQuery around 900 times... I think that would do it. This is also not what JQuery is for.
– FreezePhoenix
Jul 30 at 23:22





2




2




Can you post an example of this in use?
– FreezePhoenix
Jul 30 at 23:27




Can you post an example of this in use?
– FreezePhoenix
Jul 30 at 23:27










2 Answers
2






active

oldest

votes

















up vote
1
down vote













No particular comment on the style, but FWIW, I had to implement the same thing recently — also in jQuery — and this is how I did it. Assume we have an outer <div> named time_display_div that we don't want to overflow, and nested immediately inside that we have a <span> named time_display_span that we're trying to make as big as possible.



function effective_fraction(span, div) 
var x = span.width() / div.width();
var y = span.height() / div.height();
return Math.max(x, y);


function update_display(msg)
// Change the text of the time display,
// and massage the font size until it fills the screen.
var div = $('#time_display_div');
var span = $('#time_display_span');
var font_size = parseInt(span.css('font-size'));
span.text(msg);
while (effective_fraction(span, div) < 0.90)
font_size += 10;
span.css('font-size', font_size + 'px');

while (effective_fraction(span, div) > 0.90)
font_size -= 2;
span.css('font-size', font_size + 'px');




Basically the only difference is that I'm skipping through the sizes at ten times the speed you are; and I'm using .height() instead of .innerHeight(), but I can confirm that at least in Safari it makes no difference which one I use.



So at least if anyone comes up with a better solution here, it'll be useful to more people than just yourself. :)






share|improve this answer




























    up vote
    1
    down vote













    How many .text-fit elements are there in the document? If there are many, then the first thing I'd do would remove adding the wrapper div dynamically, and instead put it into the HTML from the start.



    Then, you are unnecessarily looking up the wrapper element in each loop. Just save an reference to the element before the loop and use that.



    var w = currentElement.find('.wrapper');
    while (w.innerHeight() > currentElement.innerHeight())
    // ...



    Next, consider getting rid of jQuery. It's huge and slow and in most modern browsers it doesn't do much that you couldn't do directly on the DOM.



    Finally the biggest problem is starting with the font size of 999px. I tried starting with the more reasonable 99px and that sped up the process considerably. If you actually need the text to be that big, you may want to try with a different step size other than 1px. For example, if you'd use a binary search, it will be much faster.






    share|improve this answer





















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "196"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200623%2fmaking-the-font-as-big-as-possible-without-overflowing-each-div%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      No particular comment on the style, but FWIW, I had to implement the same thing recently — also in jQuery — and this is how I did it. Assume we have an outer <div> named time_display_div that we don't want to overflow, and nested immediately inside that we have a <span> named time_display_span that we're trying to make as big as possible.



      function effective_fraction(span, div) 
      var x = span.width() / div.width();
      var y = span.height() / div.height();
      return Math.max(x, y);


      function update_display(msg)
      // Change the text of the time display,
      // and massage the font size until it fills the screen.
      var div = $('#time_display_div');
      var span = $('#time_display_span');
      var font_size = parseInt(span.css('font-size'));
      span.text(msg);
      while (effective_fraction(span, div) < 0.90)
      font_size += 10;
      span.css('font-size', font_size + 'px');

      while (effective_fraction(span, div) > 0.90)
      font_size -= 2;
      span.css('font-size', font_size + 'px');




      Basically the only difference is that I'm skipping through the sizes at ten times the speed you are; and I'm using .height() instead of .innerHeight(), but I can confirm that at least in Safari it makes no difference which one I use.



      So at least if anyone comes up with a better solution here, it'll be useful to more people than just yourself. :)






      share|improve this answer

























        up vote
        1
        down vote













        No particular comment on the style, but FWIW, I had to implement the same thing recently — also in jQuery — and this is how I did it. Assume we have an outer <div> named time_display_div that we don't want to overflow, and nested immediately inside that we have a <span> named time_display_span that we're trying to make as big as possible.



        function effective_fraction(span, div) 
        var x = span.width() / div.width();
        var y = span.height() / div.height();
        return Math.max(x, y);


        function update_display(msg)
        // Change the text of the time display,
        // and massage the font size until it fills the screen.
        var div = $('#time_display_div');
        var span = $('#time_display_span');
        var font_size = parseInt(span.css('font-size'));
        span.text(msg);
        while (effective_fraction(span, div) < 0.90)
        font_size += 10;
        span.css('font-size', font_size + 'px');

        while (effective_fraction(span, div) > 0.90)
        font_size -= 2;
        span.css('font-size', font_size + 'px');




        Basically the only difference is that I'm skipping through the sizes at ten times the speed you are; and I'm using .height() instead of .innerHeight(), but I can confirm that at least in Safari it makes no difference which one I use.



        So at least if anyone comes up with a better solution here, it'll be useful to more people than just yourself. :)






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          No particular comment on the style, but FWIW, I had to implement the same thing recently — also in jQuery — and this is how I did it. Assume we have an outer <div> named time_display_div that we don't want to overflow, and nested immediately inside that we have a <span> named time_display_span that we're trying to make as big as possible.



          function effective_fraction(span, div) 
          var x = span.width() / div.width();
          var y = span.height() / div.height();
          return Math.max(x, y);


          function update_display(msg)
          // Change the text of the time display,
          // and massage the font size until it fills the screen.
          var div = $('#time_display_div');
          var span = $('#time_display_span');
          var font_size = parseInt(span.css('font-size'));
          span.text(msg);
          while (effective_fraction(span, div) < 0.90)
          font_size += 10;
          span.css('font-size', font_size + 'px');

          while (effective_fraction(span, div) > 0.90)
          font_size -= 2;
          span.css('font-size', font_size + 'px');




          Basically the only difference is that I'm skipping through the sizes at ten times the speed you are; and I'm using .height() instead of .innerHeight(), but I can confirm that at least in Safari it makes no difference which one I use.



          So at least if anyone comes up with a better solution here, it'll be useful to more people than just yourself. :)






          share|improve this answer













          No particular comment on the style, but FWIW, I had to implement the same thing recently — also in jQuery — and this is how I did it. Assume we have an outer <div> named time_display_div that we don't want to overflow, and nested immediately inside that we have a <span> named time_display_span that we're trying to make as big as possible.



          function effective_fraction(span, div) 
          var x = span.width() / div.width();
          var y = span.height() / div.height();
          return Math.max(x, y);


          function update_display(msg)
          // Change the text of the time display,
          // and massage the font size until it fills the screen.
          var div = $('#time_display_div');
          var span = $('#time_display_span');
          var font_size = parseInt(span.css('font-size'));
          span.text(msg);
          while (effective_fraction(span, div) < 0.90)
          font_size += 10;
          span.css('font-size', font_size + 'px');

          while (effective_fraction(span, div) > 0.90)
          font_size -= 2;
          span.css('font-size', font_size + 'px');




          Basically the only difference is that I'm skipping through the sizes at ten times the speed you are; and I'm using .height() instead of .innerHeight(), but I can confirm that at least in Safari it makes no difference which one I use.



          So at least if anyone comes up with a better solution here, it'll be useful to more people than just yourself. :)







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Aug 1 at 3:08









          Quuxplusone

          9,62011451




          9,62011451






















              up vote
              1
              down vote













              How many .text-fit elements are there in the document? If there are many, then the first thing I'd do would remove adding the wrapper div dynamically, and instead put it into the HTML from the start.



              Then, you are unnecessarily looking up the wrapper element in each loop. Just save an reference to the element before the loop and use that.



              var w = currentElement.find('.wrapper');
              while (w.innerHeight() > currentElement.innerHeight())
              // ...



              Next, consider getting rid of jQuery. It's huge and slow and in most modern browsers it doesn't do much that you couldn't do directly on the DOM.



              Finally the biggest problem is starting with the font size of 999px. I tried starting with the more reasonable 99px and that sped up the process considerably. If you actually need the text to be that big, you may want to try with a different step size other than 1px. For example, if you'd use a binary search, it will be much faster.






              share|improve this answer

























                up vote
                1
                down vote













                How many .text-fit elements are there in the document? If there are many, then the first thing I'd do would remove adding the wrapper div dynamically, and instead put it into the HTML from the start.



                Then, you are unnecessarily looking up the wrapper element in each loop. Just save an reference to the element before the loop and use that.



                var w = currentElement.find('.wrapper');
                while (w.innerHeight() > currentElement.innerHeight())
                // ...



                Next, consider getting rid of jQuery. It's huge and slow and in most modern browsers it doesn't do much that you couldn't do directly on the DOM.



                Finally the biggest problem is starting with the font size of 999px. I tried starting with the more reasonable 99px and that sped up the process considerably. If you actually need the text to be that big, you may want to try with a different step size other than 1px. For example, if you'd use a binary search, it will be much faster.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  How many .text-fit elements are there in the document? If there are many, then the first thing I'd do would remove adding the wrapper div dynamically, and instead put it into the HTML from the start.



                  Then, you are unnecessarily looking up the wrapper element in each loop. Just save an reference to the element before the loop and use that.



                  var w = currentElement.find('.wrapper');
                  while (w.innerHeight() > currentElement.innerHeight())
                  // ...



                  Next, consider getting rid of jQuery. It's huge and slow and in most modern browsers it doesn't do much that you couldn't do directly on the DOM.



                  Finally the biggest problem is starting with the font size of 999px. I tried starting with the more reasonable 99px and that sped up the process considerably. If you actually need the text to be that big, you may want to try with a different step size other than 1px. For example, if you'd use a binary search, it will be much faster.






                  share|improve this answer













                  How many .text-fit elements are there in the document? If there are many, then the first thing I'd do would remove adding the wrapper div dynamically, and instead put it into the HTML from the start.



                  Then, you are unnecessarily looking up the wrapper element in each loop. Just save an reference to the element before the loop and use that.



                  var w = currentElement.find('.wrapper');
                  while (w.innerHeight() > currentElement.innerHeight())
                  // ...



                  Next, consider getting rid of jQuery. It's huge and slow and in most modern browsers it doesn't do much that you couldn't do directly on the DOM.



                  Finally the biggest problem is starting with the font size of 999px. I tried starting with the more reasonable 99px and that sped up the process considerably. If you actually need the text to be that big, you may want to try with a different step size other than 1px. For example, if you'd use a binary search, it will be much faster.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Aug 1 at 7:58









                  RoToRa

                  6,0121236




                  6,0121236






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200623%2fmaking-the-font-as-big-as-possible-without-overflowing-each-div%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Greedy Best First Search implementation in Rust

                      Function to Return a JSON Like Objects Using VBA Collections and Arrays

                      C++11 CLH Lock Implementation