html - 如何在 <h1> 中使用图像和重复背景

标签 html css

我正在尝试创建一个 <h1>以文本为中心,每边一个图像,然后动态填充行以达到容器的剩余宽度。

我已经尝试了一些技术...目前使用 :before 和 :after,带有嵌套跨度。 :after 正在工作,但试图解决 :before 部分。

fiddle :jsfiddle.net/dtar4rgs/

最佳答案

您可以使用 flex实现您想要做的事情( demo (supports flexbox)demo (doesn't support flexbox) ):

<div id="container">
    <h1><span>a b c</span></h1>
    <p>Change text shorter or longer.</p>
    <p>Need left lines to be dynamic.</p>
</div>
h1 {
    display:flex; /* Treats the :before, :after, and span as individual items */
    align-items:center; /* Aligns items vertically (to align border properly) */
    text-align:center; /* Aligns items horizontally for browsers that don't support flexbox */
}
h1 span {
    /* Places arrow on each side of span */
    background: url('https://dl.dropboxusercontent.com/u/101534624/arrow_cyan.gif') no-repeat left,
    url('https://dl.dropboxusercontent.com/u/101534624/arrow_cyan.gif') no-repeat right;
    padding: 0 50px; /* Prevents text from overlapping image */
}
h1:before,
h1:after {
    content: " ";
    /* Lines on sides of header */
    background: url('https://dl.dropboxusercontent.com/u/101534624/h1-lines.png') repeat-x;
    height: 6px; /* Height of lines */
    flex-grow:1; /* Makes lines expand to edges of screen */
}

注意: Support for flexbox缺少 (IE11+),但使用 text-align:center,唯一的区别是缺少行。

关于html - 如何在 <h1> 中使用图像和重复背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25370340/

相关文章:

css - 我可以使用 CSS 将 HTML 选择转换为 "regular text"吗?

javascript - 我可以在某个级别停止事件的传播吗?

php - 如何使用 GET 将加密消息传递到浏览器?

javascript - Zotonic:如何创建包含 Javascript 的 HTML 页面?

html - 停止 CSS 动画

jquery - CSS 样式被表格打断

javascript - 如何让我的页面在不滚动的情况下按比例显示?

html - 你如何获得位置 :absolute; element above a position:relative; one

html - 使用 CSS Grid 和 Flexbox 水平对齐位于不同网格单元格中的元素

javascript - CSS - 更改框阴影列表中的特定框阴影