html - 文本溢出按钮而不是移动到下一行

标签 html css twitter-bootstrap-4

我正在尝试创建一个打开链接的 HTML 按钮。它看起来像一个小标题和一些简短的描述。但是每当描述太大时,它就会从按钮中溢出。

我正在使用 bootstrap 和 jquery。

代码:

body {
  background-color: #c0c0c0;
}
<link rel="stylesheet prefetch" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css">
<div class="container-fluid" style="text-align : center">
  <div id="final">
    <div>
      <a href="#" target="_blank" style="text-decoration: none;">
        <button class='btn btn-info btn-block'>
          <b>
            <p>
              Heading which is okay
            </p>
          </b>
          <p>
            And this is some really really very long text which is not fitting inside my button tag and is overflowing out of the button. But this text is meant to be a small description of the heading. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor iaculis enim eu ultrices. Pellentesque nec hendrerit mauris.
          </p>
        </button>
      </a>
    </div>
    <div id="search-result"></div>
  </div>
</div>


我已经尝试过的

p { // Truncating instead of moving to the next line
  overflow: hidden;
  text-overflow: ellipsis;
}

或者

btn {
  // This is not working
  display: inline-block;
}

或者

p {
  // Also not working
  display: inline-block;
}

我尝试搜索 Stack Overflow 并找到了两个建议:

  • 应用 overflow: hidden; 但它截断了文本而不是继续到下一行。
  • 正在应用 display: inline-block; 但我无法让它正常工作。

最佳答案

好吧, Bootstrap 适用 white-space: nowrap所有 .btn将按钮文本包装在一行中的类...

因此您需要将其更改为 white-space: normal通过使用您的父选择器,这样它就不会影响所有其他按钮。

此外,元素 p 在此上下文中不允许作为元素按钮的子元素..所以最好使用 <br>而不是 <p>如果你想要下一行的文字

此外,元素按钮不得作为 a 元素的后代出现...所以使用 btn btn-info btn-block类(class) <a>并删除 button

#final .btn{
  white-space: normal;
}

body {
  background-color: #c0c0c0;
}

#final .btn {
  white-space: normal;
}
<link rel="stylesheet prefetch" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css">
<div class="container-fluid" style="text-align : center">
  <div id="final">
    <div>
      <a href="#" target="_blank" style="text-decoration: none;" class="btn btn-info btn-block">
        <b>Heading which is okay</b><br> And this is some really really very long text which is not fitting inside my button tag and is overflowing out of the button. But this text is meant to be a small description of the heading. Lorem ipsum dolor sit
        amet, consectetur adipiscing elit. Morbi auctor iaculis enim eu ultrices. Pellentesque nec hendrerit mauris.
      </a>
    </div>
    <div id="search-result"></div>
  </div>
</div>

关于html - 文本溢出按钮而不是移动到下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49332856/

相关文章:

html - Bootstrap 4 - 右对齐导航栏元素

html - dl(详细列表)在 bootstrap 4 中不起作用

asp.net - Window.open javascript 函数在 Mozilla 中不起作用,但在其他浏览器中起作用

html - 使用 flexbox 的响应式表格

javascript - 更改背景图像 - javascript

jquery - 我如何定位 jquery 或 css 中的下一个或同级元素?

javascript - 用图像、CSS 或纯 JS 替换单选按钮。一些限制

javascript - 通过RequestDispatcher调用html页面获取请求参数

jquery - 使用 jquery 为跳过的必填字段着色

css - Bootstrap 4 导航栏防止按钮换行