Compact/Loose text

Jquery/CSS snippet to maintain text content in one line and to expand on click

by David Gonzalez

HTML

<div class="compact-overflow"> 
  <div class="compact-overflow">
    For more information, please visit:           http://csstricks.com/thisisanonexistentandreallylongurltoaddtoanytextinfactwhyareyoustillreadingit.
  </div>
  <!-- does not work on buttons-->
  <button>
  <div  class="compact-overflow">
    For more information, please visit: http://csstricks.com/thisisanonexistentandreallylongurltoaddtoanytextinfactwhyareyoustillreadingit.</div>
  </button>
</div>

CSS

.compact-overflow {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.loose-overflow {  
  text-overflow: unset;  
  white-space: normal;
  word-wrap: break-word;
  -ms-hyphens: auto;
  -moz-hyphens: auto;
  -webkit-hyphens: auto;
  hyphens: auto;
}

JavaScript

$(".compact-overflow").click( function(){
	$(this).toggleClass("loose-overflow");
});