Creating a strong line count in HTML with the added features of text wrapping and font size alteration

Many queries have been raised about line numbers, such as: Create line numbers on pre with CSS only

or this: Display line number in textarea

The issue I am facing is the need for line count for lines that are wrapped with multiple lines.

section {
background: #303030;
color: #f1f1f1;
padding: 10px 16px;
border-radius: 2px;
border-top: 4px solid #00aeef;
-moz-box-shadow: inset 0 0 10px #000;
box-shadow: inset 0 0 10px #000;
counter-reset: line;
}

section div {
  display: block;
line-height: 1.5rem;
 
}

section div:before {
  counter-increment: line;
  content: counter(line);
  display: inline-block;
  border-right: 1px solid #ddd;
  padding: 0 .5em;
  margin-right: .5em;
  color: #888

}
<section>
  <div>lorem ipsum.lorem ipsum.lorem ipsum.lorem ipsum...</div>
  <div>lorem ipsum. <span style="font-size: 54px;">big text</span></div>
  <div>lorem ipsum.</div>
</section>

Line #1 is wrapped on more than one physical line and the current counting does not consider it.

While searching for a solution, another approach I came across is to hard code a list of line numbers with the same height as the textarea. However, this method will not work when supporting multiple font sizes, resulting in unaligned line numbers.

For example (Credits to: Aakash Chakravarthy, https://jsfiddle.net/vaakash/5TF5h/):

textarea{
    background: url(http://i.imgur.com/2cOaJ.png);
background-attachment: local;
background-repeat: no-repeat;
padding-left: 35px;
padding-top: 10px;
    border-color:#ccc;
}
<textarea rows="10" cols="40"></textarea>

I generate my HTML using a different tool (etherpad) and need to add line numbers to it afterwards. Therefore, I do not know the length of the text or the size of the font within it.

My question is: How can I add line numbers to dynamic HTML while considering text-wrapping and font size?

Answer №1

Based on your response, it appears that the font-size of the textarea is causing the line numbers to be misaligned. To fix this issue, you can adjust the font-size to 14px, which should align the line numbers properly.

textarea{
background: url(http://i.imgur.com/2cOaJ.png);
background-attachment: local;
background-repeat: no-repeat;
padding-left: 35px;
padding-top: 10px;
border-color:#ccc;
font-size: 14px;//add the style
}
<textarea rows="10" cols="40"></textarea>

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

Align a Bootstrap button to the bottom of a flex-box column

After exploring numerous online discussions in search of a solution to my issue, I have come up short. Applying 'align-self: flex-end' to the .reveal-btn only moves the button to the right, whereas using 'margin-top: auto' has proven in ...

tips for obtaining necessary input fields

What is the best way to include mandatory input fields? appreciate it <input type="text" name="name"> ...

Identifying input value changes without needing to manually enter key values

As an example: <input class="table-input" type="number" name="qty" [(ngModel)]="cpy.qty" matInput min="1" max="{{cpy.qty}}" where cpy represents an object that is constantly changing. Currently, I need to execute a certain operati ...

ui-grid row size set to automatically adjust using rowHeight : 'auto'

Has anyone else experienced this strange behavior with ui-grid? When I set the rowHeight to auto, each cell in the same row ends up with different heights. One of the cells contains multiline data, which seems to be causing issues for ui-grid. I've ev ...

Take off the wrapping from the package

I need help with my code on how to remove the wrapper span tag without removing the text. <ul> <li> <a href="#"> </a> <ul> <li> <a href="#"> ...

Parsing nested json objects in an angular application

I'm trying to work with a complex nested JSON structure and I need to extract and display the data in HTML. The JSON data I have looks like this: { "Test Data": [ { "First Test": { "Design Name": "testname", "Output": "1" ...

Tips for eliminating the page URL when printing a page using window.print() in JavaScript or Angular 4

Can someone help me with a function that uses window.print() to print the current page, but I need to remove the page URL when printing? I'm looking to exclude the URL from being printed and here is the code snippet where I want to make this adjustme ...

I'm having trouble applying JavaScript to my Flask template, even though it is stored in the static directory. Any suggestions on how to fix this issue with the code provided?

I am currently working on a Flask project and attempting to create a hierarchical structure resembling a tree. However, I suspect that the JavaScript is not being correctly applied to my tree.html file, as the options cannot be expanded. The code provided ...

Is there a way for me to manipulate the RGB values of the canvas in such a manner that the Red and Green components of the gradient are determined by dividing the position of my mouse cursor

My homework assignment requires using RGB colors where the red value is set to 0, green is the x-coordinate divided by 2, and blue is the y-coordinate divided by 2. I've been trying to control the colors on a canvas using addColorStop functions, but I ...

What is the best way to eliminate YouTube recommended videos that appear at the conclusion of my video?

I am attempting to incorporate my own YouTube video onto my blog without having recommended videos display at the end. I have tried using ?rel=0 and &rel=0 but they do not seem to be working. <iframe class="embed-responsive-item" src="https://www.y ...

Creating an Angular 7 Template using HTML CSS and Javascript

I recently acquired a template that comes with HTML, CSS, and Javascript files for a static webpage. My goal is to integrate these existing files into my Angular project so I can link it with a Nodejs application to create a full-stack webpage with backend ...

Open $_POST in a new tab that has been edited for your convenience

<form method="post" target="_blank" action="battle.php" onsubmit="window.open('battle.php','','width=700,height=500,toolbar=0,menubar=0,location=0,status=0,scrollbars=0,resizable=0,left=30,top=0');" > < ...

Tips for creating dynamic animations in an opencart dropdown menu

Currently, I am in the process of enhancing my ecommerce website using OpenCart and I have been trying to incorporate a simple animation for the dropdown menu. However, despite applying the transition properties in the code snippet below, it seems that the ...

Adjust the size of multiple elements upon hovering over one of them

I encountered a puzzling issue, and here is a demonstration of my problem: https://jsfiddle.net/k1y8afst/ <div class="Sample"> <div class="Dummy"> <div class="Books"> <a st ...

"Choose between displaying the Bootstrap Column consistently in a vertical orientation or a horizontal orientation

I'm just diving into the world of HTML/CSS and currently experimenting with Bootstrap Studio for a project. My focus is on creating an 'About' section for a webpage, where I've structured four rows each containing two columns as follows ...

Enhance the connectivity between flex items in Bootstrap by incorporating joining lines

I'm currently developing a "tree" using bootstrap 5, a list of items that can be navigated down into. I'm implementing this using bootstrap 5 and raw HTML. Here is the HTML structure I have set up: https://i.sstatic.net/j84vp.png However, I wo ...

Unable to implement a design on a button during its click event

I successfully implemented a dynamic button in HTML5 and Javascript. The button has a click event assigned to it, so when clicked, its content and background color are supposed to change. However, while the content changes correctly, the background color d ...

What is the best way to eliminate empty space at the bottom of a page that is being caused by a button?

I have identified the reason for the blank space at the bottom of my page - it's due to a sticky "back to top" button. However, I am unsure how to resolve this issue. Here is the layout of the page: <nav> navbar </nav> <div> car ...

When deploying a website with the default Hugo theme on Netlify, I encountered a strange issue where the webpage appeared blank. Sur

After coming across a question similar to mine about creating a static page using blogdown with the same Hugo theme as the main site on Stack Overflow, I still find myself struggling to understand the solution provided. As an absolute beginner, I am curren ...

How to target child <div> elements within a parent <div> using jQuery

I am facing an issue with my parent <div> named #amwcontentwrapper. It contains a series of child divs with their own classes and ids. My goal is to utilize jQuery to select these child divs, and if they have the class .amwhidden, I want to leave th ...