html - 防止自动背景大小调整为全线宽标题文本

标签 html css

我正在尝试在我的 <h1> 上使用 CSS 实现以下目标元素(因为它显然不是浏览器的默认行为):

enter image description here

但是,我似乎无法阻止我的浏览器将其呈现如下:

enter image description here

有人知道如何正确地实现这一目标吗?

当前代码:

.title {
  width: 300px;
  float: left;
  line-height: 68px;
  display: inline-block;
}

.title h1 {
  font-size: 42px;
  line-height: 58px;
  color: #FFF;
  background: #000;
  white-space: normal;
  word-wrap: break-word;
  display: inline-block;
  vertical-align: bottom;
}
<div class="title">
  <h1>This is a title on two lines</h1>
</div>

最佳答案

display: inline 而不是 inline-block

.title {
  width: 300px;
  float: left;
  line-height: 68px;
  display: inline-block;
}

.title h1 {
  font-size: 42px;
  line-height: 58px;
  color: #FFF;
  background: #000;
  white-space: normal;
  word-wrap: break-word;
  display: inline;
  vertical-align: bottom;
}
<div class="title">
  <h1>This is a title on two lines</h1>
</div>

关于html - 防止自动背景大小调整为全线宽标题文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356980/

相关文章:

javascript - 通过 Jquery 用视频填充容器时出现问题

html - 表单无法正确使用 Mailto

html - 如何在禁用调整大小的情况下将 HTML 表单提交到弹出窗口?

javascript - 如何使元素成为浏览器窗口的高度?

css - IE7 调试问题 - 绝对背景

javascript - 当下拉值更改时将属性设置为只读

html - 网页内容以列表格式显示

javascript - 逐渐增加 eclipse 刻素描元素的不透明度

css - bootstrap 按钮在单击时显示蓝色轮廓

css - 将表转换为 div - css