Adjusting font size in dynamic containers relatively

Imagine we have a slider named .outer, which adjusts its height based on the width of the viewport. Inside this slider, there is an example slide called .inner that contains some text. However, when we shrink the window width, the content in slide .inner starts to overflow outside of the .outer container.

My goal was to dynamically adjust the font size of the text inside .inner whenever the window is resized and the height of .inner becomes larger than .outer. This adjustment would be done in intervals of 5%, gradually decreasing from the original size until the text fits correctly within the slider.

Check out this unworking example: https://codepen.io/rKaiser/pen/JOQQWY?editors=1111

$(window).on('load resize',function() {
  var inn = $('.inner');
  var inner = $('.inner').outerHeight();
  var outer = $('.outer').outerHeight();
  if (inner > outer) {
      var i;
      for (i = 100; i > 50;i--){
      inn.css('font-size', i+'%' )
      i--;
      }
 }
console.log('outer ' + outer);
console.log('inner ' + inner);
});

I attempted to use a for loop to decrease the font-size percentage on resize, but I'm uncertain if this approach will work as intended.

Answer №1

    $(window).on("load resize", function() {
      var inn = $(".inside");
      var inside = $(".inside").outerHeight();
      var outside = $(".outside").outerHeight();
      if (inside<outside) {
        //while (inside > outside) {
        var j;
        for (j = 90; j > 40; j--) {
          $(".inside").css("font-size",j+'%');
          j--;
        }
      }
      // }
      //inn.css('font-size', '60%');

      console.log("outside " + outside);
      console.log("inside " + inside);
    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Optimizing NodeJS for scheduling and sending bulk text messages based on MongoDB data

I am currently developing an application that relies on MongoDB database entries to send text messages through AWS. The information stored in the database includes the message content, phone number, and scheduled time for sending the message. As of now, ...

What is the reason the .clearfix class fails to properly clear a floated element?

Check out this post: Understanding clearfix The most helpful response indicates that the clearfix should be applied directly to the last floating element: To incorporate a clearfix, you simply need to include HTML code: <div class="clearfix"> ...

Utilizing ng-repeat $index for locating an element within an array

Within my $scope, there is a property called $scope.cars, which is an array of cars. Users have the ability to delete a car from this array. When calling the delete function deleteThis, I pass the $index parameter created by ng-repeat. However, in the Ja ...

How to Call a Nested Object in JavaScript Dynamically?

var myObj = { bar_foo : "test", bar : { foo : "hi there"; }, foo : { bar : { foo: "and here we go!" } } } How can we achieve the following: var arr = [["bar", "foo"], ...

What is the reason for seeing "11111111" at the end of the displayed data?

My PHP and Ajax setup successfully retrieves data, but for some reason, after the data is loaded, it is displaying "1111111" at the bottom. I have searched various websites for a solution but haven't been able to find one. Can someone please help me w ...

Comparison of the "Proposed Recommendation" versus the "Candidate Recommendation"

In a recent piece about HTML5, it was mentioned that the Proposed Recommendation date is set for 2022, while the Candidate Recommendation date is from 2012. I'm curious to understand the distinction between the "Proposed Recommendation" and the "Cand ...

Learn how to dynamically highlight a table row when any changes are made to it using jQuery

Is there a way to automatically highlight the current table row when any input text or image is changed within that specific row using jQuery? In the given code snippet below, with two rows provided, how can one of the rows be highlighted upon modifying ...

Resolved navigation bar problems

As a beginner in web development, I am working hard to launch my first website. Today, I have a question for the stack overflow community regarding a fixed navbar issue. The navbar I have created is functioning properly on Chrome, but when it comes to Fire ...

generate JSON array using jQuery

After fetching a JSON array from a REST API using AJAX, I encounter an issue: { "results": [ { "language_code": "es", }, { "language_code": "gl", }, { "language_code": "pt", } ] } Upon trying to display each langua ...

Code for object creation, inheritance, and initialization

In the code snippet below, a class is defined for managing input events such as mouse, touch, and pointer: // base.js export default () => { return { el: undefined, event: undefined, handler(ev) { console.log('default handler&a ...

"Choosing a div element using nth-child() method: a step-by-step guide

An HTML structure was provided which includes a section with an id of "main-content" and a class of "photo-grid". Inside this section are multiple div elements with a class of "col-1-4", each containing a link, paragraph, and image. <section id="main-c ...

Unexpected issue encountered while working with json, ajax, and jQuery

Here's the code snippet that I'm working with: $.ajax({type: 'get', mode: 'abort', dataType: 'json', url: 'http://localhost/1.php', data: {}, success: function(res){ alert(res); }, time ...

"Enhancing User Authentication with Firebase Email Verification in React Native

Does Firebase have a feature that allows me to verify if an email confirmation has already been sent to a user? I am able to check validation, but I need to determine whether the verification email has already been sent in order to decide if it needs to be ...

Choosing Elements from Twin Dropdown Lists in PHP

I am currently working on building a dynamic drop-down menu where the options in the second dropdown depend on the selection made in the first one. The initial dropdown consists of a list of tables, and based on which table is chosen, the columns of that s ...

Angular JS failing to display error messages

I'm experiencing difficulties displaying login validation errors with the following code. After clicking on the Login button, it redirects to the next page without showing error messages as expected. Any suggestions? index.html:- <!DOCTYPE ht ...

Having trouble getting SCSS to function properly? (Updated Code)

Transitioning from CSS to SCSS is my current project. I decided to make the switch because of SCSS's nesting capabilities, which I find more convenient for coding. However, I'm facing some challenges getting it to work properly. In my regular CSS ...

Caching HTML5 videos in Google Chrome

I am currently in the process of building a website and have successfully added an HTML5 video. However, I encountered an issue when attempting to change the video file in the background for users to upload a new one. Despite updating the video URL, Chro ...

Is there a way to verify the presence of a value and then isolate it without affecting the rest

When making an ajax call, the response is received in the following format: var response = { "wwc_tag_names": [{ "waterbreak_free": "waterbreak_free" }], "wwc_details": [{ "generatedid": "2", "timer_value": "5", ...

What specific data am I sending from my ajax request to the MVC controller?

How can I determine the appropriate parameter to use when passing a JavaScript object? Please see below: var all = []; //iterate through each instrument for (var i = 0; i < 3; i++) { var txt = getThis(i);//int var detail =getThat(i);//string ...

Input ENTER triggered JSON path loading

Upon clicking "enter", I am looking to display the description corresponding to the title. To achieve this, I have defined a variable to store the path of the description: var descri = json.query.results.channel.item.map(function (item) { return item. ...