Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing words directly under or above other words

Tags:

html

css

I wish to do the following within div tags:

enter image description here

The words will be coloured differently using spans.

I will be given some text in a text box and via JavaScript I will need to dynamically update to div to show something like the above.

What is the best way to do this?

Will it involve a monospaced font?

Will it involve writing "hidden" text?

I wish to do entire paragraphs in this manner.

This might seem weird but the research I'm doing requires me present certain words from a given text with multiple colours and I think this might be a nice way of conveying this information.

Updating the text in the text box will update the following variables, and in turn I will need to convert these two variables into something like the image above.

text = "I am under the text above me and there is lots more text to come./n I am even moving onto a new line since I have more text"

color_per_word_position =  {0:green, 1: red, 2: cyan, 4: yellow, 5: red, ...}
like image 789
Baz Avatar asked Aug 24 '13 22:08

Baz


People also ask

What is subscript and superscript in HTML?

Subscript text appears half a character below the normal line and is sometimes rendered in a smaller font. Subscript text can be used for chemical formulas, like H2O to be written as H2O. Superscript: The <sup> tag is used to add a superscript text to the HTML document. The <sup> tag defines the superscript text.

How do I align text to the bottom right?

The bottom-right text should be set to position: absolute; bottom: 0; right: 0 . You'll need to experiment with padding to stop the main contents of the box from running underneath the absolute positioned elements, as they exist outside the normal flow of the text contents. Save this answer. Show activity on this post.

How do I align text to the top right corner in CSS?

Aligning text in CSS can be achieved using the text-align property or the vertical-align property. The values are: left. right.

How do I put text at the bottom of the page in HTML?

Use the text-align property to align the inner content of the block element. Use the bottom and left properties. The bottom property specifies the bottom position of an element along with the position property. The left property specifies the left position of an element along with the position property.


1 Answers

You will have to use a monospaced font for this.*

I basically see two options: 1. use whitespace 2. margins.

Option 1

Your text will look like

I•am•under•the•text•above
••am•under•••••text•above

where denotes a space character. Pretty straight-forward in terms of CSS, since you don't have to worry about the spacing. The browser does it all for you. Example: http://jsfiddle.net/PYXdr/

*well, it may be possible with any font, using a lot of JS, but I guess it's not worth it.

Option 2

Since you probably don't want whitespace in between your spans, you may prefer this:

I•am•under•the•text•above
  am•under     text•above

Now, the spacing needs to be taken care of manually. Each span should get a margin-left that pushes it to the desired position. But before we can do that, we need to know the width of one character (using JS, since CSS does not provide that). Okay, pretty easy:

var el = document.createElement('pre');
el.style.display = 'inline-block';
el.innerHTML = ' ';
document.body.appendChild(el);
var width = parseFloat(getComputedStyle(el).width);
document.body.removeChild(el);

Now let's go ahead and move the spans:

span1.style.marginLeft = (2 * width) + 'px';
span2.style.marginLeft = (5 * width) + 'px';

Example: http://jsfiddle.net/JC3Sc/

Putting it all together

Now here's a basic example of how this might work:

var text = "I am under the text above me and there is lots more text to come.\nI am even moving onto a new line since I have more text"

var highlightBorders = [[2, 3, 4, 6], [6, 7]]; // YOUR TASK: implement the logic to display the following lines

var color_per_word_position =  {0:'lime', 1: 'red', 2: 'cyan', 3:'orange', 4: 'yellow', 5: 'red'}

/* generate CSS */
var style = document.createElement('style');
for (var i in color_per_word_position) {
    style.innerHTML += '.hl' + i + '{background:' + color_per_word_position[i] + '}';
}
document.head.appendChild(style);


/* generating the text */
text = text.split('\n');
var pre = document.createElement('pre');
text.forEach(function (line, i) {
    var div = document.createElement('div');
    var words = line.split(' ');
    var result = [];
    highlightBorders[i].forEach(function (len, j) {
        var span = document.createElement('span');
        span.innerHTML = words.splice(0, len).join(' ');
        span.className = 'hl' + j;
        if (j) {
            span.style.marginLeft = width + 'px' // YOUR TASK: implement the logic
        }
        div.appendChild(span);
    });
    pre.appendChild(div);
});

document.body.appendChild(pre);

This is not a complete solution, since a) I don't really see which parts exactly you want to highlight and b) I don't want to spoil all the fun. But you get the idea.

Example: http://jsfiddle.net/tNyqL/

like image 95
user123444555621 Avatar answered Oct 11 '22 10:10

user123444555621