fightstarr20
fightstarr20

Reputation: 12598

CSS vertically and horizontally position text over an image

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

Upvotes: 1

Views: 82

Answers (3)

user2164884
user2164884

Reputation:

Try the following CSS:

.banner {
    position: relative;
}

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

Upvotes: 1

Dan Winter-Wijntjes
Dan Winter-Wijntjes

Reputation: 312

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/

Upvotes: 1

Ilanus
Ilanus

Reputation: 6928

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/

Upvotes: 1

Related Questions