0

I need my div with the id "splashlogo" to stay centred of my document at all times. I have tried so many different things but not sure why its not working. Can anyone help?

.splashscreenlogo {
  position: absolute;
  top: 42%;
  left: 25%;
}
<div class="splashscreenlogo">
  <div id="splashlogo">
    <img src="logo_splashscreen.png" alt="Splashscreen logo">
  </div>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272
Clarke
  • 47
  • 1
  • 9

6 Answers6

1

You can do it in this way

Say for example if your image height is 100px and image width is 100px you can always center it like this

<div class="splashscreenlogo">
  <div id="splashlogo">
    <img src="logo_splashscreen.png" alt="Splashscreen logo" width=655 height=138>
  </div>
</div>

.splashscreenlogo {
  position: absolute;
  top: 50%;
  left: 50%;
    margin-top: -69px;
    margin-left: -327.5px
}

Refer Fiddle

Sai Deepak
  • 678
  • 4
  • 16
1

Considering the div has display as block you can use something like this

<div class="splashscreenlogo">
  <center>
  <div id="splashlogo">
    <img src="logo_splashscreen.png" alt="Splashscreen logo">
  </div>
  </center>
</div>

Or Something Like This

<div class="splashscreenlogo" style="text-align:center">

  <div id="splashlogo">
    <img src="logo_splashscreen.png" alt="Splashscreen logo">
  </div>

</div>

Or Something Like This

<div class="splashscreenlogo" style="margin:0 auto">

  <div id="splashlogo">
    <img src="logo_splashscreen.png" alt="Splashscreen logo">
  </div>

</div>
Raj Nandan Sharma
  • 3,694
  • 3
  • 32
  • 42
1

If you have a full splash screen, which it sounds like you do, I would use the transform trick.

The important thing here is that this solution does not need to know the size of your splash image- you don't need to change your CSS to accommodate a differently sized splash image.

The CSS is quite simple for this- basically you position the upper left corner absolutely at 50%, 50%; then transform the image by half of it's width and height to effectively move the center of the object to that window midpoint.

#splashlogo {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translateX(-50%) translateY(-50%);
}


/* Page styling, ignore this */
body {
  background-color: #bada55; }
img {
  box-shadow: 6px 6px 3px rgba(0,0,0,0.25); }
<div class="splashscreenlogo">
  <div id="splashlogo">
    <img src="http://www.placecage.com/300/300" alt="Splashscreen logo">
  </div>
</div>
CaseyHunt
  • 535
  • 1
  • 4
  • 17
0

Make sure your absolute div is within a relative container first then.

#splashlogo {
  position:relative;
}

.splashscreenlogo {
  position: absolute;
  width:*yourwidth*
  height:*yourheight*
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin:auto;
}

You will also want some width & height in there for the absolute method.

0

I usually use margin: 0 auto or you can also use left: 50%. This is for horizontal centering. For vertical centering you can use top: 50% or line-height.

Tadeáš Jílek
  • 2,813
  • 2
  • 19
  • 32
0

This will keep the slapshlogo centered, both horizontally and vertically under most circumstances, even on vertical re-size.

.splashscreenlogo {
  height: 300px;
  width: 300px;
  background: #fff; /*#fff for white background*/
  position: absolute;
  /*margin property shorthand notation*/
  margin: -150px 0 0 -150px;
  left: 50%;
  top: 50%;
}
#splashlogo {
  height: 100px;
  width: 100px;
  background: #fff; /*#fff for white background*/
  position: absolute;
  /*margin property shorthand notation*/
  margin: -50px 0 0 -50px;
  left: 50%;
  top: 50%;
}
<div class="splashscreenlogo">
  <img id="splashlogo" src="http://www.clker.com/cliparts/o/i/x/2/k/G/ink-splash-with-drops-black-hi.png" alt="Splashscreen logo">
</div>

source: How to Center Anything With CSS

Ashesh
  • 3,499
  • 1
  • 27
  • 44