How to center-justify the last line of text in CSS?

后端 未结 10 1682
余生分开走
余生分开走 2020-12-02 08:23

How can I center-justify text?

Currently, justify does not center the last line.

10条回答
  •  抹茶落季
    2020-12-02 09:09

    You can also split the element into two via HTML + JS.

    HTML:

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

    JS:

    function justify() {
        // Query for elements search
        let arr = document.querySelectorAll('.justificator');
        for (let current of arr) {
            let oldHeight = current.offsetHeight;
            // Stores cut part
            let buffer = '';
    
            if (current.innerText.lastIndexOf(' ') >= 0) {
                while (current.offsetHeight == oldHeight) {
                    let lastIndex = current.innerText.lastIndexOf(' ');
                    buffer = current.innerText.substring(lastIndex) + buffer;
                    current.innerText = current.innerText.substring(0, lastIndex);
                }
                let sibling = current.cloneNode(true);
                sibling.innerText = buffer;
                sibling.classList.remove('justificator');
                // Center
                sibling.style['text-align'] = 'center';
    
    
                current.style['text-align'] = 'justify';
                // For devices that do support text-align-last
                current.style['text-align-last'] = 'justify';
                // Insert new element after current
                current.parentNode.insertBefore(sibling, current.nextSibling);
            }
        }
    }
    document.addEventListener("DOMContentLoaded", justify);
    

    Here is an example with div and p tags

    function justify() {
        // Query for elements search
        let arr = document.querySelectorAll('.justificator');
        for (let current of arr) {
            let oldHeight = current.offsetHeight;
            // Stores cut part
            let buffer = '';
    
            if (current.innerText.lastIndexOf(' ') >= 0) {
                while (current.offsetHeight == oldHeight) {
                    let lastIndex = current.innerText.lastIndexOf(' ');
                    buffer = current.innerText.substring(lastIndex) + buffer;
                    current.innerText = current.innerText.substring(0, lastIndex);
                }
                let sibling = current.cloneNode(true);
                sibling.innerText = buffer;
                sibling.classList.remove('justificator');
                // Center
                sibling.style['text-align'] = 'center';
                // For devices that do support text-align-last
                current.style['text-align-last'] = 'justify';
                current.style['text-align'] = 'justify';
                // Insert new element after current
                current.parentNode.insertBefore(sibling, current.nextSibling);
            }
        }
    }
    justify();
    p.justificator {
        margin-bottom: 0px;
    }
    p.justificator + p {
        margin-top: 0px;
    }
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    Some other text

    Disadvantage: doesn't work when page width changes dynamically.

提交回复
热议问题