Reputation: 41
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="">
Upvotes: 4
Views: 7868
Reputation: 2159
When the value is provided as a percentage, it is relative to the height of the containing block.
But img
has no containing block. You can add position: absolute;
to the img.
CSS:
img{
position: absolute;
top: 40%;
}
Upvotes: 1
Reputation: 3523
Use a container div
set to position: relative;
and then add position: absolute;
to the img.
Upvotes: 2
Reputation: 11
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).
Upvotes: 1
Reputation: 1121
Added float:left for p tag
New css,
html,body{
height: 100%;
}
img{
position: relative;
top: 40%;
}
p {
float:left
}
Upvotes: -1
Reputation: 1478
May this help you... My suggestion is to use px instead of %. It works fine.
html,body{
height: 100%;
}
img{
position: relative;
top: 40px;
}
https://jsfiddle.net/vy301c07/
Upvotes: 0