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

Trouble with Div Display

I have a section below my form that will show an alert message if there is an error. The issue I am facing is that if I use the inline-block property, everything within the section will be displayed in the alert color. I specifically want the background- ...

Utilizing HTML list elements in conjunction with a switch statement, and connecting the list directly to a database for

Hey there, I'm interested in learning how to access HTML elements and use them within switch statements. <div id="hori"> <ul> <li><a href="#">Aerospace</a></li> <li><a href="#">Automotive</a>< ...

The menu bar becomes distorted when the page is zoomed out

Hey everyone, I could really use some assistance with my menus. Whenever I zoom out of the page, they become distorted. Here is the link to my website: https://dl.dropbox.com/u/22813136/Finding%20Nemo%20Inc/FNemo_front.htm# I tested the link on Internet E ...

Unexpected outcomes arise when parsing headers from a file-based stream

Currently, I am in the process of creating a small parser to analyze some log files using node streams (more specifically io.js). I have been referring to the documentation for unshift to extract the header. While I can successfully divide the buffer and ...

Hovering over an option in Vue-Bootstrap b-select

I'm working with the Vue-Bootstrap framework and I need to update the CSS rule for the 'option' tag when it is being hovered over. Here is my code snippet: jsfiddle option:hover { background-color: red; } Can someone please explain why ...

Utilizing Next.js with formidable for efficient parsing of multipart/form-data

I've been working on developing a next.js application that is supposed to handle multipart/form-data and then process it to extract the name, address, and file data from an endpoint. Although I attempted to use Formidable library for parsing the form ...

"Implementing jQuery to dynamically set the href attribute for a link when

My website features a tabbed menu that displays content from various WordPress categories including Cars, Trucks, and Buses. The active tab is randomly selected each time the page is reloaded. However, there seems to be an issue with the "View More" button ...

Exploring the integration of Stripe Checkout with React and Express.js

Seeking assistance with integrating Stripe Checkout into my React application. I need to create a route in my nodejs/express server that will provide the session id required for setting up Stripe Checkout on the front end. The aim is to redirect users to s ...

In AngularJS, how can you filter the ng-repeat value by clicking on a checkbox field?

Hey there, I'm looking to filter the ng-repeat value when a checkbox is clicked. Check out my Plunker here. By checking the checkbox labeled Role block, it should only show elements with roles value of "block". Similarly, by checking the checkbo ...

Update your mappings for the city of Istanbul when utilizing both TypeScript and Babel

Currently, I am facing the challenge of generating code coverage for my TypeScript project using remap Istanbul. The issue arises due to the usage of async/await in my code, which TypeScript cannot transpile into ES5 directly. To circumvent this limitation ...

After being initialized, the added Vue.js DOM elements do not function together

I updated an HTML page with ajax contents and incorporated Vue.js for front-end events. Unfortunately, the dynamically added elements are not interacting with the Vue.js instance, even when I attempted to forceUpdate them. Any suggestions on how to resol ...

Issue with VueJS rendering data within a for loop

As a newcomer to VueJS, I appreciate your patience as I navigate through this. Let me provide as much detail as possible. I am currently working on a Vue app that needs to retrieve a response from a server, iterate through the data, and set a Vue data var ...

Querying MongoDB findAndModify: <<< locate and modify an object within an array in a document

Question : I am attempting to locate an object within a document's array and make updates to it. The syntax below provides me with the desired object, but I am unsure of how to update it. I attempted to use this query in findAndModify, but it seems ...

Increase the date by one day in the minimum date field using the date picker

I've been struggling with adding one day to the minDate in my code. Despite trying multiple solutions from Stackoverflow, none of them have worked successfully. Here is the code I currently have: $('#cal_mulamhn').datepicker({ minDate ...

Error: webpack-cli encountered an unrecognized argument along with a syntax error

Hello, I am currently delving into the realm of prestashop theme development. After setting up my local environment successfully, everything seems to be working fine. My next step is to create a new theme based on the classic default theme. Upon navigat ...

Having trouble with the jQuery toggle functionality not working as expected

I'm implementing a function where a div should increase in height and opacity when clicked, and then revert back to its original state when clicked again. I used the toggle function for this purpose, but the issue is that the button disappears when th ...

Dynamically load current components in Angular 2's final release

In my quest to dynamically load a component in the upcoming release version 2.0.0, I encountered some challenges. Previously, in RC5, I utilized the following code for loading: I created a directive responsible for loading the controls: import { Check ...

The Strong Password checker fails to identify the presence of a forward slash

Here is a regex I have for validating a strong password: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d!$\/%@]{6,20}$/ Criteria: Alphanumeric with special characters $/%@ At least 1 number At least 1 lowercase letter At least ...

Issue with PHP form submission not functioning within a table when utilizing jQuery

I've created a function that retrieves user data. function returnChild(){ global $pdo; $stmt = $pdo->prepare("SELECT * FROM children INNER JOIN districts ON children.ch_district = districts.dst_id ...

What causes the "Invalid hook call" error to occur when the useQuery function is invoked?

Encountering an issue while trying to use the useQuery() function from react-admin within a custom component. Despite the clear error message, I'm struggling to determine the right course of action. Visiting the provided website and following the inst ...