Skip to content Skip to sidebar Skip to footer

Vertical Align Middle And Centered Text On Top Of Image

I am trying to place the text over an image using vertical aligned middle and text align center. But none of them seems working. My html is

Solution 1:

You can use display: flex on the .hero-image in combination with align-items: center;:

.hero-image {
  display: flex;
  position: relative;
  align-items: center;
}
.hero-imageimg{
  width: 100%;
}

.hero-imageh2 {
  background: #ff2bff none repeat scroll 00;
  color: #fff;
  text-align: center;
  width: 200px;
  margin: 0px auto;
  position: absolute;
  left: 0;
  right: 0;
}
<divclass="hero-image"><imgsrc="https://s23.postimg.org/ahcg75tsb/hero.png"alt=""><ahref=""><h2>Some Sample text</h2></a></div>

Solution 2:

Make the "top" padding, See the code:

.hero-image {
  display: block;
  position: relative;
}

.hero-imagea {
  background: #ff2bff none repeat scroll 00;
  color: #fff;
  left: 0;
  position: absolute;
  right: 0;
  text-align: center;
  top: 45%;
  width: 200px;
  bottom: 0;
  vertical-align: middle;
  text-align: center;
  margin: 0px auto;
  height: 50px
}
<divclass="hero-image"><imgsrc="https://s23.postimg.org/ahcg75tsb/hero.png"alt=""><ahref=""><h2>Some Sample text</h2></a></div>

Post a Comment for "Vertical Align Middle And Centered Text On Top Of Image"