Adjust the size of fonts for elements that are added dynamically

My goal is to dynamically add elements with decreasing font sizes to a fixed-width/height container until they no longer fit. I've managed to scale the font based on the browser window size, but that's not the intended solution.

Is it possible to achieve this effect via CSS, JavaScript, or jQuery?

Take a look at this JSFiddle example: https://jsfiddle.net/qzt0wkyd/

Sample Code:

.bottom {
  background-color: #fff;
  text-align: center;
  margin: 0px auto;
  padding: 10px 5px 5px 5px;
  margin-top: 5px;
  width: 420px;
  height: 150px;
  border: solid #ff9400;
  border-radius: 10px;
  display: inline-block;
  overflow: hidden;
  box-shadow: inset 0 0 10px #000000;
}

.aside3 {
  color: #FFB894;
  background-color: #666;
  font-size: 1.5em;
  margin-top: -11px;
  margin-left: -8px;
  width: 103%;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  text-shadow: 1px 1px 1px #111, -1px -1px 1px #999, 1px -1px 1px #111, -1px 1px 1px #999;
}

.linksites {
  display: inline-flex;
  float: center;
  text-align: justify;
  font-weight: bold;
  color: orange;
  letter-spacing: .15em;
  font-size: 30px;
  text-decoration: none;
  text-rendering: optimizeLegibility;
  transition: 2s ease;
  -webkit-transition: 2s ease;
  -moz-transition: 2s ease;
  text-shadow: 1px -1px 0 #767676, -1px 2px 1px #737272, -2px 4px 1px #767474
}

.linksites:hover {
  text-decoration: none;
  font-size: 2.5vwx;
  -webkit-transform: scale(1.1, 1.1);
  -moz-transform: scale(1.1, 1.1);
  -ms-transform: scale(1.1, 1.1);
}
<div class="bottom">
  <p class="aside3">CONTACTS</p>
  <div class="bottom-inner">


    <!--ALL DYNAMICALLY INSERTED <a> TAGS COULD BE MORE OR LESS OR ADDED AND REMOVED-->
   .... (additional code not shown for brevity)

  </div>
</div>

In the provided example, I have multiple links that exceed the parent container's size. Is there a way to automatically scale the font size down as more links are added and scale it up when links are removed to fit the container? (I initially tried using em and rem units before reverting to fixed pixel sizes)

Thank you in advance

Answer №1

With the power of jQuery, you have the ability to dynamically adjust CSS values based on the number of elements present. This can be achieved by implementing code similar to the following:

if ($('a.linksites').length>9 && $('a.linksites').length<13) {
    $('.linksites').css('font-size','20px')
}

By utilizing multiple if statements like the one shown above, you can assign different font sizes to different quantities of linksites.

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

Utilizing JSON to enhance Jquery Autocomplete with custom parameters

My goal is to create an autocomplete input field where, as a user types a letter, the value of the field is sent to the server and words that match are returned. var acOptions = { source:function (request, response) { $.ajax( ...

Utilizing jQuery Template to retrieve a specific field from a JSON file

$.getJSON('dat.js', function(i,data) { $( "#lessons" ).tmpl( data[1].title ).appendTo( "#result" ); }); This is my JSON file { "posts": [ { "title": "Learning about A ...

Utilizing jQuery for JSON parsing

Within my JavaScript code, I am working with the following array: var versions = [{"id":"454","name":"jack"}, {"id":"4","name":"rose"} {"id":"6","name":"ikma"} {"id":"5","name":"naki"} {"id":"667","name":"dasi"} ] I need to extract the name from this ar ...

Sorting through arrays in JavaScript/React

My search functionality works well, but I'm facing an issue when the item is not found in the array. I attempted using objects.Keys to handle this situation, but it's displaying on render instead of when the book is not found as expected. Should ...

Incorrect positioning of dropdown menu when using Bootstrap 4 and translate3d() function

Having multiple bootstrap 4.4.1 dropdown menus, each within a column which is functioning correctly except for the position calculation. Example of HTML col: <div class="container"> <div class="row"> <div class="col"> <div ...

What is the reason for function expressions being hoisted when called within Express JS middleware?

It's common knowledge that function declarations are hoisted, allowing them to be called from anywhere in your script. However, this is not the case for function expressions. For instance: test(); const test = () => { console.log(1+3); } ...

The kendo-grid-messages are utilized across all columns

I encountered an issue while working with the following code: <kendo-grid-column field="isActive" [title]="l('Status')" filter="boolean"> <kendo-grid-messages filterIsTrue="Yes" filterIsFalse=&qu ...

Maintain the border styling of a radio button when it is in the selected state

Having an issue with the radio type options - when selected, a blue border appears but disappears if clicking elsewhere. Need the blue border effect to stay even when clicked outside, unless switching to the other option. Looking to align the price in th ...

Updating specific data in MongoDB arrays: A step-by-step guide

{ "_id":{"$oid":"5f5287db8c4dbe22383eca58"}, "__v":0, "createdAt":{"$date":"2020-09-12T11:35:45.965Z"}, "data":["Buy RAM","Money buys freedom"], & ...

typescript: best practices for typing key and value parameters in the forEach loop of Object.entries()

I have a specific object with key/value pairs that I need to iterate over using the entries() method of Object followed by a forEach() method of Array. However, I'm struggling to understand how to avoid a typescript error in this situation: type objTy ...

Using Angular JS, send out a notification and pause execution until it is finished

I recently encountered an interesting situation involving an Angular event: $rootScope.$broadcast("postData"); doSomething(); However, I realized that doSomething() needs to wait for the completion of postData before executing. This led me to contemplate ...

My goal is to create collapsible and expandable rows within this table

Discover the code snippet here, without pasting the entire content... As an illustration, let's utilize this example: <head> </head> <body> <table> <tr> <th></th> < ...

Using AngularJS Typeahead with restrictions on $http requests

I have been attempting to restrict the number of results displayed by Angular Bootstrap Typeahead during Async calls, but unfortunately, it does not seem to be functioning as expected. <input type="text" ng-model="asyncSelected" placeholder="Locations ...

Is there a way for me to delay sending an immediate response until the asynchronous loop has completed?

Currently trying to implement something similar to the code snippet below. I suspect that the issue lies within the asynchronous call as it seems like the response I am getting is always an empty array, despite the API returning data. As a beginner in th ...

How can AngularJS apps handle authentication?

Seeking input on user authentication with AngularJS and Zend... I currently have Angular on the client side and Zend on the server side handling authentication successfully. However, I'm looking for best practices and code examples for enhancing the ...

Change the JavaFX ToggleButton's color and revert back to its original color

Can you tell me what the default background and border color is for a toggle button? Here’s an example of code that changes the background color of a toggle button: i.toggle11.setOnAction(e->{ if(i.toggle11.isSelected()){ i.toggle ...

Gradually returning to the top with Vuetify's smooth scroll feature

Looking for a way to make the scrolling back to the top of the page smoother on my website. The button I currently have in place does the job, but it's not as smooth as I would like. Any suggestions on how to improve this? I've checked similar q ...

Adjusting Font Size of Menu BarORModifying

I'm currently using WordPress to build my website. If you'd like to take a look, here is the link: I'm trying to make the font size smaller to accommodate more menu bars on the site. Any suggestions on how I can achieve this would be great ...

The Facebook JavaScript API function FB.api() is limited to posting messages and does not support media attachments

I am attempting to share a message with an mp3 attachment on the active user's wall. It works perfectly fine within Facebook's Test Console, but when I try it from my mobile app, only the message gets posted. Can anyone help me figure out what I ...

Incorrect comparison of floats within arrays resulted in inaccurate results

I am currently working on a project that involves comparing values in an Array which are dynamically fetched from a website, and I'm using Selenium-IDE to assist with this comparison. However, I've noticed that the values are being compared as s ...