Looking to adjust line-height property for text with different line heights

I am struggling with adjusting the line-height of row-based text content in HTML enclosed in <p> tags. The challenge I'm facing is that the text could vary in length, resulting in either one or two lines. If it's a single line, it should contain up to 30 words. I believe this can be achieved using JavaScript or jQuery with an if statement, but being a beginner in these languages, I need guidance on writing the necessary code with complete syntax instead of just snippets.

I also need assistance in understanding where to place the syntax - whether inside the head section or at the bottom in the body - as the placement can impact the functionality.

Update:

I have included a link for reference - . Please let me know if more details are required. Thank you!

Update:

A working example is available at - http://jsfiddle.net/ravk/M4GSR/.

Based on the fiddle, my goal is to dynamically set the line-height based on whether the text spans one or two lines (not more than two). Additionally, I aim to vertically center the text within the base div.

Answer №1

A method to achieve this is by utilizing JAVASCRIPT

insert the following snippet:

   <body>
      <p id = 'p'> // your text </p>
    </body>
      <script type = 'text/javascript'>
         p = document.getElementsByClassName('txt-mid')
         for(i in p){   
          no_of_words = p[i].innerHTML.length
          alert(no_of_words)
           if(no_of_words <= 30){
             p[i].style.lineHeight = '100px'
           }
          else{
            p[i].style.lineHeight = '50px'
          }
            }
      </script>

check out this JSFiddle demonstration.

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

The dropdown menu is not able to retrieve information from the secondary database

I have been encountering multiple challenges while working on a web-based dynamic form that I am developing. My current major issue is with populating the second #bodytype dropdown based on the selection made in the first, #bodyman, dropdown. Subsequently ...

Encountering a problem when utilizing the each loop within an ajax request

While attempting to iterate through a each loop within an Ajax call, I encounter the following error: TypeError: invalid 'in' operand e Here is my Ajax call code snippet: $.ajax({ type: "POST", url: "/admin/counselormanagem ...

Tips for creating a responsive image gallery

After completing the development of my interactive Video Gallery using only HTML and CSS, I encountered an issue with responsiveness. Whenever I resize my browser, the images go out of the frame. I am looking for a solution to make the image gallery adjust ...

Contrasts in Bootstrap 5.3 color schemes between development and live environments

Currently in the process of building a website using Astro [^2.2.1], Bootstrap [5.3.0-alpha3], and Svelte [^3.58.0]. Most components are functioning correctly, except for some elements like buttons where the foreground and background colors seem off. I&apo ...

The JQuery datepicker fails to display the current date

I am experiencing an issue with the datepicker on my webpage. While it is working correctly, the default date being displayed is '01/01/2001' instead of '11/23/2012', as I intended. Here is the jquery code I am using: $(":inpu ...

Numerous toggles paired with various forms and links

I am currently facing an issue with toggling between forms on my website. Although the toggle functionality works fine, I believe I may have structured the steps in the wrong order. My ideal scenario is to display only the 'generating customer calcul ...

Simple guide to identifying when the content of a dropdown option changes using jQuery

I am seeking a solution for detecting changes in the options of a dropdown field that are updated dynamically. Is there a method to capture an event each time the set of options within the dropdown is modified? I am not looking to track individual option ...

What is the method to adjust line spacing of hyperlinks in JavaFX?

I am setting up a vbox and inserting hyperlinks into it. Hyperlink clickableString; clickableString = new Hyperlink(); clickableString.setStyle("-fx-text-fill: black;-fx-padding: 0; -fx-line-spacing: 0em;"); //setting the hyperlink color to black. clicka ...

Using jQuery for real time countdown and count up synchronization with server-side values

Script: Every day, I receive a json response with specific event timings: items: { fajr: '5:23 am', sharooq: '7:23 am', dhur: '1:34 pm', asr: '4:66 pm': magrib: '6:23 pm', isha: '8:01 pm'} Upon ...

Text that spans across multiple lines

I have a multi-line text that I need to adapt to a div. For example: https://i.stack.imgur.com/mtZmf.png Is there a way to adjust the text within the div? Can we prevent the blue spacing after the word "text" using CSS? If I have various texts of differ ...

Challenge with PHP mail function in sending emails

I run a website and have a "Contact" section where users can fill out a form to reach me. The form is a basic one with its action being a php page. Here is the php code: $to = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4 ...

Tips for accessing information from a FOR loop within DIV tags

Let's explore the following code snippet: Here is the HTML generated: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Website ...

The Ajax form is failing to send data to PHP

Thanks to Stackoverflow, I've been diving into website design. I recently put together some code for handling data sent from an HTML form using Ajax. The code does a good job of error checking, but it seems to be missing the step of actually sending t ...

Iterate through an Array of Objects and exhibit a single object property in HTML one at a time by utilizing Javascript

I am working with an array of objects that contain two properties: "quote" and "author". I have a container div where I want the quotes to be displayed one by one at an interval of every 2 seconds. Currently, it is showing one letter at a time instead of d ...

The Jquery instructions were not executed

Enjoy your day! I have recently downloaded the latest compressed version of jQuery from a lesson site here (Download the compressed, production jQuery 3.6.0) After that, in my HTML document, I included it like this: <head> <meta charset = &qu ...

Does setInterval run only once?

JSFiddle is here... http://jsfiddle.net/Vd8PJ/10/ Currently, I am attempting to create a JQuery carousel that can showcase images of varying widths. The goal is to achieve a seamless and infinite slide to the right on an unordered list containing these i ...

"Chrome's failure to fire the appCache event for HTML5 has left developers scratching

Currently, I am working on an offline website using HTML5. In my JavaScript file, I have written some code for event listeners; however, the events are not being fired. var appCache = window.applicationCache; if (appCache) { appCache.addEventListener ...

JavaScript function for automatic scrolling to the bottom of the page is not functioning as expected

I'm working on incorporating a terminal/console feature into my website. I came across the JavaScript functions for scrolling down a page, namely window.scrollTo(0,document.body.scrollHeight); and window.scrollTo(0,document.querySelector(".fakeSc ...

The functionality of Jquery Ajax is limited to a single use

I'm having an issue with a page that is supposed to reload the data within a div using jQuery, but it only updates once. Here's a snippet of my code: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0 ...

Connect two radio buttons across separate forms

I am encountering an issue with two radio buttons that I am trying to link together. The problem arises because they are located in two separate form elements, causing them not to function as intended. It is important for the forms to be utilized in Bootst ...