1

I am trying to vertically and horizontally center the div banner_title within another div like this...

.box {
  text-align: center;
}
.banner_title {
  font-size: 32px;
  position: absolute;
  top: 50%;
  width: 100%;
}
.banner_title > div {
  background: rgba(0, 0, 0, 0.7) none repeat scroll 0 0;
  color: #fff;
  padding: 15px;
}
<div class="box">
  <img src="http://dummyimage.com/1000x400/b895b8/fff.jpg&text=+">
  <div class="banner_title">
    <div>
      THIS MESSAGE IS A TEST MESSAGE
    </div>
    <button>
      This is a test button
    </button>
  </div>
</div>

also on https://jsfiddle.net/j90gaawb/

This isn't working for me.

This is what I am trying to achieve...

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • 1
    Possible duplicate of [How to vertically center a div for all browsers?](http://stackoverflow.com/questions/396145/how-to-vertically-center-a-div-for-all-browsers) – henry Sep 06 '16 at 20:51
  • This duplicates many existing SO questions. Check out http://stackoverflow.com/questions/396145/how-to-vertically-center-a-div-for-all-browsers and http://stackoverflow.com/questions/19461521/how-to-center-an-element-horizontally-and-vertically/19461564#19461564 – henry Sep 06 '16 at 20:52

3 Answers3

1

Is this what you are looking for?

.box {
  text-align: center;
  position: relative;
  }

.banner_title {
  position: absolute;  
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.banner_title > div {
   background: rgba(0, 0, 0, 0.7) none repeat scroll 0 0;color: #fff;
   padding: 15px;
}

Fiddle: https://jsfiddle.net/j90gaawb/1/

Ilanus
  • 6,690
  • 5
  • 13
  • 37
  • That works great, but if I increase the font size it goes on to 2 lines, is there a way to change this breakpoint? https://jsfiddle.net/j90gaawb/2/ – fightstarr20 Sep 06 '16 at 20:41
  • Not the original poster of this answer, but what about something like this? https://jsfiddle.net/r53o7wqb/. You set the width explicitly, but height is dynamic. – Joseph Marikle Sep 06 '16 at 20:51
  • @fightstarr20 that can be easily fixed with white-space: nowrap; see https://jsfiddle.net/j90gaawb/4/ – Ilanus Sep 06 '16 at 20:53
1

I would recommend the following method

.box {
  text-align: center;
  display: table;
  width: 100vw;
  height: 100vh;
}
.banner {
  font-size: 32px;
  display: table-cell;
  vertical-align: middle;
  height;
  auto;
  background-image: url('http://dummyimage.com/1000x400/b895b8/fff.jpg&text=+');
  background-size: cover;
}
.banner_title {
  background: rgba(0, 0, 0, 0.7) none repeat scroll 0 0;
  color: #fff;
  display: inline-block;
}
.banner_title > div {
  padding: 15px;
}
<div class="box">
  <div class="banner">
    <div class="banner_content">
      <div class="banner_title">
        THIS MESSAGE IS A TEST MESSAGE
      </div>
      <div>
        <button>
          This is a test button
        </button>
      </div>
    </div>
  </div>
</div>

https://jsfiddle.net/j90gaawb/6/

henry
  • 4,244
  • 2
  • 26
  • 37
1

Try the following CSS:

.banner {
    position: relative;
}

.box {
    left: 0;
    position: absolute;
    text-align: center;
    top: 30px;
    width: 100%;
}