Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

CSS

Text truncation

1
2
3
white-space: nowrap !important;
overflow: hidden !important;
text-overflow: ellipsis !important;
https://snipt.net/embed/d5d264f2f5aca907e8bff61a5607c2fc/
https://snipt.net/raw/d5d264f2f5aca907e8bff61a5607c2fc/
d5d264f2f5aca907e8bff61a5607c2fc
css
CSS
3
2016-12-02T20:20:01
True
False
False
May 20, 2013 at 12:13 PM
/api/public/snipt/61786/
text-truncation
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nt">white-space</span><span class="o">:</span> <span class="nt">nowrap</span> <span class="o">!</span><span class="nt">important</span><span class="o">;</span> </span><span id="L-2"><a name="L-2"></a><span class="nt">overflow</span><span class="o">:</span> <span class="nt">hidden</span> <span class="o">!</span><span class="nt">important</span><span class="o">;</span> </span><span id="L-3"><a name="L-3"></a><span class="nt">text-overflow</span><span class="o">:</span> <span class="nt">ellipsis</span> <span class="o">!</span><span class="nt">important</span><span class="o">;</span> </span></pre></div> </td></tr></table>
css, text, truncate

JavaScript

Text Truncate

1
2
3
4
5
6
7
getTruncatedStr:function(){
          var _displayChars = 14;
          var _suffix = "...";
          var _str = this.getOverallScore();
          var _isTruncated = _str.length > _displayChars.length;
          return _isTruncated ? this.str.substring(0, _displayChars - _suffix.length) + _suffix: _str;
        },
https://snipt.net/embed/9ba9a779f9a2abcada65bc73c2b69d09/
https://snipt.net/raw/9ba9a779f9a2abcada65bc73c2b69d09/
9ba9a779f9a2abcada65bc73c2b69d09
js
JavaScript
7
2016-12-02T18:42:32
True
False
False
/api/public/snipt/37293/
text-truncate
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nx">getTruncatedStr</span><span class="o">:</span><span class="kd">function</span><span class="p">(){</span> </span><span id="L-2"><a name="L-2"></a> <span class="kd">var</span> <span class="nx">_displayChars</span> <span class="o">=</span> <span class="mi">14</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a> <span class="kd">var</span> <span class="nx">_suffix</span> <span class="o">=</span> <span class="s2">&quot;...&quot;</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="kd">var</span> <span class="nx">_str</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">getOverallScore</span><span class="p">();</span> </span><span id="L-5"><a name="L-5"></a> <span class="kd">var</span> <span class="nx">_isTruncated</span> <span class="o">=</span> <span class="nx">_str</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="nx">_displayChars</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">return</span> <span class="nx">_isTruncated</span> <span class="o">?</span> <span class="k">this</span><span class="p">.</span><span class="nx">str</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">_displayChars</span> <span class="o">-</span> <span class="nx">_suffix</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">+</span> <span class="nx">_suffix</span><span class="o">:</span> <span class="nx">_str</span><span class="p">;</span> </span><span id="L-7"><a name="L-7"></a> <span class="p">},</span> </span></pre></div> </td></tr></table>
text, truncate

CSS

truncated text with css

1
2
3
4
5
6
7
8
.link_truncated {
    text-overflow: ellipsis;
    display: inline-block;
    width: 275px; /* width of the container */
    white-space: nowrap;
    overflow: hidden;
    vertical-align: top;
}
https://snipt.net/embed/add2a47c1dba5912e5453db15d21ce2a/
https://snipt.net/raw/add2a47c1dba5912e5453db15d21ce2a/
add2a47c1dba5912e5453db15d21ce2a
css
CSS
8
2016-12-02T18:39:53
True
False
False
/api/public/snipt/34670/
truncated-text-with-css
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nc">.link_truncated</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="nb">text</span><span class="o">-</span><span class="nb">overflow</span><span class="o">:</span> <span class="n">ellipsis</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a> <span class="nb">display</span><span class="o">:</span> <span class="nb">inline</span><span class="o">-</span><span class="nb">block</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="nb">width</span><span class="o">:</span> <span class="m">275px</span><span class="p">;</span> <span class="c">/* width of the container */</span> </span><span id="L-5"><a name="L-5"></a> <span class="nb">white-space</span><span class="o">:</span> <span class="nb">nowrap</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a> <span class="nb">overflow</span><span class="o">:</span> <span class="nb">hidden</span><span class="p">;</span> </span><span id="L-7"><a name="L-7"></a> <span class="nb">vertical-align</span><span class="o">:</span> <span class="nb">top</span><span class="p">;</span> </span><span id="L-8"><a name="L-8"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
css, shorten, truncate

PHP

truncate text output of any variable so not to break a fixed layout

1
2
3
4
5
6
7
8
<?php
// Limit $title characters, shorten and include ... when necessary
  $limit = 22;
  if (strlen($title) > $limit)
    $title= substr($title, 0, strrpos(substr($title, 0, $limit), ' ')) . '...';
?>

<p><?php print $title; ?></a></p>
https://snipt.net/embed/463eedf87192c44909a1f25a982ec422/
https://snipt.net/raw/463eedf87192c44909a1f25a982ec422/
463eedf87192c44909a1f25a982ec422
php
PHP
8
2016-12-01T01:58:41
True
False
False
/api/public/snipt/5981/
truncate-text-output-of-any-variable-so-not-to-break-a-fixed-layout
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// Limit $title characters, shorten and include ... when necessary</span> </span><span id="L-3"><a name="L-3"></a> <span class="nv">$limit</span> <span class="o">=</span> <span class="mi">22</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">if</span> <span class="p">(</span><span class="nb">strlen</span><span class="p">(</span><span class="nv">$title</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nv">$limit</span><span class="p">)</span> </span><span id="L-5"><a name="L-5"></a> <span class="nv">$title</span><span class="o">=</span> <span class="nb">substr</span><span class="p">(</span><span class="nv">$title</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">strrpos</span><span class="p">(</span><span class="nb">substr</span><span class="p">(</span><span class="nv">$title</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nv">$limit</span><span class="p">),</span> <span class="s1">&#39; &#39;</span><span class="p">))</span> <span class="o">.</span> <span class="s1">&#39;...&#39;</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a><span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-7"><a name="L-7"></a> </span><span id="L-8"><a name="L-8"></a><span class="x">&lt;p&gt;</span><span class="cp">&lt;?php</span> <span class="k">print</span> <span class="nv">$title</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x">&lt;/a&gt;&lt;/p&gt;</span> </span></pre></div> </td></tr></table>
drupal, output, php, truncate
Copyrighted, illegal, or inappropriate content? Email [email protected].