-2

I want to position my text exactly in the center of my background image without using and position relative or absolute. Most of the answers online use position absolute for the text and a top. But i don't want to use any positioning

4 Answers4

0

Did you try:

margin: 0 auto;
text-align: center;
vertical-align: middle;
Jared Chu
  • 2,360
  • 3
  • 22
  • 35
0

You can use also the inline margin(top,bottom,right or left) to positioning where you want your image. You simply have to put your margin with px(es margin:10px) and you can try this online on the browser(normally just press F12 and you have the screen with all information)

travis_91
  • 151
  • 1
  • 11
0

you can use flex for it.

.container {
    display: flex;
    width: 350px;
    height: 350px;
    background-image: url('http://www.intrawallpaper.com/static/images/abstract-mosaic-background.png')
}

.item {
    background-color: white;
    width: 80px;
    height: 80px;
    margin: auto;
}
<div class="container">
  <div class="item">Sample Text</div>
</div>
chirag
  • 1,599
  • 1
  • 14
  • 27
-1

Using Flexbox solves the issue quite easily

.test{
  background-image:url("http://blogueandoalos50.com/wp-content/uploads/2015/01/fondo9.jpg");
  height:200px;
  display: flex;
  align-items: center;
  justify-content: center;
  }
<div class="test">Hello World</div>
abhirathore2006
  • 2,807
  • 22
  • 29
  • its css, and every browser behaves different.. if you want full support use JavaScript or you can check this one http://stackoverflow.com/questions/17447807/cross-browser-support-for-css-flexbox @ArtemSolovev marking others answer -1 doesn't help you. specially when it solves the issue ...and user isn't specified specific browser requirements – abhirathore2006 Oct 20 '16 at 06:08
  • @ArtemSolovev world moves forward and there are compilers which auto generate the browser support so.. code doesn't get polluted, you just write original flex version... – abhirathore2006 Oct 20 '16 at 06:13