从窗口底部看到的元素100px的CSS顶部

时间:2013-02-01 23:50:45

标签: javascript html css

是否可以使用绝对定位将块级元素的顶部从窗口底部放置100px?

在我的应用程序中,我无法将此元素从页面顶部锚定给定数量的像素,因为页面上的几个元素具有未知高度,我将不得不使用JavaScript来动态测量,调整大小和放置这些元素。

感谢您的时间。

4 个答案:

答案 0 :(得分:1)

要相对于您要使用的浏览器窗口定位项目position:fixed,而不是position:abslute。然后你只需设置bottom: 100px或者你喜欢的距离窗口底部。

答案 1 :(得分:1)

不,如果没有利用JavaScript或基于所定位元素高度的服务器端逻辑,就不可能使用position:absolute(但听起来你也不知道)。

答案 2 :(得分:0)

我不确定这一点,但也许这可能会有所帮助:

<div class='b'>
    <div class='a'>
        element
    </div>
</div>

.a {
    margin-bottom:-100%;
}

.b {
    position:absolute;
    bottom:100px;
}

http://jsfiddle.net/pmB9U/

答案 3 :(得分:0)

如果可以使用不跨越整个页面的滚动条,这是非常可能的:

<!DOCTYPE html>
<html>

<head>
<title>Emulate position:fixed</title>
<style type="text/css">
html, body {
    margin: 0;
    padding: 0;
}
body {
    height: 100%;
    overflow: hidden;
}
#content {
    position: absolute;
    top: 50px;
    bottom: 100px;
    width: 100%;
    overflow: auto;
}
#header {
    height: 50px;
    background-color: yellow;
    position: absolute;
    top: 0;
    width: 100%;
    height: 50px;
}
#footer {
    position: absolute;
    background-color: yellow;
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 100px;
}
</style>
</head>

<body>
<div id="header">
    header
</div>
<div id="content">
    <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
    <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
    <p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
    <p>Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
    <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis.</p>
</div>
<div id="footer">
    footer
</div>
</body>

</html>