Top in position relative 没有按预期工作

问题描述 投票:0回答:5
html css css-position
5个回答
2
投票

http://jsfiddle.net/9bxwxfe2/

使用容器

div
设置为
position: relative;
然后添加
position: absolute;
到img.


1
投票
CSS:

html,body{
        height: 100%;
}

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

使用浮动:左;在 img 类中。这将解决问题,图像将显示在预期位置(距离屏幕顶部 40%)。


1
投票

当该值以百分比形式提供时,它是相对于包含块的高度。

但是

img
没有包含块。你可以添加
position: absolute;
到img.

CSS:

img{
    position: absolute;
    top: 40%;
}

0
投票

这可以帮助你... 我的建议是使用 px 而不是 %。它工作正常。

html,body{
    height: 100%;
}

img{
    position: relative;
    top: 40px;

}

https://jsfiddle.net/vy301c07/


-1
投票

为 p 标签添加 float:left

新的CSS,

html,body{
    height: 100%;
}

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

见 jsfiddle

© www.soinside.com 2019 - 2024. All rights reserved.