mskuratowski
mskuratowski

Reputation: 4124

Bootstrap 4 - word wrapping in cards

How can I word wrap text insided cards.

Here's the problem: plunker link

Do you have any idea how to fix it?

Upvotes: 22

Views: 61198

Answers (4)

ABC
ABC

Reputation: 199

For future reference, this would work too:

.card-text {
  overflow-wrap: anywhere;
  word-wrap: break-word;
  word-break: normal;
  hyphens: auto;
}

See this discussion for additional info.

Upvotes: 0

Ben Collins
Ben Collins

Reputation: 484

Also consider this (for URLs etc); from the docs:

For longer content, you can add a .text-truncate class to truncate the text with an ellipsis. Requires display: inline-block or display: block.

https://getbootstrap.com/docs/4.1/utilities/text/

Upvotes: 0

andreas
andreas

Reputation: 16936

You need two rules:

  • max-width for your .card elements (because without defining a width CSS will not know where to break your long word) or overflow: hidden; to make the .card width no longer depend on the long word length
  • word-wrap: break-word; to tell the browser to break a word

.card {
    max-width: 100%;
}
.card-text {
    word-wrap: break-word;
}

.card {
  overflow: hidden;
}
.card-block {
  word-wrap: break-word;
}
<link data-require="[email protected]" data-semver="4.0.0-alpha.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />

  <div class="card-deck">
    <div class="card">
      <div class="card-block">
        <h4 class="card-title">Card title</h4>
        <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
        <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small>
        </p>
      </div>
    </div>
    <div class="card">
      <div class="card-block">
        <h4 class="card-title">Card title</h4>
        <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
        <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small>
        </p>
      </div>
    </div>
    <div class="card">
      <div class="card-block">
        <h4 class="card-title">Card title</h4>
        <p class="card-text">supportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingsupportingto additional content. This card has even longer content than the
          first to show that equal height action.</p>
        <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small>
        </p>
      </div>
    </div>
  </div>

Upvotes: 27

Marian07
Marian07

Reputation: 2580

Simple:

/* For screens above: 576px */
.card{
  overflow: hidden;
}

.card-text{
  word-wrap: break-word;
}

https://plnkr.co/edit/BEbJehY8hkWpDoTfFJXz?p=preview

Upvotes: 4

Related Questions