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

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

How can I center-justify text?

Currently, justify does not center the last line.

相关标签:
10条回答
  • 2020-12-02 09:00

    There doesn't appear to be a way. You can fake it by using justify and then wrapping the last line of text in a span and setting just that to text align center. It works ok for small blocks of text but is not a useful approach to large quantities of text or dynamic text.

    I suggest finding somebody at Adobe who's involved in their W3C work and nagging them to bring up right/left/center justification in their next meeting. If anyone's gonna be able to push for basic typography features in CSS it'd be Adobe.

    0 讨论(0)
  • 2020-12-02 09:07

    You can use the text-align-last property

    .center-justified {
        text-align: justify;
        text-align-last: center;
    }
    

    Here is a compatibility table : https://developer.mozilla.org/en-US/docs/Web/CSS/text-align-last#Browser_compatibility.

    Works in all browsers except for Safari (both Mac and iOS), including Internet Explorer.

    Also in Internet Explorer, only works with text-align: justify (no other values of text-align) and start and end are not supported.

    0 讨论(0)
  • 2020-12-02 09:07

    Calculate the length of your text line and create a block which is the same size as the line of text. Center the block. If you have two lines you will need two blocks if they are different lengths. You could use a span tag and a br tag if you don't want extra spaces from the blocks. You can also use the pre tag to format inside a block.

    And you can do this: style='text-align:center;'

    For vertical see: http://www.w3schools.com/cssref/pr_pos_vertical-align.asp

    Here is the best way for blocks and web page layouts, go here and learn flex the new standard which started in 2009. http://www.w3.org/TR/2014/WD-css-flexbox-1-20140325/#justify-content-property

    Also w3schools has lots of flex examples.

    0 讨论(0)
  • 2020-12-02 09:09
    <div class="centered">
    <p style="text-align: justify;">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Nullam id dolor id nibh ultricies vehicula ut id elit. Etiam porta sem malesuada magna mollis euismod. Nullam id dolor id nibh ultricies vehicula ut id elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna, vel scelerisque</p>nisl consectetur et.</div>
    

    I was able to achieve the result by wrapping the content in a div tag and applying the attribute text-align: center. Immediately after the div tag I wrapped the content in a paragraph tag and applied attribute, text-align: justify. To make the last line centered, I excluded it from the paragraph tag, which then falls back to attribute applied in the div tag. You just have to strategic about how many words you want on the last line. I've included a demo from fiddle. Hope this helps.

    Demo - Center Justify Paragraph Text

    0 讨论(0)
  • 2020-12-02 09:09

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

    HTML:

    <div class='justificator'>
    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.
    </div>
    

    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;
    }
    <div class='justificator'>
    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.
    </div>
    <p class='justificator'>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.
    </p><p>Some other text</p>
    Disadvantage: doesn't work when page width changes dynamically.

    0 讨论(0)
  • 2020-12-02 09:10

    Simple. Text-align: justify; (to get the elements aligned) Padding-left: ?px; (to center the elements)

    0 讨论(0)
提交回复
热议问题