Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top in position relative is not working as expected

Why doesn't work top attribute for <img> , when the tag <img> is sibling to <p>, but if I delete tag <p>, this works.

html,
body {
  height: 100%;
}

img {
  position: relative;
  top: 40%;
}
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus delectus accusantium nulla eveniet aperiam, quo odit qui voluptas. Illo vel sed ex dolores illum eum architecto a libero atque. Voluptatibus.</p>
<img src="http://lorempixel.com/400/200/" alt="">
like image 312
Mariadicar Avatar asked Oct 16 '25 07:10

Mariadicar


2 Answers

http://jsfiddle.net/9bxwxfe2/

Use a container div set to position: relative; and then add position: absolute; to the img.

like image 181
Marcel Burkhard Avatar answered Oct 17 '25 20:10

Marcel Burkhard


CSS:

html,body{
        height: 100%;
}

img{
        position: relative;
        top: 40%;
        float:left;
}

Use float:left; in the img class. This will solve the problem and image will be displayed in expected position(40% from the top of the screen).

like image 28
Prakash Paramasivam Avatar answered Oct 17 '25 21:10

Prakash Paramasivam