admin管理员组文章数量:1310043
I want a div
to be positioned below the screen, but when you scroll it to appears. I can do this using margin-top
but this requires a specific number whereas I want it to be relative to the screen it is on, so that it appears when you just start to scroll down on all screens.
How can I do this? Thanks.
I want a div
to be positioned below the screen, but when you scroll it to appears. I can do this using margin-top
but this requires a specific number whereas I want it to be relative to the screen it is on, so that it appears when you just start to scroll down on all screens.
How can I do this? Thanks.
Share Improve this question edited Jun 21, 2015 at 0:24 HaveNoDisplayName 8,517106 gold badges40 silver badges50 bronze badges asked Jun 17, 2015 at 6:22 BMJ1260BMJ1260 351 gold badge1 silver badge8 bronze badges 3- 2 post what you tryed.. – Sarath Kumar Commented Jun 17, 2015 at 6:24
- Use position:fixed property to fix the elements on the page. – stanze Commented Jun 17, 2015 at 6:29
-
Don't use
margin-top
, setposition: absolute
andbottom
to the negative height. – Rory McCrossan Commented Jun 17, 2015 at 6:53
4 Answers
Reset to default 5Use vh
:
iewport-percentage lengths defined a length relatively to the size of viewport, that is the visible portion of the document. Only Gecko-based browsers are updating the viewport values dynamically, when the size of the viewport is modified (by modifying the size of the window on a desktop puter or by turning the device on a phone or a tablet).
#myEl {
position: absolute;
top: 100vh;
}
Docs: https://developer.mozilla/en-US/docs/Web/CSS/length
Demo: http://jsfiddle/tusharj/g8pfow8r/
This can be handled using position:fixed
and use top
instead of margin-top
div {
position:fixed;
top:100px;
}
if you are using jquery, you can use it like that in your javascript, also in window.resize
var windowHeight = window.innerHeight;
$("#box").css('position','absolute');
$("#box").css('top',windowHeight+'px');
viewport
values help you define attributes relative to screen size:
margin-top:20vw;
JSFiddle
Take a look here
本文标签: javascriptSet the margintop of a divrelative to screen resolutionStack Overflow
版权声明:本文标题:javascript - Set the margin-top of a div, relative to screen resolution - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741836935a2400271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论