html - 使图像充当文本下划线

标签 html css

我想让这个笔划图像充当文本下划线,除了笔划图像等于容器 div 宽度而不是文本长度宽度之外,我几乎明白了。

h1 { text-align: center; }

.container { width: 500px; margin: 0 auto;}

.text-underline {
    background: transparent url("https://cockandbullfestival.co.uk/wp-content/themes/cock_bull/img/nav_underline.png") center bottom repeat-x;
}
<div class="container">
    <h1 class="text-underline"> Text With Stroke Underline </h1>
</div>

我怎样才能使这个笔划图像充当文本下划线?

最佳答案

考虑 h1 中的 span 元素 (内联元素),这样您就有了收缩以适应行为。

h1 {
  text-align: center;
}

.container {
  width: 500px;
  margin: 0 auto;
}

.text-underline span {
  background: url("https://cockandbullfestival.co.uk/wp-content/themes/cock_bull/img/nav_underline.png") center bottom repeat-x;
}
<div class="container">
  <h1 class="text-underline"> <span>Text With Stroke Underline</span> </h1>
</div>

它也适用于多行文本

h1 {
  text-align: center;
}

.container {
  width: 500px;
  margin: 0 auto;
}

.text-underline span {
  background: url("https://cockandbullfestival.co.uk/wp-content/themes/cock_bull/img/nav_underline.png") center bottom repeat-x;
}
<div class="container">
  <h1 class="text-underline"> <span>Text With Stroke Underline Underline Underline Underline Underline Underline</span> </h1>
</div>

关于html - 使图像充当文本下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53886873/

相关文章:

javascript - 在页面加载之前触发 jQuery 代码,这可能吗?

css - 如果使用 chrome 打印时 border-radius 更大,则 border-width 会发生变化

html - 在 css 中将 div 和/或 ul 居中

javascript - 如何获取canvas的中心点

css - Susy Grid 没有得到 Grid Padding

html - Twitter 引导填充不起作用

css - 在正文标记中使用背景图像时无法将背景颜色应用于登录框

html - 在布局中针对不同的移动版本

html - Twitter bootstrap float div right

javascript - localStorage 的 'storage' 事件中的未定义属性