【问题标题】:Bootstrap 4 - word wrapping in cardsBootstrap 4 - 卡片中的自动换行
【发布时间】:2022-01-23 07:26:30
【问题描述】:

如何在卡片内自动换行。

问题来了:plunker link

你知道如何解决它吗?

【问题讨论】:

标签: html twitter-bootstrap css


【解决方案1】:

你需要两条规则:

  • max-width 用于您的 .card 元素(因为没有定义宽度 CSS 将不知道在哪里打破您的长字) overflow: hidden; 使 .card 宽度不再依赖于长字长
  • word-wrap: break-word; 告诉浏览器断字

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

.card {
  overflow: hidden;
}
.card-block {
  word-wrap: break-word;
}
<link data-require="bootstrap@4.0.0-alpha.6" 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>

【讨论】:

    【解决方案2】:

    简单:

    /* For screens above: 576px */
    .card{
      overflow: hidden;
    }
    
    .card-text{
      word-wrap: break-word;
    }
    

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

    【讨论】:

      【解决方案3】:

      还要考虑这一点(对于 URL 等);来自文档:

      对于较长的内容,您可以添加一个 .text-truncate 类来截断带有省略号的文本。需要 display: inline-block 或 display: block。

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

      【讨论】:

      • 但这会将“描述”文本呈现为单行。
      【解决方案4】:

      为了将来参考,这也可以:

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

      请参阅this discussion 了解更多信息。

      【讨论】:

        猜你喜欢
        • 2018-12-16
        • 2016-03-12
        • 2018-08-06
        • 2017-07-23
        • 1970-01-01
        • 2018-10-30
        • 1970-01-01
        • 2019-12-17
        • 2020-10-15
        相关资源
        最近更新 更多