html - 如何在标题周围创建双面虚线?

标签 html css

<分区>

我正在尝试在模板顶部实现标题 (h1),如下所示(点代表垂直居中的点图像线):

title example http://www.pixelplus.nl/klanten/klijsen/example.jpg

通常我会这样做:

<h1><span>This is a title</span></h1>

h1 中的文本居中,并向 span 添加背景颜色以及一些填充。

在当前元素中,我正在处理 background-image 上的透明背景。所以...背景色 span 不会飞。

在尝试了一些事情之后,这最接近:

<header class="headerPage">
    <div class="row">
        <div class="dotted">&nbsp;</div>
        <div class="title"><h1>This is a title</h1></div>
        <div class="dotted">&nbsp;</div>
    </div>
</header>

还有这个 CSS:

header.headerPage {
    display: table;
    margin: 0 0 35px;
    width: 100%;
}
header.headerPage .row {display: table-row;}
header.headerPage .row div {display: table-cell;}
header.headerPage .row div.dotted {
    width: 10%;
    background: url('../img/line-dotted.svg') left center repeat-x transparent;
}
header.headerPage .row div.title {
    padding: 0 15px;
    text-align: center;
}
header.headerPage .row div.title > * {display: inline;}
header.headerPage .row div h1 {margin: 0;}

如您所见,标题充当表格。问题出在 dotted-divs 的 width: 10%; 中。我如何让这些相对于 h1 的动态高度具有可变宽度?希望这可以在 css/scss 中完成。

最佳答案

此解决方案改编自此答案:Line separator under text and transparent background

虚线会根据文本的高度(字体大小,多行)保持垂直居中,并适应文本的宽度:

dotted line separator

@import url(http://fonts.googleapis.com/css?family=Open+Sans:300);
body{
    background-image: url(http://fr.playstation.com/media/5ZfqPjVF/BigSkyInfinity_Hero_EN.JPG);
    background-repeat:no-repeat;
    background-size:100% auto;
    font-family: 'Open Sans', sans-serif;
}

.divider{
    color:#ccc;
    width:70%;
    margin:20px auto;
    overflow:hidden;
    text-align:center;   
    line-height:1.2em;
}

.divider:before, .divider:after{
    content:"";
    vertical-align:middle;
    display:inline-block;
    width:50%;
    border-bottom:2px dotted #ccc;
    margin:0 2% 0 -55%;
}
.divider:after{
    margin:0 -55% 0 2%;
}
h1:nth-child(2){
    font-size:3em;
}
span{
  display:inline-block;
  vertical-align:middle;
  }
<h1 class="divider">Today</h1>
<h1 class="divider">Today News</h1>
<h1 class="divider"><span>Today News<br/>More text<span></h1>

请注意,如果您没有多行,则可以在没有 <span> 的情况下实现效果并且只有一个标签。

关于html - 如何在标题周围创建双面虚线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28318976/

相关文章:

css - JSF 是如何缓存 CSS 的?

javascript - 如何在 HTML5 JavaScript 中检测 Joy-Con 输入/运动控件

javascript - 如何从 knob.js 获取值

javascript - 菜单图标动画 jQuery

html - div 多个 float 向左拉伸(stretch)

iPhone CSS - 视口(viewport)宽度对我不起作用

javascript - react native Flexbox : Distributing the items evenly

asp.net - 为什么 DIV 在此代码中的 ASP 标记内无效?

javascript - 文本悬停在图像上

html - 使 ul nav 的 block 元素自动填充整个宽度