DEV Community


Posted on

The way better than 'string'.truncate(3) in Rails, If it's responsive website.

Usual Way

We could choose this way. It is fine if it's not for a responsive website.

<%= %>

Problem Situation

I have a responsive website. I should truncate the string at the client side but not the server-side.


There is a way to truncate string in CSS.

.truncate-title {
  width: 50%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  <ul class="navbar-nav mr-auto truncate-title">γ€€
    <li class="nav-item">
      <span class="navbar-text">
        <%= yield(:header_title) if content_for?(:header_title)%>


Alt Text

Discussion (2)

bizzibody profile image
Ian bradbury

Another option..... text-truncate is a standard Bootstrap class.

<span class="d-inline-block text-truncate" style="max-width: 150px;">
Praeterea iter est quasdam res quas ex communi.

n350071 profile image
n350071πŸ‡―πŸ‡΅ Author

I didn't know that. it's a very useful class! I remember it. Thank you for the comment :D