0

I would like to take a logo image, centre it exactly in the middle of the screen, when keeping the image in its original size. Then right under that image (- 5px down), put a small text line, where I choose its font.

Everything is messed up when I am trying to do that, so I will put the basic. How can I achieve this simple goal?

<html>
    <body>
        <img src="images/logoBlackBig.png" alt="W3Schools.com" >
    </body>
</html>
halfer
  • 19,824
  • 17
  • 99
  • 186
Curnelious
  • 1
  • 16
  • 76
  • 150
  • Do you want the image to be at full size and centered even if the window is smaller than the image, effectively cropping off the sides? – Trevin Avery Oct 11 '14 at 16:24
  • hi , the image is always smaller than screen , its a logo .but lets say i want to proportionally change its size according to screens . – Curnelious Oct 11 '14 at 16:26

2 Answers2

5

You could achieve this by using css transform and absolute position like this:

JSFiddle - DEMO

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
<style>
* {margin: 0; padding: 0;}
.div {
    position: absolute;
    top: 50%;
    left: 50%;
    -o-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    border: 1px solid #000;
    text-align: center;
}
.img {
    vertical-align:middle;
}
.p {
    margin: 0;
    position: absolute;
    bottom: 0;
    width: 100%;
    color: #000;
    font-size: 24px;
}
</style>
</head>
<body>
    <div class="div">
        <img class="img" src="http://media.tumblr.com/tumblr_m6cfbeVrUy1qegis6o1_400.gif" alt="W3Schools.com">
        <p class="p">MY TEXT</p>
    </div>
</body>
</html>
Anonymous
  • 10,002
  • 3
  • 23
  • 39
3

Improved - now with image re-sizing

Building Blocks

  • The image and text are wrapped in a div.

  • The div is centered thanks to the right combination of position: absolute, top, right, bottom, left and margin: auto.

  • The container will re-size to its max width and height with height: 100% and width: 100%

  • max-height: 100% and max-width: 100% ensures that the image width height ratio remains 1:1 when re-sizing.

IE 8 needs a fixed pixel height for the container.

Change max width and height to match the image.

Demo

* {
  margin: 0;
  padding: 0;
}
.centered {
  position: absolute;
  top: -4em;
  /* change top unit size to get desired vertical placement */
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  height: 100%; /*IE 8 Needs px height */
  width: 100%;
  max-height: 300px;
  max-width: 300px;
  min-width: 100px;
  text-align: center;
}
.centered img {
  max-height: 100%;
  max-width: 100%;
  min-width: 100px;
}
<div class="centered">
  <img src="http://www.placehold.it/300" />
  <h1>Green Eggs and Ham</h1>
</div>
misterManSam
  • 24,303
  • 11
  • 69
  • 89