-->

Wrap text from bottom to top

2019-01-25 04:29发布

问题:

Anybody know how I could wrap the text in reverse order, from bottom to top? I attached an example image.

P.S. refined question: Instead of breaking the line after it is full and having an incomplete line at the end... I need to brake somehow from bottom to top, so bottom lines are full and top line is incomplete.

回答1:

I would not recommend using exotic CSS attributes which aren't even in Chrome & Firefox yet. The best cross-browser solution is to handle this in Javascript when the document loads. Here's a sketch of how to do that:

$(function() {
    $(".title").each(function(i,title) {
        var width = 0;
        var originalHeight = $(title).height();
        var spacer = $('<div style="float:right;height:1px;"/>').prependTo(title);
        while (originalHeight == $(title).height()) {
            spacer.width( ++width );
        }
        spacer.width( --width );
    });
});

Working JSFiddle is here: http://jsfiddle.net/zephod/hfuu3m49/1/



回答2:

There is no general css solution for it. You must have to utilize help of any language. This is one of the solution using PHP:

<?php
$str= "This is what I want to achieve with your help";
$str = strrev($str);
$exp = str_split($str,18);
$str = implode(">rb<", $exp);
echo strrev($str);
?>


回答3:

Well, if that is depending on the text, then you can try something like a word replacer. For example

var words = "This is what I want to achieve";
var newWords.replace("what", "what <br />"); // note the line break
document.write(newWords);

Here is a fiddle for you: http://jsfiddle.net/afzaal_ahmad_zeeshan/Ume85/

Otherwise, I don't think you can break a line depending on number of characters in a line.



回答4:

Wrap and Nowrap will be rendered by the client-browser, so you can not force the browser to wrap from bottom to top. but you can do that with javascript or asp.



回答5:

This is not a formal solution for this problem. But see if this helps.

The HTML CODE

<div id="mydiv">
I can imagine the logic behind the code having to detect what is the last line, detect the div size, and the font size... then measure how many characters it can fit and finally go to the above line and insert the break where necessary. Some font families might make this harder, but trial and error should solve the issue once the basic code is set..
</div>

CSS:

#mydiv
{
    width:1000px;
    line-height:18px;
    font-size:20px;
    text-align:justify;
    word-break:break-all;
}

Here setting the div width around 50 times that of the font-size will give you the precise result. Other width values or font values might slightly disorient the last line, giving some blank space after the last character.(Could not solve that part, yet).

JQuery:

$(document).ready(function(){
        //GET the total height of the element
        var height = $('#mydiv').outerHeight();

        //Get the height of each line, which is set in CSS
        var lineheight = $('#mydiv').css('line-height');

        //Divide The total height by line height to get the no of lines.
        var globalHeight = parseInt(height)/parseInt(lineheight);
        var myContent = $('#mydiv').html();
        var quotient = 0;

        //As long as no of lines does not increase, keep looping.
        while(quotient<=globalHeight)
        {
            //Add tiny single blank space to the div's beginning
            $('#mydiv').html('&#8239;'+myContent);

            //Get the new height of line and height of div and get the new no of lines and loop again.
            height = $('#mydiv').outerHeight();
            lineheight = $('#mydiv').css('line-height');
            quotient = parseInt(height)/parseInt(lineheight);
            myContent = $('#mydiv').html();
        }
        //get the final div content after exiting the loop.
        var myString = $('#mydiv').html();

        //This is to remove the extra space, which will put the last chars to a new line.
        var newString = myString.substr(1); 
        $('#mydiv').html(newString);
    });


回答6:

If you already know where you want your breaks to take place just use simple HTML breaks to break your content and have it display the way you want.

<p>This is what<br/>
want to acheive with your help</p>

If you set the breaks manually (and you know where you want them to break) then create them yourself.

You could also try setting separate css width adjustments based on the dimensions of the screen you are seeing the breaking you are not liking and set an @media reference to make the div width smaller to break the text so it doesn't run unevenly across the top of certain size devices.



标签: css text wrap