0

How do I create centered <h1> with <hr/> on both sides over a background image?

I also need it to handle various text lengths, scale well for mobile viewing and have the <hr/> go to 100% width of its container.

I want this look, but over a background image. text on solid background

There are lots of answers (here, here here and here) for text with lines on either side but all of them rely on using a solid background colour behind the text, which doesn't work for me as the page I want to put this on has a background image.

Here is how I achieve the look above, which handles various lengths of text and scales well:

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>

I have tried the method below and it kind of works but it doesn't handle various text widths or scale well due to the <h1> and the <hr/>s being in seperate <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>

Note: This is example is using the Bootstrap grid system but that is not part of the problem/solution.

So any ideas how I can get the same look and behaviour but without the backgound colour for the text so it can sit over a background image?

Community
  • 1
  • 1
Dhaust
  • 5,470
  • 9
  • 54
  • 80

4 Answers4

6

No need JS, here is a pure CSS solution.

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>

Result: http://jsfiddle.net/yptmftr4/

hthetiot
  • 359
  • 4
  • 8
2

Ok, I've played a bit with this code and here is my solution. Yes, it's a bit dirty because I've used :before and :after, but works.

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/

Because :before and :after are not part of DOM, I've used .append() function to append style tags in head for every title.

This code will on page load calculate everything, so it's responsive.

Miljan Puzović
  • 5,840
  • 1
  • 24
  • 30
  • +1 Thanks for posting this up. Interesting approach to use :before and :after. I have only recently become aware of those but never used them. See the [answer I posted](http://stackoverflow.com/a/16028515/242) (Note: Arbel wrote that code) which produces similar results but without having to reload the page. – Dhaust Apr 16 '13 at 04:09
  • @DavidHAust, :before and :after are aswesome :) Also, look at my updated jsfiddle, I've added .resize() function, so now there is no need to reload page. But remember - this is only for demo, because there is no need for that on site. Responsive site will get viewport on page load, users will not resize their viewports. – Miljan Puzović Apr 16 '13 at 04:13
  • Actually, users **will** resize their viewports, because they have portrait and landscape view on tablets and mobile phones :) – Miljan Puzović Apr 16 '13 at 04:22
  • Thanks for your help. Will definitely be using :before and :after in the future. I've ended up going with Arbel's solution. HTML has one more element that yours but the CSS and JS are a bit tidier. Cheers again. – Dhaust Apr 16 '13 at 04:41
1

This code was posted originally by Arbel but his/her answer disappeared for some reason? I am reposting it (including some mods I've made) because it was the solution I ended up using. Credit where credit is due.

Working 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);
});
Community
  • 1
  • 1
Dhaust
  • 5,470
  • 9
  • 54
  • 80
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;
}
HaBo
  • 13,999
  • 36
  • 114
  • 206
  • Thanks HaBo but I am trying to avoid hard coding the widths. Looking for something more dynamic. Arbel and Miljan suggested using jQuery to calculte the widths which is looking like it could work. – Dhaust Apr 16 '13 at 04:01
  • agreed. it looks better way of handling this. – HaBo Apr 16 '13 at 13:17