如何在背景图像的两侧用<hr />居中<h1> </h1>

时间:2013-04-16 01:18:49

标签: html css background-image

如何在背景图片 的两侧 创建带<h1>的居中<hr/>

我还需要它来处理各种文本长度,适合移动浏览,并且<hr/>可以达到其容器宽度的100%。

我想要这个样子,但是在背景图片上。 text on solid background

对于带有两侧行的文字,有很多答案(herehere herehere),但所有答案都依赖于使用纯色背景色在文本后面,这对我来说不起作用,因为我想要的页面有一个背景图像。

以下是我实现上述内容的方法,它可以处理各种长度的文本并且可以很好地扩展:

CSS

.title-box {
    height: 2px; 
    background-color: rgb(215, 0, 0); 
    text-align: center;
}
.title-outer {
    background-color:rgb(230, 230, 230); 
    position: relative; 
    top: -0.7em;
}
.title-inner {
    margin:0px 20px; 
    font-size: 17.5px; 
    font-weight:bold; 
    color:rgb(100, 100, 100);
}

HTML

<div class="title-box">
    <span class="title-outer">
        <span class="title-inner">OUR STORY</span>
    </span>
</div>

我已经尝试过以下方法,种类有效,但由于<h1><hr/>正在处理,它无法处理各种文字宽度或缩放比例单独<div> s:

text on background image

HTML

<div class="row">
    <div class="span4"><hr /></div>
    <div class="span4"><h4>OUR STORY</h4></div>
    <div class="span4"><hr /></div>
</div>

注意:这是使用Bootstrap网格系统的示例,但这不是问题/解决方案的一部分。

所以任何想法如何我可以获得相同的外观和行为,但没有文本的背景颜色,所以它可以坐在背景图像上?

4 个答案:

答案 0 :(得分:4)

不需要JS,这是一个纯CSS解决方案。

<强> CSS

.title-hr hr {
    display: inline-block;
    width: 30%;
    margin: 5px 10px;
    border-top: 1px solid #e5e5e5;
}

<强> HTML

<h1 class="title-hr"><hr />My Title<hr /></h5>

结果:http://jsfiddle.net/yptmftr4/

答案 1 :(得分:2)

好的,我用这段代码玩了一下,这是我的解决方案。是的,它有点脏,因为我使用了:before:after,但是有效。

<强> HTML

<div class="title-box">
    <span id="first" class="title-inner">OUR LOOOoo oooo oOONG STORY</span>
</div>
<div class="title-box">
    <span id="second" class="title-inner">OUR STORY</span>
</div>
<div class="title-box">
    <span id="third" class="title-inner">STORY</span>
</div>

<强> CSS

.title-box {
    text-align: center;
}
.title-inner {
    margin:0px 20px;
    font-size: 17.5px;
    font-weight:bold;
    position: relative;
    color:rgb(100, 100, 100);
}
.title-inner:after, .title-inner:before {
    content:"";
    float: right;
    position: relative;
    top: 8px;
    height: 2px;
    background: red;
}
.title-inner:before {
    float: left;
}

<强>的jQuery

$(document).ready(function () {

    function work() {
        $(".title-inner").each(function () {
            var full_width = $(window).width();
            var id = $(this).attr("id");
            var title_width = $("#" + id).width();
            var new_width = (full_width - title_width) / 2 - 40;
            $('head').append("<style>#" + id + ":before, #" + id + ":after{width:" + new_width + "px !important;}</style>");
        });
    }
    work();
    $(window).resize(function () {
        work();
    });
});

http://jsfiddle.net/ffb3X/4/

由于:before:after不属于DOM,我使用.append()函数为每个标题添加样式标记。

此代码将在页面加载时计算所有内容,因此它具有响应性。

答案 2 :(得分:2)

此代码最初由Arbel发布,但由于某些原因,他/她的回答消失了?我正在重新发布它(包括我制作的一些mod),因为它是我最终使用的解决方案。信用到期的信用。

工作jsFiddle:http://jsfiddle.net/pA5Gu/

<强> HTML

<div class="title-box"> 
    <fieldset class="title-outer">
            <legend id="titleInner" class="title-inner">OUR STORY</legend>
    </fieldset>
</div>

<强> CSS

.title-box { 
    background-image: url('http://imagezo.com/images/1302-green-bubbles-awesome-background-wallpaper.jpg');
    height:100%;
}
.title-outer {
    border-top:2px solid rgb(215, 0, 0);
    background-color: transparent;
}
.title-inner {
    width:auto;
    padding:0px 20px;
    border: 0;
    background-color: transparent;
    font-size: 17.5px; 
    font-weight:bold; 
    color:rgb(255, 255, 255);
}

<强>的jQuery

$(document).ready(function() {
    var legendWidth = $('#titleInner').outerWidth();
    var margin = 'calc((100% - '+legendWidth+'px) / 2)';
    $('#titleInner').css('margin-left', margin);
    $('#titleInner').css('margin-right', margin);
});

答案 3 :(得分:0)

http://jsfiddle.net/habo/HrfuH/1/

<div class="title-box">
    <div class="myContent">
    <div class="title-outer"><hr /></div>
    <div class="title-inner "><h4>OUR STORY</h4></div>
    <div class="title-outer"><hr /></div>
    </div>
</div>


.myContent{
    display:block;
    width:600px;
    margin:0 auto;
}
.title-box {
    background:#eee;
    height:60px;
}
.title-outer{

}

hr {
    height: 2px;
    background-color:rgb(215, 0, 0);
    margin: 2em 0;
    width:25%;
    float:left;
}

.title-inner {
    margin:0px 20px; 
    font-size: 17.5px; 
    font-weight:bold; 
    color:rgb(100, 100, 100);
    float:left;
}