What are the recommended margins for various alphabets?

When displaying text, some alphabets take up more space than others. So how can I adjust the white space between elements '#re1' and '#re2' automatically in the scenario described below?

In this code snippet, what is the best way to adjust the margin between '#re1' and '#re2' dynamically? I want to avoid situations like the one shown in the images linked below,

https://i.stack.imgur.com/WzOPl.png

https://i.stack.imgur.com/igKQi.png

If I set the margin based on the widest alphabet 'm', then the gap between the elements becomes too large when typing narrower letters like 'a'.

function tx() {
  var x = document.getElementById("123").value;
  document.getElementsByClassName("re")[0].innerHTML = x;
  document.getElementsByClassName("re")[1].innerHTML = x;
}
#re1 {
  width: 200px;
  word-wrap: break-word;
  height: 20px;
}

#re2 {
  width: 150px;
  word-wrap: break-word;
  height: 20px;
  margin-top: 3px;
}
<!doctype html>
<html>

<head>

  <body>
    <textarea id="123" onKeyUp="tx();" onKeyPress="tx();" maxlength="23"></textarea><br>
    <div class="out">
      <div class="re" id="re1">1</div>
      <div class="re" id="re2">2</div>
    </div>
  </body>

</html>

Answer №1

Adjust the line-height property.

function tx() {
  var x = document.getElementById("123").value;
  document.getElementsByClassName("re")[0].innerHTML = x;
  document.getElementsByClassName("re")[1].innerHTML = x;
}
#re1 {
  width: 200px;
  word-wrap: break-word;
  line-height: 110%;
}

#re2 {
  width: 150px;
  word-wrap: break-word;
  line-height: 110%;
}
<!doctype html>
<html>

<head>

</head>

<body>
  <textarea id="123" onKeyUp="tx();" onKeyPress="tx();" maxlength="23"></textarea><br>
  <div class="out">
    <div class="re" id="re1">1</div>
    <div class="re" id="re2">2</div>
  </div>
</body>

</html>

Answer №2

give this code a go:

#section1 {
position:relative;      //include
width: 300px;           //exclude
word-wrap: break-word;
height: 30px;
}

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 functionality of the jQuery click event is not functioning properly

I encountered a strange issue where the code below works perfectly fine when directly pasted into the browser (chrome console). However, it does not work when executed from my source file. <script type="text/javascript" > $(".test").click( ...

Changing the properties of the admin bar when it is hovered over in

After successfully styling a button on the admin bar, I noticed a few imperfections in the implementation. The button in question is situated on the admin bar and is labeled "maintenance". Upon clicking the button, jQuery triggers the addition of the clas ...

New to Angular: Getting Started with NgModel Binding

Novice query: I am facing an issue with a simple input type=text element linked to ng-model="xyz.zyx", where xyz refers to an object. In my controller, I initialize this object and set the value for the property zyx as shown below: xyz { zyx: $scope.zz ...

Various tasks to be executed on a shared element

Struggling with implementing actions on a grid component without using a router in Next.js. Here is the current code snippet: const handleActions = (btnPress, row) => { if (btnPress === "Add") {/* implementation */} else if (btnPr ...

Close button for body

I have created a form that floats in the center of the screen On this form, I have included a button designed to close it However, I would like for the form to be closed anywhere on the screen when clicked with the mouse Here is my code: $(".offer-clo ...

Tips for keeping the footer consistently at the bottom of the page without using a fixed position (to avoid overlapping with the main content)

I've been struggling to keep the footer at the bottom of my webpage. I came across a solution online suggesting to use fixed CSS positioning. However, when I applied this fix, the footer ended up overlapping with the actual content on the page. Is the ...

Custom properties of an object are being erased when converting to JSON format within the canvas

I am working on a canvas project that involves multiple image objects, each with custom attributes. My goal is to save this canvas as a json object in a database, but the conversion process seems to strip away the custom attributes. Currently, I am using t ...

Personalizing the pop-up window using window.open

When clicking a hyperlink on a page, I need to open multiple pop-up windows. To achieve this, I must use the Window.open function instead of showModalDialog. However, I have noticed that the appearance is not satisfactory when using Window.open. (Essentia ...

How to extract the HTML content from specific text nodes using Javascript

I have a piece of HTML that looks like this: <div id="editable_phrase"> <span data-id="42">My</span> <span data-id="43">very</span> <span data-id="1">first</span> <span data-id="21">phrase< ...

Consistently encountering the error message "(0 , _reactDom.h) is not a function" or "h is not defined"

I'm currently in the process of developing an app that makes use of electron, react, redux, and several other technologies. At the moment, I have included electron, react, electron-compile, and babel in the project. Redux is installed but has not been ...

Discover the method to determine the total count of days in a given week number

I am developing a gantt chart feature that allows users to select a start date and an end date. The gantt chart should display the week numbers in accordance with the ISO standard. However, I have encountered two situations where either the start week numb ...

How to handle multiple radio inputs and determine the checked value in Vue?

Currently, I am in the process of learning Vue.js and developing a basic poll generator. However, I have encountered an issue with radio inputs. In this application, each question can be of two types - 'select' or 'range.' 'Select ...

Hover effect exclusively for specific child elements

Applying a hover effect to all divs inside a well can be done using the following code: .well:hover div { background-color:green } However, if you only want to apply the hover effect to specific divs inside the well, you can specify them in this way: ...

Nodemailer is functioning properly in a local environment, however, it is encountering issues when

I am facing an issue with Nodemailer where it is sending emails successfully on my local machine but not on Lambda. I have tried various solutions from Stack Overflow, but none of them seem to be working for me. One suggestion was to make the sendEmail fun ...

Retrieving form data in Servlet

Just began working with servlets and encountered an issue. I am trying to upload a file to my server using a servlet, while also sending a text value (the file name) to be changed on the server side. The problem arises when I submit the form data to the se ...

Having trouble accessing the value of an object within a nested array object

Looking for a way to extract the object value from a nested array object using JavaScript? If the sourcecountry matches the country in the object, it should return the corresponding payment service. Here is what I have attempted: function getValue(source ...

Is the IE7 Modal Dialog Misaligned?

Update After some investigation, I discovered the root cause of the problem. In my code, I was referencing the height of the overlay, which resulted in different values in IE7 compared to other browsers. To resolve this issue, I adjusted the code to refe ...

Is it necessary to have the script tag as the first tag in the body of the HTML?

I have a script file that needs to be included by third party implementors on their web pages. It is crucial for this script to be the first tag within the body element, like so: <body> <script type="text/javascript" src="/my_script.js"></ ...

"Silent Passageway: Connecting Node JS to ASW RDS MySQL Through a Secret

I am currently collaborating on a project with a partner. The backbone of our project is node js, and we are using an AWS RDS MySQL DB for our database. Our main challenge lies in establishing effective communication with the DB through SSH within NodeJS. ...

Tips for placing the brand logo on the left edge of the Bootstrap 4 navbar

I am facing an issue with the placement of my logo. The code snippet below shows how it currently looks: <nav class="navbar navbar-fixed-top navbar-light bg-primary"> <div class="container-fluid"> <img class="navbar-brand" src="logo.p ...