HTML-CSS : How to text-align starting from the last word?

标签 html css

我有相当具有挑战性的设计要完成。 有没有人遇到过这种情况?

这是我需要的设计,文本底部对齐,但如果行上没有足够的空间,文本会向上流动。

-------------------


this is 
a pretty long title
-------------------

这里不是我需要的,可以通过vertical-align: bottomposition: absolute; 轻松实现; bottom: 0,但文本流并不是我想要的。

-------------------


this is a pretty long
title
-------------------

请注意第二行的字数比第一行多。换行过程是如果文本对于一行来说太长,将第一个单词换行到上面一行

如有任何帮助,我们将不胜感激。

最佳答案

这是一个可怕的困惑。但它确实做你想做的。我不认为真的有任何好的方法来获得您正在寻找的行为。祝你好运!

window.onload = function () {
    var peaches = document.getElementsByClassName("peaches");
    for (var i = 0; i < peaches.length; i++) {
        addSpans(peaches[i]);
        var box = peaches[i].getBoundingClientRect();
        alignSpans(peaches[i].childNodes, box);
    }
}

function addSpans(element) {
    var text = element.innerHTML.split(" ");
    var result = "";
    for (var i = text.length - 1; i >= 0; i--) {
        result += "<span>" + text[i] + "&nbsp;</span>";
    }
    element.innerHTML = result;
}

function alignSpans(nodes, box) {
    var previousY = null, previousRight = null;

    for (var i = nodes.length - 1; i >= 0; i--) {
        var style = nodes[i].style;
        style.position = "relative";

        var rect = nodes[i].getBoundingClientRect();
        if (previousY != null && rect.y == previousY) {
            style.right = previousRight;
        } else {
            style.right = rect.x - box.x;
            previousY = rect.y;
            previousRight = style.right;
        }
    }
}
p.peaches {
    width: 125px;
    display: flex;
    flex-wrap: wrap-reverse;
    direction: rtl;
    justify-content: flex-end;
}
<body>
<p class="peaches">
this is a pretty long title
</p>
</body>

关于HTML-CSS : How to text-align starting from the last word?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47784880/

相关文章:

php - Echo php 代码不打印任何内容

html - 纯 CSS 中的响应式 responsive-square 位于任意大小的 div 元素的右上角 - 可能吗?

css - 可以删除语义 css 中的 .ui 类吗?

html - 如何让我的导航链接在页面中均匀分布并居中?

html - 我要逃脱! Angular $ sce不处理音频文件的链接(严格上下文转义)

javascript - 使用 JavaScript 进行日期验证

c# - HTML 选择 C#

javascript - 循环 td

css - 修改大型 CSS 代码/重新设计网站的最佳方式(约 850 行代码)

javascript - 将 gulp@3.9.1 中的 gulp watch 转换为 gulp@4