CSS text justify with letter spacing

后端 未结 9 1139
时光取名叫无心
时光取名叫无心 2020-12-01 12:29

Is there a way to automatically justify words using letter spacing, each in its row, to a defined width, using CSS?

For example, \"Something like this\" would look,

相关标签:
9条回答
  • 2020-12-01 12:31

    Again, I know this is REALLY old, but why not just put a space between each letter and then text-align:justify? Then each letter would be regarded as a 'word' and justified accordingly

    0 讨论(0)
  • 2020-12-01 12:31

    Found another way to achieve this with pure CSS, alas you need to spell out your words.

    In my situation, this was the only solution that worked (some letters had classes), plus it also produced the straightest right-alignment among answers here, without using hacks.

    .box {
      width: min-content;
      border: solid red;
    }
    
    .word {
      display: flex;
      justify-content: space-between;
    }
    <div class="box">
      <div class="word">
        <span>S</span>
        <span>o</span>
        <span>m</span>
        <span>e</span>
        <span>t</span>
        <span>h</span>
        <span>i</span>
        <span>n</span>
        <span>g</span>
        <span>&nbsp;</span>
        <span>w</span>
        <span>i</span>
        <span>c</span>
        <span>k</span>
        <span>e</span>
        <span>d</span>
      </div>
    
      <div class="word">
        <span>t</span>
        <span>h</span>
        <span>i</span>
        <span>s</span>
        <span>&nbsp;</span>
        <span>w</span>
        <span>a</span>
        <span>y</span>
      </div>
    
      <div class="word">
        <span>c</span>
        <span>o</span>
        <span>m</span>
        <span>e</span>
        <span>s</span>
      </div>
    </div>

    0 讨论(0)
  • 2020-12-01 12:36

    Needed this too, so I've bundled the suggested technique in a simple to use jquery-plugin you can find here: https://github.com/marc-portier/jquery-letterjustify#readme.

    It uses the same procedure at its core, and adds some options to tweak. Comments welcome.

    0 讨论(0)
  • 2020-12-01 12:40

    The css only solution is text-justify: distribute https://www.w3.org/TR/css-text-3/#text-justify but the support is still very poor.

    A small experiment using text-align-last: justify and adding spaces between letters.

    div{
    	display:inline-block;
    	text-align: justify;
    	text-align-last: justify;
    	letter-spacing: -0.1em;
    }
    <div>
    S o m e t h i n g<br>
    l i k e<br>
    t h i s
    </div>

    0 讨论(0)
  • 2020-12-01 12:42

    I just made a JQuery script from table's Tony B approach. Here is the JSFiddle https://jsfiddle.net/7tvuzkg3/7/

    This script creates a table with each char in a row. This works with full sentence. I'm not sure this is fully optimized.

    justifyLetterSpacing("sentence");
    
    function justifyLetterSpacing(divId) {
    
    	// target element
    	domWrapper = $('#' + divId).clone().html('');
    	// construct <td>
    	$('#' + divId).contents().each(function(index){
          // store div id to td child elements class
    	  var tdClass = divId ;
          // split text 
    	  $textArray = $(this).text().split('');
          // insert each letters in a <td>
    	  for (var i = 0; i < $textArray.length; i++) {
            // if it's a 'space', replace him by an 'html space'
            if( $textArray[i] == " " )  {
                $('<td>')
                    .addClass(tdClass)
                    .html("&nbsp;&nbsp;")
                    .appendTo(domWrapper);
            }// if it's a char, add it
            else{  
                $('<td>')
                    .addClass(tdClass)
                    .text($textArray[i])
                    .appendTo(domWrapper);
            }
    	  }
    	});
        // create table
        table = 
        $( "<table id='"+divId+"'/>" ).append( 
            $( "<tbody>" ).append( 
                $( "<tr>" ).append( 
                    ( domWrapper ).children('td') 
                ) 
            )
        );
        // replace original div
    	$('#' + divId).replaceWith( table );
    }	
    #sentence {
      width : 100%;
      background-color : #000;
      color : #fff;
      padding : 1rem;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="sentence">LOREM IPSUM DOLOR</div>

    0 讨论(0)
  • 2020-12-01 12:44

    I know this is an old topic, but I faced this the other night. And found a suitable solution using tables.

    Every letter shall be put into a <td> </td> I know it looks tedious, but if you wanna do this, it would be for a word or two, right? Or you always can use JS to fill it if is too much. However, this is only CSS and very versatile solution.

    Using letter-spacing the letters get distributed properly. You should play around with it, depending on the width of the table.

    #justify {
      width: 300px;
      letter-spacing: 0.5em;
    }
    
    <table id="justify">
      <tbody>
        <tr>
          <td>J</td>
          <td>U</td>
          <td>S</td>
          <td>T</td>
          <td>I</td>
          <td>F</td>
          <td>Y</td>
        </tr>
      </tbody>
    </table>
    

    See the example here

    Crossbrowser safe, virtually nothing shall differ. Is just CSS.

    I used it in My website which is in english and spanish. the subtitle under my name in spanish has an additional letter and it will step out the width. Using the tables explained above, it gets distributed to the same width automatically. Spacing it manually I'd had to define a whole condition for each language to go around that.

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