Maintaining a consistent distance from the baseline while adjusting the font size dynamically in an HTML text element

Looking to lock a text element to the baseline while also resizing its font dynamically when the window is resized.

I created a demonstration on jsfiddle. However, the issue arises when the window size changes; the distance from the bottom of the window is not consistent.

<div class="foo">text</div>

<script>
  var onResizeFunction = function() {
    var newFontSize = Math.floor(0.2 * $(window).width());
    $(".foo").css("font-size", newFontSize);
  };

  onResizeFunction();

  $(window).resize(function() {
    onResizeFunction();
  });
</script>

In an attempt to resolve this issue, I added the following code:

.foo {
  position:fixed;
  bottom: 0;
}

The main problem seems to be the "hitbox" of the text element. Is there a clever css workaround or trick that anyone knows?

This functionality is within angular, so no advanced jQuery solutions are possible ;)

EDIT:

The concern is the consistency of the distance from the bottom of the text element to the bottom of the window.

Answer №1

The issue here lies in the relationship between your line-height and the font you have chosen to use. Even though you have set the bottom value to 0, the text does not align perfectly with the bottom of the window as expected.

This can be resolved by setting the line height to a unitless value. By doing this, the line height will adjust relative to the font size. While setting it to one might seem like the solution, it actually varies depending on the font and how much of its available height it utilizes.

With some experimentation, you should be able to find the optimal line height. It may not be completely precise, but close enough. I have made adjustments to your code in the fiddle below, and by using the ruler I included, you can see that it now aligns quite well:

.foo {
    position:fixed;
    font-family: arial;
    bottom: 10px;
    line-height: .7; /* this may vary based on the font */
}

https://jsfiddle.net/u6bd9qfp/2/

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

Error message "Undefined is not a function" occurred while using jQuery's .replace and scrollTop functions

I'm having issues with the scroll function in my code. It doesn't seem to be able to locate the ids in my HTML, even though I can't figure out why. I had a previous version that worked perfectly fine (unfortunately, I didn't save it D:) ...

PHP/HTML code being disrupted by the improper use of quotation marks

Trying to update some inherited code is proving tricky due to the use of quote marks. Here's the original snippet: <a href='<?php echo SITE_URL.'members-search.html';?>' class='btn btn-large<?php if($curPage == "F ...

The background image on a lengthy HTML table goes missing

Within a table cell, I have around 7500 short text lines. Interestingly, the background image of the table disappears around the 1800th line. I'm wondering if there is a specific limit on the length of the table that could be causing this? Even ...

Initiate an Ajax request from a hyperlink

Is it possible to trigger an Ajax request from a hyperlink? I am interested in updating the inner html of a div element upon clicking on a hyperlink through JavaScript. ...

MongoDB's findOne is returning null when it shouldn't

Why is my template returning null when it shouldn't be? Here is my findOne function: await this.findOne({name}, async (template) => { console.log(template); if (template) return cb(new Error('Template already exists')); I am ce ...

Stopping a JavaScript promise.all() based on a certain condition

Here's a snippet of code I'm working with: let promiseList = [] for (let i in data) { let promise = checkIfMember(data[i].tg_id, chatId).then(res => { if (res) { //if the user has a undefined username ...

What is preventing me from retrieving an ID value within an IF condition?

I'm trying to create a script that only allows devices with matching IP addresses, but I'm having trouble using an if statement. Whenever I include x.id inside the statement, it doesn't seem to work... any suggestions? <html> <sc ...

PHP search function malfunctioning

I'm feeling quite confused about the current situation. I am utilizing a form that requires a student code input of "lz." <form action="classgrades.php?id=<?php echo $courseid ?>" method="post"> <input type="text" name="studen ...

Dropdown menu with CSS arrow

I'm attempting to create something similar to this: Here's what I have so far: Html: <div class="panel-heading" data-toggle="collapse" href="#data2"> <div class="heading"> APPLICATION </div> <span clas ...

Turning off the ability to horizontally scroll in an iframe using touch controls

I am trying to disable horizontal scrolling in an iframe on my website, particularly when a user uses touch input and drags horizontally. The scroll bars can still be visible and draggable when touched directly. Unfortunately, I do not have control over th ...

"Revamp your site with Vue and API for dynamic background

I'm faced with an issue where I am attempting to modify the background of a joke fetched from an API, but for some reason, the background is not changing and I can't seem to identify why. The main problem lies in my inability to change the proper ...

There is no Api.js file in the Cordova iOS platform

I have been attempting to integrate the iOS platform into a new Cordova 7.0.1 project, but I continuously encounter the error mentioned in the title of this post. Despite following the steps outlined in this thread here, including removing and re-adding ...

The rules for prioritizing CSS selectors

Struggling with CSS selector rules and specificity? I've already checked this for specifics. Even tried using firebug and inspecting elements in Chrome to copy the CSS path, but still can't get it to work. This is my first time creating a website ...

Why are my Bootstrap nav-tabs not showing tab-content in MVC?

I'm dynamically creating tab navigation from controllers using a list. <div class=""row> <div class="col-xl-3"> <!-- Tabs nav --> <div class="nav flex-column nav-pills nav-pills-custom" id="v-p ...

How come my dimensions are not returning correctly when I use offset().top and scrollTop() inside a scrolling element?

Currently in the process of developing a web app at , I am looking to implement a feature that will fade elements in and out as they enter and leave the visible part of a scrolling parent element. Taking inspiration from myfunkyside's response on this ...

Displaying a set through an express server cookie for the client to see

New to using Express and encountering issues with sending cookies. Created a basic express app that is supposed to set a cookie in the browser. Here's the server setup: const express = require('express'); const cookieParser = require(' ...

Form input using the div element (when clicked)

I am currently working on a page that connects to a database with user information. For each user, I am creating a separate div element so that there are 6 divs total (each representing a different user). My goal is to have the ability for a user to click ...

Using AngularJS to implement a clickable functionality within a directive

Currently, I am in the process of implementing a drag-and-drop directive. When an element is dropped, I am adding a copy of that element to my div and appending the ng-click attribute to it in this manner: copy.append('<button class="close" ng-cli ...

Using the split and join methods in PHP for Javascript operations

Can you assist me with writing this code in PHP? I've attempted multiple times but I can't seem to get it right. Any help would be greatly appreciated! return s.split(/\r?\n/).join("<br>"); Thank you in advance! ...

How can I pass authentication details using jqGrid?

I am currently working with a service that has CORS support enabled. Typically, when making a server request, I create a request object using jQuery and include the withCredentials parameter set to true, which usually works well. However, I am facing an i ...